From 7665f6112a8d4aed6894e0b9a42428ab10c8f21a Mon Sep 17 00:00:00 2001 From: JustinAJ Date: Thu, 5 Jun 2014 01:28:31 -0400 Subject: [PATCH] Changed many types to size_t and fixed several bugs. --- Jupiter/INIFile.cpp | 128 ++++++++++++++++++++------------------------ Jupiter/INIFile.h | 31 +++++------ 2 files changed, 72 insertions(+), 87 deletions(-) diff --git a/Jupiter/INIFile.cpp b/Jupiter/INIFile.cpp index 2853fae..f1f582c 100644 --- a/Jupiter/INIFile.cpp +++ b/Jupiter/INIFile.cpp @@ -93,8 +93,7 @@ Jupiter::INIFile::Section::Section(const Jupiter::INIFile::Section &source) { Jupiter::INIFile::Section::data_ = new Jupiter::INIFile::Section::Data(); Jupiter::INIFile::Section::data_->name = source.data_->name; - unsigned int size = Jupiter::INIFile::Section::data_->data.size(); - for (unsigned int i = 0; i < size; i++) + for (size_t i = 0; i != Jupiter::INIFile::Section::data_->data.size(); i++) { Jupiter::INIFile::Section::data_->data.add(new Jupiter::INIFile::Section::KeyValuePair(*source.data_->data.get(i))); } @@ -110,16 +109,15 @@ const Jupiter::ReadableString &Jupiter::INIFile::Section::getName() const return Jupiter::INIFile::Section::data_->name; } -const Jupiter::ReadableString &Jupiter::INIFile::Section::getValue(unsigned int index) const +const Jupiter::ReadableString &Jupiter::INIFile::Section::getValue(size_t index) const { return Jupiter::INIFile::Section::data_->data.get(index)->data_->value; } const Jupiter::ReadableString &Jupiter::INIFile::Section::getValue(const Jupiter::ReadableString &key) const { - unsigned int size = Jupiter::INIFile::Section::data_->data.size(); Jupiter::INIFile::Section::KeyValuePair *pair; - for (unsigned int i = 0; i < size; i++) + for (size_t i = 0; i != Jupiter::INIFile::Section::data_->data.size(); i++) { pair = Jupiter::INIFile::Section::data_->data.get(i); if (pair->getKey().equalsi(key)) return pair->getValue(); @@ -127,16 +125,15 @@ const Jupiter::ReadableString &Jupiter::INIFile::Section::getValue(const Jupiter return Jupiter::emptyString; } -Jupiter::INIFile::Section::KeyValuePair *Jupiter::INIFile::Section::getPair(unsigned int index) const +Jupiter::INIFile::Section::KeyValuePair *Jupiter::INIFile::Section::getPair(size_t index) const { return Jupiter::INIFile::Section::data_->data.get(index); } Jupiter::INIFile::Section::KeyValuePair *Jupiter::INIFile::Section::getPair(const Jupiter::ReadableString &key) const { - unsigned int size = Jupiter::INIFile::Section::data_->data.size(); Jupiter::INIFile::Section::KeyValuePair *pair; - for (unsigned int i = 0; i < size; i++) + for (size_t i = 0; i != Jupiter::INIFile::Section::data_->data.size(); i++) { pair = Jupiter::INIFile::Section::data_->data.get(i); if (pair->getKey().equalsi(key)) return pair; @@ -144,16 +141,15 @@ Jupiter::INIFile::Section::KeyValuePair *Jupiter::INIFile::Section::getPair(cons return nullptr; } -unsigned int Jupiter::INIFile::Section::size() const +size_t Jupiter::INIFile::Section::size() const { return Jupiter::INIFile::Section::data_->data.size(); } bool Jupiter::INIFile::Section::hasKey(const Jupiter::ReadableString &key) const { - unsigned int size = Jupiter::INIFile::Section::data_->data.size(); Jupiter::INIFile::Section::KeyValuePair *pair; - for (unsigned int i = 0; i < size; i++) + for (size_t i = 0; i != Jupiter::INIFile::Section::data_->data.size(); i++) { pair = Jupiter::INIFile::Section::data_->data.get(i); if (pair->getKey().equalsi(key)) return true; @@ -163,9 +159,8 @@ bool Jupiter::INIFile::Section::hasKey(const Jupiter::ReadableString &key) const bool Jupiter::INIFile::Section::set(const Jupiter::ReadableString &key, const Jupiter::ReadableString &value) { - unsigned int size = Jupiter::INIFile::Section::data_->data.size(); Jupiter::INIFile::Section::KeyValuePair *pair; - for (unsigned int i = 0; i < size; i++) + for (size_t i = 0; i != Jupiter::INIFile::Section::data_->data.size(); i++) { pair = Jupiter::INIFile::Section::data_->data.get(i); if (pair->getKey().equalsi(key)) @@ -188,7 +183,7 @@ void Jupiter::INIFile::Section::setName(const Jupiter::ReadableString &name) bool Jupiter::INIFile::Section::remove(const Jupiter::ReadableString &key) { - for (size_t i = 0; i < Jupiter::INIFile::Section::data_->data.size(); i++) + for (size_t i = 0; i != Jupiter::INIFile::Section::data_->data.size(); i++) { if (Jupiter::INIFile::Section::data_->data.get(i)->getKey().equalsi(key)) { @@ -222,7 +217,7 @@ Jupiter::INIFile::INIFile(const INIFile &source) { Jupiter::INIFile::data_ = new Jupiter::INIFile::Data; Jupiter::INIFile::data_->fName = source.data_->fName; - for (size_t i = 0; i < Jupiter::INIFile::data_->data.size(); i++) + for (size_t i = 0; i != Jupiter::INIFile::data_->data.size(); i++) { Jupiter::INIFile::data_->data.add(new Jupiter::INIFile::Section(*source.data_->data.get(i))); } @@ -242,47 +237,53 @@ void Jupiter::INIFile::flushData() // TODO: Rewrite this. unsigned int Jupiter::INIFile::readFile(const char *fileName) { - unsigned int count = 0; + size_t total = 0; Jupiter::File file; - if (file.load(fileName) == false) return Jupiter::ERROR_INDICATOR; - Jupiter::INIFile::data_->fName = file.getFileName(); + if (file.load(fileName) == false) + return Jupiter::ERROR_INDICATOR; - const char *ptr; - const char *ptrEnd; + Jupiter::INIFile::data_->fName = file.getFileName(); Jupiter::ReferenceString section; - for (unsigned int index = 0; index != file.getLineCount(); index++) + Jupiter::ReferenceString line; + for (size_t index = 0; index != file.getLineCount(); index++) { - const Jupiter::ReadableString &line = file.getLine(index); + line = file.getLine(index); // check if line is a comment. - ptr = line.ptr(); - ptrEnd = ptr + line.size(); - while (ptr != ptrEnd && isspace(*ptr)) ptr++; + while (line.size() != 0 && isspace(line.get(0))) + line.shiftRight(1); + + if (line.size() == 0) continue; // Completely whitespace. + if (line.get(0) == ';') continue; // Comment. - if (ptr == ptrEnd) continue; // line is completely whitespace. + while (isspace(line.get(line.size() - 1))) // This is safe due to the previous check, which confirms that there is a non-whitespace character. + line.truncate(1); - if (*ptr == '[') // changing sections. + if (line.get(0) == '[') // changing sections. { - section.set(Jupiter::ReferenceString::getWord(++ptr, 0, "]")); + line.shiftRight(1); + + while (line.size() != 0 && line.get(line.size() - 1) != ']') // This also inadvertently adds support for comments on section lines! + line.truncate(1); + + if (line.size() != 0) + section.set(line); } else if (line.contains('=')) // key/value pair. { - size_t eqlPos = line.find('='); - - const char *endOfKey = line.ptr() + eqlPos; - while (endOfKey != ptr) - { - ptr--; - if (!isspace(*endOfKey)) break; - } + Jupiter::ReferenceString key = line.getWord(0, "="); + while (key.size() != 0 && isspace(key.get(key.size() - 1))) + key.truncate(1); - const char *startOfValue = line.ptr() + eqlPos + 1; - while (startOfValue != ptrEnd && isspace(*startOfValue)) startOfValue++; + Jupiter::ReferenceString value = line.gotoWord(1, "="); + while (value.size() != 0 && isspace(value.get(0))) + value.shiftRight(1); - Jupiter::INIFile::set(section, Jupiter::ReferenceString(ptr, endOfKey - ptr), Jupiter::ReferenceString(startOfValue, ptrEnd - startOfValue)); + if (Jupiter::INIFile::set(section, key, value)) + total++; } } - return count; + return total; } unsigned int Jupiter::INIFile::reload() @@ -294,23 +295,20 @@ unsigned int Jupiter::INIFile::reload() bool Jupiter::INIFile::sync(const char *fileName) { - unsigned int size = Jupiter::INIFile::data_->data.size(); - unsigned int sectionSize; Jupiter::INIFile::Section *section; Jupiter::INIFile::Section::KeyValuePair *pair; FILE *file = fopen(fileName, "wb"); if (file == nullptr) return false; fputs("; This file was automatically generated by Agent's INI parser." ENDL "; The currently used data values are as follows." ENDL ENDL, file); - for (unsigned int a = 0; a < size; a++) + for (size_t a = 0; a != Jupiter::INIFile::data_->data.size(); a++) { section = Jupiter::INIFile::data_->data.get(a); fputc('[', file); section->getName().print(file); fputs("]" ENDL, file); - sectionSize = section->size(); - for (unsigned int b = 0; b < sectionSize; b++) + for (size_t b = 0; b != section->size(); b++) { pair = section->getPair(b); pair->getKey().print(file); @@ -333,18 +331,12 @@ bool Jupiter::INIFile::sync() bool Jupiter::INIFile::set(const Jupiter::ReadableString §ion, const Jupiter::ReadableString &key, const Jupiter::ReadableString &value) { - unsigned int i = 0; - unsigned int size = Jupiter::INIFile::data_->data.size(); Jupiter::INIFile::Section *sec; - while (i < size) + for (size_t i = 0; i != Jupiter::INIFile::data_->data.size(); i++) { sec = Jupiter::INIFile::data_->data.get(i); if (sec->getName().equalsi(section)) - { - sec->set(key, value); - return false; - } - i++; + return sec->set(key, value); } sec = new Jupiter::INIFile::Section(section); sec->set(key, value); @@ -354,9 +346,8 @@ bool Jupiter::INIFile::set(const Jupiter::ReadableString §ion, const Jupiter bool Jupiter::INIFile::remove(const Jupiter::ReadableString §ion, const Jupiter::ReadableString &key) { - unsigned int size = Jupiter::INIFile::data_->data.size(); Jupiter::INIFile::Section *sec; - for (unsigned int i = 0; i < size; i++) + for (size_t i = 0; i != Jupiter::INIFile::data_->data.size(); i++) { sec = Jupiter::INIFile::data_->data.get(i); if (sec->getName().equalsi(section)) return sec->remove(key); @@ -365,54 +356,51 @@ bool Jupiter::INIFile::remove(const Jupiter::ReadableString §ion, const Jupi return false; } -unsigned int Jupiter::INIFile::getSections() const +size_t Jupiter::INIFile::getSections() const { return Jupiter::INIFile::data_->data.size(); } -Jupiter::INIFile::Section *Jupiter::INIFile::getSection(unsigned int index) const +Jupiter::INIFile::Section *Jupiter::INIFile::getSection(size_t index) const { return Jupiter::INIFile::data_->data.get(index); } -unsigned int Jupiter::INIFile::getSectionIndex(const Jupiter::ReadableString §ion) const +size_t Jupiter::INIFile::getSectionIndex(const Jupiter::ReadableString §ion) const { - unsigned int size = Jupiter::INIFile::data_->data.size(); - for (unsigned int i = 0; i < size; i++) + for (size_t i = 0; i != Jupiter::INIFile::data_->data.size(); i++) { if (Jupiter::INIFile::data_->data.get(i)->getName().equalsi(section)) return i; } return Jupiter::INVALID_INDEX; } -unsigned int Jupiter::INIFile::getSectionLength(unsigned int index) const +size_t Jupiter::INIFile::getSectionLength(size_t index) const { return Jupiter::INIFile::data_->data.get(index)->size(); } -const Jupiter::ReadableString &Jupiter::INIFile::getKey(const Jupiter::ReadableString §ion, unsigned int index) const +const Jupiter::ReadableString &Jupiter::INIFile::getKey(const Jupiter::ReadableString §ion, size_t index) const { - unsigned int sectionIndex = Jupiter::INIFile::getSectionIndex(section); + size_t sectionIndex = Jupiter::INIFile::getSectionIndex(section); if (sectionIndex == Jupiter::INVALID_INDEX) return Jupiter::ReferenceString::empty; return Jupiter::INIFile::data_->data.get(sectionIndex)->getPair(index)->getKey(); } -unsigned int Jupiter::INIFile::getKeyIndex(const Jupiter::ReadableString §ion, const Jupiter::ReadableString &key) const +size_t Jupiter::INIFile::getKeyIndex(const Jupiter::ReadableString §ion, const Jupiter::ReadableString &key) const { - unsigned int sectionIndex = Jupiter::INIFile::getSectionIndex(section); + size_t sectionIndex = Jupiter::INIFile::getSectionIndex(section); if (sectionIndex == Jupiter::INVALID_INDEX) return Jupiter::INVALID_INDEX; Jupiter::INIFile::Section *sect = Jupiter::INIFile::data_->data.get(sectionIndex); - unsigned int sectionSize = sect->size(); - for (unsigned int i = 0; i < sectionSize; i++) if (sect->getPair(i)->getKey().equalsi(key)) return i; + size_t sectionSize = sect->size(); + for (size_t i = 0; i != sectionSize; i++) if (sect->getPair(i)->getKey().equalsi(key)) return i; return Jupiter::INVALID_INDEX; } const Jupiter::ReadableString &Jupiter::INIFile::get(const Jupiter::ReadableString §ion, const Jupiter::ReadableString &key, const Jupiter::ReadableString &defaultValue) const { - unsigned int size = Jupiter::INIFile::data_->data.size(); Jupiter::INIFile::Section *sect; - - for (unsigned int i = 0; i < size; i++) + for (size_t i = 0; i != Jupiter::INIFile::data_->data.size(); i++) { sect = Jupiter::INIFile::data_->data.get(i); if (sect->getName().equalsi(section)) diff --git a/Jupiter/INIFile.h b/Jupiter/INIFile.h index e2fe0a2..5f7d7cb 100644 --- a/Jupiter/INIFile.h +++ b/Jupiter/INIFile.h @@ -104,7 +104,7 @@ namespace Jupiter * @param index Index of the key-value pair. * @return Value of a key-value pair at a specified index. */ - const Jupiter::ReadableString &getValue(unsigned int index) const; + const Jupiter::ReadableString &getValue(size_t index) const; /** * @brief Fetches the value of a key-value pair. @@ -120,7 +120,7 @@ namespace Jupiter * @param index Index of the key-value pair to fetch. * @return The key-value pair at the specified index. */ - Jupiter::INIFile::Section::KeyValuePair *getPair(unsigned int index) const; + Jupiter::INIFile::Section::KeyValuePair *getPair(size_t index) const; /** * @brief Fetches a key-value pair. @@ -135,7 +135,7 @@ namespace Jupiter * * @return Number of key-value pairs in the section. */ - unsigned int size() const; + size_t size() const; /** * @brief Checks if a section has a key-value pair with a specified key. @@ -201,22 +201,19 @@ namespace Jupiter /** * @brief Reads data from a file. - * The readFile function does not currently support keys which - * are not under a section, though this is not currently an issue, - * and may still be added later. The parser will also automatically - * remove any excessive whitespace, including any whitespace prefixing - * a key name, prefixing a key value, trailing a key name, or traling - * a key value. + * The parser will automatically remove any excessive whitespace, including any whitespace + * prefixing a key name, prefixing a key value, trailing a key name, or trailing a key value. + * Whitespace is defined by locale. Please refer to C's isspace() documentation for details. * * @param fileName String containing the name of a file. - * @return The number of entries added on success, ERROR_INDICATOR (-1) otherwise. + * @return The number of key-value pairs added on success, ERROR_INDICATOR (-1) otherwise. */ unsigned int readFile(const char *fileName); /** * @brief Flushes all stored data and recollects from the last read file. * - * @return The number of entries added on success, ERROR_INDICATOR (-1) otherwise. + * @return The number of key-value pairs added on success, ERROR_INDICATOR (-1) otherwise. */ unsigned int reload(); @@ -259,7 +256,7 @@ namespace Jupiter * * @return Number of sections. */ - unsigned int getSections() const; + size_t getSections() const; /** * @brief Returns a section. @@ -267,7 +264,7 @@ namespace Jupiter * @param index Index of the section to get. * @return String containing section if it exists, nullptr otherwise. */ - Jupiter::INIFile::Section *getSection(unsigned int index) const; + Jupiter::INIFile::Section *getSection(size_t index) const; /** * @brief Returns the index of a section. @@ -275,14 +272,14 @@ namespace Jupiter * @param section The name of the section. * @return The index of section if it exists, INVALID_INDEX (-1) otherwise. */ - unsigned int getSectionIndex(const Jupiter::ReadableString §ion) const; + size_t getSectionIndex(const Jupiter::ReadableString §ion) const; /** * @brief Returns the number of keys in a section. * * @return Number of data entries in a section. */ - unsigned int getSectionLength(unsigned int index) const; + size_t getSectionLength(size_t index) const; /** * @brief Returns a key name. @@ -291,7 +288,7 @@ namespace Jupiter * @param keyIndex Index of the key. * @return Key name on success, nullptr otherwise. */ - const Jupiter::ReadableString &getKey(const Jupiter::ReadableString §ion, unsigned int index) const; + const Jupiter::ReadableString &getKey(const Jupiter::ReadableString §ion, size_t index) const; /** * @brief Returns the index of a key. @@ -300,7 +297,7 @@ namespace Jupiter * @param key String containing key name. * @return Index of key if it exists, INVALID_INDEX (-1) otherwise. */ - unsigned int getKeyIndex(const Jupiter::ReadableString §ion, const Jupiter::ReadableString &key) const; + size_t getKeyIndex(const Jupiter::ReadableString §ion, const Jupiter::ReadableString &key) const; /** * @brief Returns the value of a key.