Browse Source

Checking if configuration files changed at all before saving over them.

David Piuva 5 years ago
parent
commit
aee01bc863
3 changed files with 41 additions and 9 deletions
  1. 6 3
      Source/DFPSR/base/text.cpp
  2. 26 3
      Source/DFPSR/base/text.h
  3. 9 3
      Source/SDK/sandbox/sprite/spriteAPI.cpp

+ 6 - 3
Source/DFPSR/base/text.cpp

@@ -455,7 +455,7 @@ void dsr::doubleToString_arabic(String& target, double value) {
 	} \
 	TARGET[SOURCE.length()] = '\0';
 
-String dsr::string_load(const ReadableString& filename) {
+String dsr::string_load(const ReadableString& filename, bool mustExist) {
 	// TODO: Load files using Unicode filenames
 	TO_RAW_ASCII(asciiFilename, filename);
 	std::ifstream inputFile(asciiFilename);
@@ -472,8 +472,11 @@ String dsr::string_load(const ReadableString& filename) {
 		inputFile.close();
 		return result;
 	} else {
-		throwError("Failed to load ", filename, "\n");
-		return U"";
+		if (mustExist) {
+			throwError("Failed to load ", filename, "\n");
+		}
+		// If the file cound not be found and opened, a null string is returned
+		return String();
 	}
 }
 

+ 26 - 3
Source/DFPSR/base/text.h

@@ -190,20 +190,43 @@ String& string_toStreamIndented(String& target, const int8_t& value, const Reada
 String& string_toStreamIndented(String& target, const uint8_t& value, const ReadableString& indentation);
 
 // Procedural API
-// TODO: Create procedural constructors
-// TODO: Make wrappers around member methods
-String string_load(const ReadableString& filename);
+
+// Post-condition:
+//   Returns the content of the file referred to be filename.
+//   If mustExist is true, then failure to load will throw an exception.
+//   If mustExist is false, then failure to load will return an empty string.
+String string_load(const ReadableString& filename, bool mustExist = true);
+// Side-effect: Saves content to filename.
 void string_save(const ReadableString& filename, const ReadableString& content);
+// Post-condition: Returns true iff strings a and b are exactly equal.
 bool string_match(const ReadableString& a, const ReadableString& b);
+// Post-condition: Returns true iff strings a and b are roughly equal using a case insensitive match.
 bool string_caseInsensitiveMatch(const ReadableString& a, const ReadableString& b);
+// Post-condition: Returns text converted to upper case.
 String string_upperCase(const ReadableString &text);
+// Post-condition: Returns text converted to lower case.
 String string_lowerCase(const ReadableString &text);
+// Post-condition: Returns a clone of text without any white-space (space, tab and carriage-return).
 String string_removeAllWhiteSpace(const ReadableString &text);
+// Post-condition: Returns a sub-set of text without surrounding white-space (space, tab and carriage-return).
+// Unlike string_removeAllWhiteSpace, string_removeOuterWhiteSpace does not require allocating a new buffer.
 ReadableString string_removeOuterWhiteSpace(const ReadableString &text);
+// Pre-condition: Content must contain an integer, or unexpected things may happen.
+// Post-condition: Returns the numerical integer value of content while ignoring any forbidden characters.
+// Examples:
+//   string_parseInteger(U"-25") == -25 // Good case
+//   string_parseInteger(U" -25 ") == -25 // Still works
+//   string_parseInteger(U" 10x10 ") == 1010 // Any digits are simply added in order while ignoring the rest
 int64_t string_parseInteger(const ReadableString& content);
+// Post-condition: Returns the double-precision floating-point approximation of content's numerical value
 double string_parseDouble(const ReadableString& content);
+// Post-condition: Returns rawText wrapped in a quote.
+// Special characters are included using escape characters, so that one can quote multiple lines but store it easily.
 String string_mangleQuote(const ReadableString &rawText);
+// Pre-condition: mangledText must be enclosed in double quotes and special characters must use escape characters (tabs in quotes are okay though).
+// Post-condition: Returns mangledText with quotes removed and excape tokens interpreted.
 String string_unmangleQuote(const ReadableString& mangledText);
+
 // Append one element
 template<typename TYPE>
 inline void string_append(String& target, TYPE value) {

+ 9 - 3
Source/SDK/sandbox/sprite/spriteAPI.cpp

@@ -977,9 +977,15 @@ void sprite_generateFromModel(const Model& visibleModel, const Model& shadowMode
 			printText("  Saved atlas to ", targetPath, ".\n");
 		}
 
-		// TODO: Make a comparison against the old config file and see if it's significant enough to not be caused by rounding errors
-		string_save(targetPath + U".ini", configText);
-		printText("  Saved sprite config to ", targetPath, ".\n\n");
+		// Save the configuration
+		String configPath = targetPath + U".ini";
+		String oldConfixText = string_load(configPath, false);
+		if (string_match(configText, oldConfixText)) {
+			printText("  No significant changes against ", targetPath, ".\n\n");
+		} else {
+			string_save(targetPath + U".ini", configText);
+			printText("  Saved sprite config to ", targetPath, ".\n\n");
+		}
 
 		if (debug) {
 			ImageRgbaU8 debugImage; String garbageText;