Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
|
|
@ -0,0 +1,121 @@
|
|||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <filesystem>
|
||||
#include <fstream>
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
|
||||
class ConfigData {
|
||||
public:
|
||||
std::string favourite_fruit;
|
||||
bool needs_peeling;
|
||||
bool seeds_removed;
|
||||
uint32_t number_bananas;
|
||||
uint32_t number_strawberries;
|
||||
};
|
||||
|
||||
std::string to_upper_case(const std::string& text) {
|
||||
std::string result = text;
|
||||
std::transform(result.begin(), result.end(), result.begin(),
|
||||
[](const char& ch) { return std::toupper(ch); });
|
||||
return result;
|
||||
}
|
||||
|
||||
void read_config_file(const std::string& read_file, const ConfigData& data) {
|
||||
std::ifstream read_stream;
|
||||
read_stream.open(read_file);
|
||||
const std::string write_file = "../temp_config.txt";
|
||||
std::ofstream write_stream;
|
||||
write_stream.open(write_file);
|
||||
if ( ! read_stream.is_open() || ! write_stream.is_open() ) {
|
||||
std::cout << "Unable to open files" << std::endl;
|
||||
}
|
||||
|
||||
bool done_fruit = false;
|
||||
bool done_peeling = false;
|
||||
bool done_seeds = false;
|
||||
bool done_bananas = false;
|
||||
bool done_strawberries = false;
|
||||
std::string line;
|
||||
while ( std::getline(read_stream, line) ) {
|
||||
if ( line.empty() || line[0] == '#' ) {
|
||||
write_stream << line << "\n";
|
||||
continue;
|
||||
}
|
||||
|
||||
const std::string words = ( line.substr(0, 2) == "; " ) ?
|
||||
to_upper_case(line.substr(2)) : to_upper_case(line);
|
||||
|
||||
if ( words.empty() ) { continue; }
|
||||
if ( words.substr(0, 14) == "FAVOURITEFRUIT" ) {
|
||||
if ( done_fruit ) { continue; }
|
||||
done_fruit = true;
|
||||
write_stream << "FAVOURITEFRUIT " << data.favourite_fruit << "\n";
|
||||
} else if ( words.substr(0, 12) == "NEEDSPEELING" ) {
|
||||
if ( done_peeling ) { continue; }
|
||||
done_peeling = true;
|
||||
if ( data.needs_peeling ) {
|
||||
write_stream << "NEEDSPEELING" << "\n";
|
||||
} else {
|
||||
write_stream << "; NEEDSPEELING" << "\n";
|
||||
}
|
||||
} else if ( words.substr(0, 12) == "SEEDSREMOVED" ) {
|
||||
if ( done_seeds ) { continue; }
|
||||
done_seeds = true;
|
||||
if ( data.seeds_removed ) {
|
||||
write_stream << "SEEDSREMOVED" << "\n";
|
||||
} else {
|
||||
write_stream << "; SEEDSREMOVED" << "\n";
|
||||
}
|
||||
} else if( words.substr(0, 15) == "NUMBEROFBANANAS" ) {
|
||||
if ( done_bananas ) { continue; }
|
||||
done_bananas = true;
|
||||
write_stream << "NUMBEROFBANANAS " << data.number_bananas << "\n";
|
||||
} else if( words.substr(0, 20) == "NUMBEROFSTRAWBERRIES" ) {
|
||||
if ( done_strawberries ) { continue; }
|
||||
done_strawberries = true;
|
||||
write_stream << "NUMBEROFSTRAWBERRIES " << data.number_strawberries << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
// Insert statements if they were absent from the original file "config.txt"
|
||||
if ( ! done_fruit ) {
|
||||
write_stream << "FAVOURITEFRUIT " + data.favourite_fruit << "\n";
|
||||
}
|
||||
|
||||
if ( ! done_peeling ) {
|
||||
if ( data.needs_peeling ) {
|
||||
write_stream << "NEEDSPEELING" << "\n";
|
||||
} else {
|
||||
write_stream << "; NEEDSPEELING" << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! done_seeds ) {
|
||||
if ( data.seeds_removed ) {
|
||||
write_stream << "SEEDSREMOVED" << "\n";
|
||||
} else {
|
||||
write_stream << "; SEEDSREMOVED" << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! done_bananas ) {
|
||||
write_stream << "NUMBEROFBANANAS "<< data.number_bananas << "\n";
|
||||
}
|
||||
|
||||
if ( ! done_strawberries ) {
|
||||
write_stream << "NUMBEROFSTRAWBERRIES " << data.number_strawberries << "\n";
|
||||
}
|
||||
|
||||
read_stream.close();
|
||||
write_stream.close();
|
||||
|
||||
std::filesystem::remove(read_file);
|
||||
std::filesystem::rename(write_file, read_file);
|
||||
}
|
||||
|
||||
int main() {
|
||||
const std::string read_file = "../config.txt";
|
||||
ConfigData configData("banana", false, true, 1024, 62000);
|
||||
read_config_file(read_file, configData);
|
||||
}
|
||||
|
|
@ -0,0 +1,107 @@
|
|||
function Update-ConfigurationFile
|
||||
{
|
||||
[CmdletBinding()]
|
||||
Param
|
||||
(
|
||||
[Parameter(Mandatory=$false,
|
||||
Position=0)]
|
||||
[ValidateScript({Test-Path $_})]
|
||||
[string]
|
||||
$Path = ".\config.txt",
|
||||
|
||||
[Parameter(Mandatory=$false)]
|
||||
[string]
|
||||
$FavouriteFruit,
|
||||
|
||||
[Parameter(Mandatory=$false)]
|
||||
[int]
|
||||
$NumberOfBananas,
|
||||
|
||||
[Parameter(Mandatory=$false)]
|
||||
[int]
|
||||
$NumberOfStrawberries,
|
||||
|
||||
[Parameter(Mandatory=$false)]
|
||||
[ValidateSet("On", "Off")]
|
||||
[string]
|
||||
$NeedsPeeling,
|
||||
|
||||
[Parameter(Mandatory=$false)]
|
||||
[ValidateSet("On", "Off")]
|
||||
[string]
|
||||
$SeedsRemoved
|
||||
)
|
||||
|
||||
[string[]]$lines = Get-Content $Path
|
||||
|
||||
Clear-Content $Path
|
||||
|
||||
if (-not ($lines | Select-String -Pattern "^\s*NumberOfStrawberries" -Quiet))
|
||||
{
|
||||
"", "# How many strawberries we have", "NumberOfStrawberries 0" | ForEach-Object {$lines += $_}
|
||||
}
|
||||
|
||||
foreach ($line in $lines)
|
||||
{
|
||||
$line = $line -replace "^\s*","" ## Strip leading whitespace
|
||||
|
||||
if ($line -match "[;].*\s*") {continue} ## Strip semicolons
|
||||
|
||||
switch -Regex ($line)
|
||||
{
|
||||
"(^$)|(^#\s.*)" ## Blank line or comment
|
||||
{
|
||||
$line = $line
|
||||
}
|
||||
"^FavouriteFruit\s*.*" ## Parameter FavouriteFruit
|
||||
{
|
||||
if ($FavouriteFruit)
|
||||
{
|
||||
$line = "FAVOURITEFRUIT $FavouriteFruit"
|
||||
}
|
||||
}
|
||||
"^NumberOfBananas\s*.*" ## Parameter NumberOfBananas
|
||||
{
|
||||
if ($NumberOfBananas)
|
||||
{
|
||||
$line = "NUMBEROFBANANAS $NumberOfBananas"
|
||||
}
|
||||
}
|
||||
"^NumberOfStrawberries\s*.*" ## Parameter NumberOfStrawberries
|
||||
{
|
||||
if ($NumberOfStrawberries)
|
||||
{
|
||||
$line = "NUMBEROFSTRAWBERRIES $NumberOfStrawberries"
|
||||
}
|
||||
}
|
||||
".*NeedsPeeling\s*.*" ## Parameter NeedsPeeling
|
||||
{
|
||||
if ($NeedsPeeling -eq "On")
|
||||
{
|
||||
$line = "NEEDSPEELING"
|
||||
}
|
||||
elseif ($NeedsPeeling -eq "Off")
|
||||
{
|
||||
$line = "; NEEDSPEELING"
|
||||
}
|
||||
}
|
||||
".*SeedsRemoved\s*.*" ## Parameter SeedsRemoved
|
||||
{
|
||||
if ($SeedsRemoved -eq "On")
|
||||
{
|
||||
$line = "SEEDSREMOVED"
|
||||
}
|
||||
elseif ($SeedsRemoved -eq "Off")
|
||||
{
|
||||
$line = "; SEEDSREMOVED"
|
||||
}
|
||||
}
|
||||
Default ## Whatever...
|
||||
{
|
||||
$line = $line
|
||||
}
|
||||
}
|
||||
|
||||
Add-Content $Path -Value $line -Force
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
Update-ConfigurationFile -NumberOfStrawberries 62000 -NumberOfBananas 1024 -SeedsRemoved On -NeedsPeeling Off
|
||||
|
||||
Invoke-Item -Path ".\config.txt"
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
Set objFSO = CreateObject("Scripting.FileSystemObject")
|
||||
|
||||
'Paramater lookups
|
||||
Set objParamLookup = CreateObject("Scripting.Dictionary")
|
||||
With objParamLookup
|
||||
.Add "FAVOURITEFRUIT", "banana"
|
||||
.Add "NEEDSPEELING", ""
|
||||
.Add "SEEDSREMOVED", ""
|
||||
.Add "NUMBEROFBANANAS", "1024"
|
||||
.Add "NUMBEROFSTRAWBERRIES", "62000"
|
||||
End With
|
||||
|
||||
'Open the config file for reading.
|
||||
Set objInFile = objFSO.OpenTextFile(objFSO.GetParentFolderName(WScript.ScriptFullName) &_
|
||||
"\IN_config.txt",1)
|
||||
'Initialize output.
|
||||
Output = ""
|
||||
Isnumberofstrawberries = False
|
||||
With objInFile
|
||||
Do Until .AtEndOfStream
|
||||
line = .ReadLine
|
||||
If Left(line,1) = "#" Or line = "" Then
|
||||
Output = Output & line & vbCrLf
|
||||
ElseIf Left(line,1) = " " And InStr(line,"#") Then
|
||||
Output = Output & Mid(line,InStr(1,line,"#"),1000) & vbCrLf
|
||||
ElseIf Replace(Replace(line,";","")," ","") <> "" Then
|
||||
If InStr(1,line,"FAVOURITEFRUIT",1) Then
|
||||
Output = Output & "FAVOURITEFRUIT" & " " & objParamLookup.Item("FAVOURITEFRUIT") & vbCrLf
|
||||
ElseIf InStr(1,line,"NEEDSPEELING",1) Then
|
||||
Output = Output & "; " & "NEEDSPEELING" & vbCrLf
|
||||
ElseIf InStr(1,line,"SEEDSREMOVED",1) Then
|
||||
Output = Output & "SEEDSREMOVED" & vbCrLf
|
||||
ElseIf InStr(1,line,"NUMBEROFBANANAS",1) Then
|
||||
Output = Output & "NUMBEROFBANANAS" & " " & objParamLookup.Item("NUMBEROFBANANAS") & vbCrLf
|
||||
ElseIf InStr(1,line,"NUMBEROFSTRAWBERRIES",1) Then
|
||||
Output = Output & "NUMBEROFSTRAWBERRIES" & " " & objParamLookup.Item("NUMBEROFSTRAWBERRIES") & vbCrLf
|
||||
Isnumberofstrawberries = True
|
||||
End If
|
||||
End If
|
||||
Loop
|
||||
If Isnumberofstrawberries = False Then
|
||||
Output = Output & "NUMBEROFSTRAWBERRIES" & " " & objParamLookup.Item("NUMBEROFSTRAWBERRIES") & vbCrLf
|
||||
Isnumberofstrawberries = True
|
||||
End If
|
||||
.Close
|
||||
End With
|
||||
|
||||
'Create a new config file.
|
||||
Set objOutFile = objFSO.OpenTextFile(objFSO.GetParentFolderName(WScript.ScriptFullName) &_
|
||||
"\OUT_config.txt",2,True)
|
||||
With objOutFile
|
||||
.Write Output
|
||||
.Close
|
||||
End With
|
||||
|
||||
Set objFSO = Nothing
|
||||
Set objParamLookup = Nothing
|
||||
Loading…
Add table
Add a link
Reference in a new issue