Skip to content

Commit

Permalink
Move documentation comments to implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
jss2a98aj committed Dec 1, 2024
1 parent f832dee commit b554e1b
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 106 deletions.
8 changes: 8 additions & 0 deletions json_intermediary_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ JsonIntermediaryWriter::JsonIntermediaryWriter(){

}

/**
* Writes an intermediary file. Some configurations will not comply with official JSON standards.
*
* @param patchText The string stream the intermediary JSON will be written to.
* @param fileSettings The file extension specific settings to use when parsing.
* @param sourceJson The JSON things are parsed from.
* @return How many values the resulting intermediary JSON contains.
*/
int JsonIntermediaryWriter::writeIntermediaryFile(std::stringstream & intermediaryText, FileSettings & fileSettings, const json & sourceJson) {
totalIntermediaryValues = 0;

Expand Down
8 changes: 0 additions & 8 deletions json_intermediary_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,5 @@ class JsonIntermediaryWriter {
void writePointerValueStart(std::stringstream & intermediaryText, const PointerSettings & pointerSettings);
public:
JsonIntermediaryWriter();
/**
* Writes an intermediary file. Some configurations will not comply with official JSON standards.
*
* @param patchText The string stream the intermediary JSON will be written to.
* @param fileSettings The file extension specific settings to use when parsing.
* @param sourceJson The JSON things are parsed from.
* @return How many values the resulting intermediary JSON contains.
*/
int writeIntermediaryFile(std::stringstream & intermediaryText, FileSettings & fileSettings, const nlohmann::json & sourceJson);
};
9 changes: 9 additions & 0 deletions json_patch_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,15 @@ JsonPatchWriter::JsonPatchWriter(MasterSettings masterSettings) {
useOperationSets = masterSettings.getUseOperationSets();
}

/**
* Writes a patch file. Some configurations will not comply with official JSON patch standards.
*
* @param patchText The string stream the patches will be written to.
* @param fileSettings The file extension specific settings to use when making patches.
* @param sourceJson The JSON patches will assume is used as a base.
* @param intermediaryJson The JSON patches will try to make the base mimic when applied.
* @return How many values the resulting patch will add or replace.
*/
int JsonPatchWriter::writePatchFile(std::stringstream & patchText, FileSettings & fileSettings, const json & sourceJson, const json & intermediaryJson) {
indentModifier = baselinePatchStyle.getIndentationInOuterBrackets() ? 1 : 0;
currentOps = 0;
Expand Down
9 changes: 0 additions & 9 deletions json_patch_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,5 @@ class JsonPatchWriter {
void writeColon(std::stringstream & patchText);
public:
JsonPatchWriter(MasterSettings masterSettings);
/**
* Writes a patch file. Some configurations will not comply with official JSON patch standards.
*
* @param patchText The string stream the patches will be written to.
* @param fileSettings The file extension specific settings to use when making patches.
* @param sourceJson The JSON patches will assume is used as a base.
* @param intermediaryJson The JSON patches will try to make the base mimic when applied.
* @return How many values the resulting patch will add or replace.
*/
int writePatchFile(std::stringstream & patchText, FileSettings & fileSettings, const nlohmann::json & sourceJson, const nlohmann::json & intermediaryJson);
};
8 changes: 8 additions & 0 deletions parse_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,18 @@ FileSettings::FileSettings(fs::path settingsPath) {
}
}

/**
* Writes example settings to a string stream.
*
* @param settingsText The std::stringstream that will be written to.
*/
void FileSettings::writeExampleSettings(std::stringstream & settingsText) {
//TODO: Write example settings.
}

/**
* @return If there are any pointerSettings in allPointerSettings.
*/
const bool FileSettings::hasPointerSettings() {
return allPointerSettings.size() >= 1;
}
Expand Down
8 changes: 0 additions & 8 deletions parse_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,7 @@ class FileSettings {
std::vector<PointerSettings> allPointerSettings;
public:
FileSettings(std::filesystem::path settingsPath);
/**
* Writes example settings to a string stream.
*
* @param settingsText The std::stringstream that will be written to.
*/
void writeExampleSettings(std::stringstream & settingsText);
/**
* @return If there are any pointerSettings in allPointerSettings.
*/
const bool hasPointerSettings();
//Getters
const std::string getFileExtension();
Expand Down
15 changes: 15 additions & 0 deletions user_interaction_helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,20 @@

namespace fs = std::filesystem;

/**
* Clears the input stream.
*/
void clearInput() {
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

/**
* Prompts for a yes/no input from the terminal.
*
* @param message The message to display.
* @return If the user answered yes or no.
*/
bool requestBoolean(std::string message) {
do {
char input;
Expand All @@ -28,6 +37,12 @@ bool requestBoolean(std::string message) {
} while (true);
}

/**
* Checks if the file at the specified path exists and issues a warning if not.
*
* @param message The message to display.
* @return If a warning was issued.
*/
bool warnIfNothingAtPath(std::filesystem::path filePath, std::string fileDescription) {
if (!fs::exists(filePath)) {
std::cout << "The " << fileDescription << " folder is missing.\n"
Expand Down
15 changes: 0 additions & 15 deletions user_interaction_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,8 @@

#include <filesystem>

/**
* Clears the input stream.
*/
void clearInput();

/**
* Prompts for a yes/no input from the terminal.
*
* @param message The message to display.
* @return If the user answered yes or no.
*/
bool requestBoolean(std::string message);

/**
* Checks if the file at the specified path exists and issues a warning if not.
*
* @param message The message to display.
* @return If a warning was issued.
*/
bool warnIfNothingAtPath(std::filesystem::path filePath, std::string fileDescription);
76 changes: 65 additions & 11 deletions utilities.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ namespace fs = std::filesystem;

enum StripMode { structure, quote, commentSingle, commentMulti };

/**
* Strip JSON comments from strings.
*
* @param text The text to remove JSON comments from.
*/
void stripJsonComments(std::string & text) {
StripMode mode = structure;
int commentStart, commentLength;
Expand Down Expand Up @@ -55,6 +60,11 @@ void stripJsonComments(std::string & text) {
}
}

/**
* Converts newlines in JSON values to breakout newlines.
*
* @param text The text to convert newlines in.
*/
void convertJsonValueNewlinesToBreakout(std::string & text) {
StripMode mode = structure;
char prev = 'U', current = 'U';
Expand All @@ -79,17 +89,11 @@ void convertJsonValueNewlinesToBreakout(std::string & text) {
}
}

int replaceFirstOfX(std::string & text, const char x, const std::string replacement) {
for (int i = 0; i < text.size(); i++) {
if (text[i] == x) {
text.erase(i, 1);
text.insert(i, replacement);
return i;
}
}
return -1;
}

/**
* Converts breakout newlines in JSON values to newlines.
*
* @param text The text to convert breakout newlines in.
*/
void convertNewlineBreakoutsToNewline(std::string & text) {
char prev = 'U', current = 'U';
for (int i = 0; i < text.size(); i++) {
Expand All @@ -105,6 +109,11 @@ void convertNewlineBreakoutsToNewline(std::string & text) {
}
}

/**
* Converts quotes in JSON values to breakout quotes.
*
* @param text The text to convert quotes in.
*/
void convertQuoteToBreakoutQuote(std::string & text) {
char prev = 'U', current = 'U';
for (int i = 0; i < text.size(); i++) {
Expand All @@ -117,6 +126,31 @@ void convertQuoteToBreakoutQuote(std::string & text) {
}
}

/**
* Converts breakout newlines in values to newlines".
*
* @param text The text to have a character replaced in.
* @param x The character to replace.
* @param replacement The string to replace the character with.
* @return The position of the character replaced, -1 if none.
*/
int replaceFirstOfX(std::string & text, const char x, const std::string replacement) {
for (int i = 0; i < text.size(); i++) {
if (text[i] == x) {
text.erase(i, 1);
text.insert(i, replacement);
return i;
}
}
return -1;
}

/**
* Retrieves a Text file from the path.
*
* @param filePath The path the file is at.
* @return The file document as std::string.
*/
const std::string fetchText(fs::path filePath) {
std::ifstream textFile;
std::stringstream textStream;
Expand All @@ -129,6 +163,13 @@ const std::string fetchText(fs::path filePath) {
return textStream.str();
}

/**
* Retrieves a JSON file from the path.
*
* @param filePath The path the file is at.
* @param valuesHaveNewlines If values have actual newlines in them.
* @return The JSON file as nlohmann::json.
*/
const json fetchJson(fs::path filePath, bool valuesHaveNewlines) {
std::string jsonString = fetchText(filePath);
stripJsonComments(jsonString);
Expand All @@ -139,10 +180,23 @@ const json fetchJson(fs::path filePath, bool valuesHaveNewlines) {
return json::parse(jsonString);
}

/**
* Retrieves a JSON file from the path.
*
* @param filePath The path the file is at.
* @return The JSON file as nlohmann::json.
*/
const json fetchJson(fs::path filePath) {
return fetchJson(filePath, false);
}

/**
* Writes a string steam to a specific path..
*
* @param stream The string stream to write.
* @param filePath The path the file should be written to.
* @return If the file was written.
*/
const bool writeStringStreamToPath(std::stringstream & stream, std::filesystem::path filePath) {
if (fs::exists(filePath.parent_path()) || fs::create_directories(filePath.parent_path())) {
std::ofstream textFile;
Expand Down
55 changes: 0 additions & 55 deletions utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,75 +4,20 @@
#include <sstream>
#include <nlohmann/json.hpp>

/**
* Strip JSON comments from strings.
*
* @param text The text to remove JSON comments from.
*/
void stripJsonComments(std::string & text);

/**
* Converts newlines in JSON values to breakout newlines.
*
* @param text The text to convert newlines in.
*/
void convertJsonValueNewlinesToBreakout(std::string & text);

/**
* Converts breakout newlines in JSON values to newlines.
*
* @param text The text to convert breakout newlines in.
*/
void convertNewlineBreakoutsToNewline(std::string & text);


/**
* Converts quotes in JSON values to breakout quotes.
*
* @param text The text to convert quotes in.
*/
void convertQuoteToBreakoutQuote(std::string & text);

/**
* Converts breakout newlines in values to newlines".
*
* @param text The text to have a character replaced in.
* @param x The character to replace.
* @param replacement The string to replace the character with.
* @return The position of the character replaced, -1 if none.
*/
int replaceFirstOfX(std::string & text, const char x, const std::string replacement);

/**
* Retrieves a Text file from the path.
*
* @param filePath The path the file is at.
* @return The file document as std::string.
*/
const std::string fetchText(std::filesystem::path filePath);

/**
* Retrieves a JSON file from the path.
*
* @param filePath The path the file is at.
* @param valuesHaveNewlines If values have actual newlines in them.
* @return The JSON file as nlohmann::json.
*/
const nlohmann::json fetchJson(std::filesystem::path filePath, bool valuesHaveNewlines);

/**
* Retrieves a JSON file from the path.
*
* @param filePath The path the file is at.
* @return The JSON file as nlohmann::json.
*/
const nlohmann::json fetchJson(std::filesystem::path filePath);

/**
* Writes a string steam to a specific path..
*
* @param stream The string stream to write.
* @param filePath The path the file should be written to.
* @return If the file was written.
*/
const bool writeStringStreamToPath(std::stringstream & stream, std::filesystem::path filePath);

0 comments on commit b554e1b

Please sign in to comment.