Browse Source

Updated to use Jupiter::ReadableString EXCEPT for functions involving file loading -- there's no way to open a file without a c-style string.

release/0.19
JustinAJ 11 years ago
parent
commit
991ab0ea28
  1. 103
      Jupiter/File.cpp
  2. 23
      Jupiter/File.h
  3. 257
      Jupiter/INIFile.cpp
  4. 110
      Jupiter/INIFile.h

103
Jupiter/File.cpp

@ -8,11 +8,14 @@
#include <sys/stat.h>
#include "File.h"
#include "CString.h"
#include "String.h"
#include "Reference_String.h"
#include "ArrayList.h"
/**
* TODO:
* Upon writing Jupiter::String, port Jupiter::File to use Jupiter::String isntead of Jupiter::CString.
* Instead of an array of String, consider an ArrayList to remove the cost of constant construction/destruction when expanding.
* Consider replacing the ArrayList of Strings with a Rope (requires a Rope implementation).
*/
#if defined _WIN32
@ -29,12 +32,12 @@ int64_t getFileSize(const char *file)
const size_t defaultBufferSize = 8192;
template class JUPITER_API Jupiter::CString_Type<char>;
template class JUPITER_API Jupiter::ArrayList<Jupiter::StringS>;
struct JUPITER_API Jupiter::File::Data
{
Jupiter::CStringS fileName;
size_t lineCount;
Jupiter::CStringS *lines;
Jupiter::ArrayList<Jupiter::StringS> lines;
Data();
Data(const Data &data);
@ -43,24 +46,19 @@ struct JUPITER_API Jupiter::File::Data
Jupiter::File::Data::Data()
{
Jupiter::File::Data::lineCount = 0;
Jupiter::File::Data::lines = nullptr;
}
Jupiter::File::Data::Data(const Jupiter::File::Data &data)
{
Jupiter::File::Data::fileName = data.fileName;
if ((Jupiter::File::Data::lineCount = data.lineCount) != 0)
{
Jupiter::File::Data::lines = new Jupiter::CStringS[Jupiter::File::Data::lineCount];
for (unsigned int i = 0; i != Jupiter::File::Data::lineCount; i++) Jupiter::File::Data::lines[i] = data.lines[i];
}
else Jupiter::File::Data::lines = nullptr;
for (size_t i = 0; i != Jupiter::File::Data::lines.size(); i++)
Jupiter::File::Data::lines.add(new Jupiter::StringS(*data.lines.get(i)));
}
Jupiter::File::Data::~Data()
{
if (Jupiter::File::Data::lines != nullptr) delete[] Jupiter::File::Data::lines;
Jupiter::File::Data::lines.emptyAndDelete();
}
Jupiter::File::File()
@ -68,6 +66,12 @@ Jupiter::File::File()
Jupiter::File::data_ = new Jupiter::File::Data();
}
Jupiter::File::File(File &&file)
{
Jupiter::File::data_ = file.data_;
file.data_ = new Jupiter::File::Data();
}
Jupiter::File::File(const File &file)
{
Jupiter::File::data_ = new Jupiter::File::Data(*file.data_);
@ -78,57 +82,28 @@ Jupiter::File::~File()
delete Jupiter::File::data_;
}
unsigned int Jupiter::File::getLineCount() const
size_t Jupiter::File::getLineCount() const
{
return Jupiter::File::data_->lineCount;
return Jupiter::File::data_->lines.size();
}
const Jupiter::StringType &Jupiter::File::getLine(unsigned int line) const
const Jupiter::ReadableString &Jupiter::File::getLine(size_t line) const
{
return Jupiter::File::data_->lines[line];
return *Jupiter::File::data_->lines.get(line);
}
const Jupiter::StringType &Jupiter::File::getFileName() const
const Jupiter::ReadableString &Jupiter::File::getFileName() const
{
return Jupiter::File::data_->fileName;
}
bool Jupiter::File::addData(const Jupiter::StringType &data)
bool Jupiter::File::addData(const Jupiter::ReadableString &data)
{
if (Jupiter::File::data_->lineCount == 0)
{
if ((Jupiter::File::data_->lineCount = data.wordCount(ENDL)) != 0)
{
Jupiter::File::data_->lines = new Jupiter::CStringS[Jupiter::File::data_->lineCount];
for (unsigned int i = Jupiter::File::data_->lineCount - 1; i != 0; i--) Jupiter::File::data_->lines[i] = Jupiter::CStringS::getWord(data, i, ENDL);
Jupiter::File::data_->lines[0] = Jupiter::CStringS::getWord(data, 0, ENDL);
return true;
}
return false;
}
else
{
unsigned int wc = data.wordCount(ENDL);
if (wc != 0)
{
Jupiter::CStringS *oldLines = Jupiter::File::data_->lines;
Jupiter::File::data_->lines = new Jupiter::CStringS[Jupiter::File::data_->lineCount + wc];
unsigned int i;
for (i = 0; i < Jupiter::File::data_->lineCount; i++) Jupiter::File::data_->lines[i] = oldLines[i];
delete[] oldLines;
Jupiter::File::data_->lineCount += wc;
for (unsigned int b = 0; b < wc; i++, b++) Jupiter::File::data_->lines[i] = Jupiter::CStringS::getWord(data, b, ENDL);
return true;
}
return false;
}
}
unsigned int wc = data.wordCount(ENDL);
if (wc == 0) return false;
bool Jupiter::File::addData(const char *data)
{
return Jupiter::File::addData(Jupiter::CStringS(data));
for (unsigned int i = 0; i < wc; i++) Jupiter::File::data_->lines.add(new Jupiter::StringS(std::move(Jupiter::StringS::getWord(data, 0, ENDL))));
return true;
}
bool Jupiter::File::load(const char *file)
@ -143,27 +118,25 @@ bool Jupiter::File::load(const char *file)
bool Jupiter::File::load(FILE *file)
{
Jupiter::CStringL fileBuffer;
bool success = false;
char buffer[defaultBufferSize];
while (fgets(buffer, defaultBufferSize, file) != nullptr)
{
fileBuffer += buffer;
if (feof(file)) break;
}
return Jupiter::File::addData(fileBuffer);
while (!feof(file))
success |= Jupiter::File::addData(Jupiter::ReferenceString(buffer, fread(buffer, sizeof(char), sizeof(buffer), file)));
return success;
}
void Jupiter::File::unload()
{
delete Jupiter::File::data_;
Jupiter::File::data_ = new Jupiter::File::Data();
Jupiter::File::data_->fileName.set("");
Jupiter::File::data_->lines.emptyAndDelete();
}
bool Jupiter::File::reload()
{
if (Jupiter::File::data_->fileName.size() == 0) return false;
Jupiter::CStringS fileName = Jupiter::File::data_->fileName;
Jupiter::CStringS fileName(std::move(Jupiter::File::data_->fileName));
Jupiter::File::unload();
return Jupiter::File::load(fileName.c_str());
}
@ -197,9 +170,7 @@ bool Jupiter::File::sync(const char *file)
bool Jupiter::File::sync(FILE *file)
{
for (unsigned int i = 0; i < Jupiter::File::data_->lineCount; i++)
{
Jupiter::File::data_->lines[i].println(file);
}
for (size_t i = 0; i != Jupiter::File::data_->lines.size(); i++)
Jupiter::File::data_->lines.get(i)->println(file);
return true;
}

23
Jupiter/File.h

@ -24,7 +24,7 @@
*/
#include "Jupiter.h"
#include "CString.h"
#include "Readable_String.h"
#include <cstdio>
namespace Jupiter
@ -39,7 +39,7 @@ namespace Jupiter
*
* @return Total number of lines.
*/
unsigned int getLineCount() const;
size_t getLineCount() const;
/**
* @brief Fetches a line of the file.
@ -47,14 +47,14 @@ namespace Jupiter
* @param line Index of the line to fetch.
* @return Line of text at the specified index.
*/
const Jupiter::StringType &getLine(unsigned int line) const;
const Jupiter::ReadableString &getLine(size_t line) const;
/**
* @brief Returns the name of the first raw file originally loaded.
*
* @return String containing the name of the first file loaded into this file.
*/
const Jupiter::StringType &getFileName() const;
const Jupiter::ReadableString &getFileName() const;
/**
* @brief Adds data to a file, which may consist of one or more lines.
@ -62,15 +62,7 @@ namespace Jupiter
* @param data Data to add to the file.
* @param True if data was added to the file, false otherwise.
*/
bool addData(const Jupiter::StringType &data);
/**
* @brief Adds data to a file, which may consist of one or more lines.
*
* @param data Data to add to the file.
* @param True if data was added to the file, false otherwise.
*/
bool addData(const char *data);
bool addData(const Jupiter::ReadableString &data);
/**
* @brief Loads a file from the drive into this file.
@ -144,6 +136,11 @@ namespace Jupiter
*/
File();
/**
* @brief Move constructor for File class.
*/
File(File &&file);
/**
* @brief Copy constructor for File class.
*/

257
Jupiter/INIFile.cpp

@ -10,10 +10,12 @@
#include <cstdlib>
#include <cstdio>
#include "Functions.h"
#include "File.h"
#include "INIFile.h"
#include "CString.h"
#include "ArrayList.h"
#include "InvalidIndex.h"
#include "Reference_String.h"
/**
* TODO:
@ -47,22 +49,17 @@ Jupiter::INIFile::Section::KeyValuePair::~KeyValuePair()
delete Jupiter::INIFile::Section::KeyValuePair::data_;
}
const Jupiter::StringType &Jupiter::INIFile::Section::KeyValuePair::getKey() const
const Jupiter::ReadableString &Jupiter::INIFile::Section::KeyValuePair::getKey() const
{
return Jupiter::INIFile::Section::KeyValuePair::data_->key;
}
const Jupiter::StringType &Jupiter::INIFile::Section::KeyValuePair::getValue() const
const Jupiter::ReadableString &Jupiter::INIFile::Section::KeyValuePair::getValue() const
{
return Jupiter::INIFile::Section::KeyValuePair::data_->value;
}
void Jupiter::INIFile::Section::KeyValuePair::setValue(const Jupiter::StringType &value)
{
Jupiter::INIFile::Section::KeyValuePair::data_->value = value;
}
void Jupiter::INIFile::Section::KeyValuePair::setValue(const char *value)
void Jupiter::INIFile::Section::KeyValuePair::setValue(const Jupiter::ReadableString &value)
{
Jupiter::INIFile::Section::KeyValuePair::data_->value = value;
}
@ -86,13 +83,7 @@ Jupiter::INIFile::Section::Section()
Jupiter::INIFile::Section::data_ = new Jupiter::INIFile::Section::Data();
}
Jupiter::INIFile::Section::Section(const Jupiter::StringType &name)
{
Jupiter::INIFile::Section::data_ = new Jupiter::INIFile::Section::Data();
Jupiter::INIFile::Section::data_->name = name;
}
Jupiter::INIFile::Section::Section(const char *name)
Jupiter::INIFile::Section::Section(const Jupiter::ReadableString &name)
{
Jupiter::INIFile::Section::data_ = new Jupiter::INIFile::Section::Data();
Jupiter::INIFile::Section::data_->name = name;
@ -114,29 +105,17 @@ Jupiter::INIFile::Section::~Section()
delete Jupiter::INIFile::Section::data_;
}
const Jupiter::StringType &Jupiter::INIFile::Section::getName() const
const Jupiter::ReadableString &Jupiter::INIFile::Section::getName() const
{
return Jupiter::INIFile::Section::data_->name;
}
const Jupiter::StringType &Jupiter::INIFile::Section::getValue(unsigned int index) const
const Jupiter::ReadableString &Jupiter::INIFile::Section::getValue(unsigned int index) const
{
return Jupiter::INIFile::Section::data_->data.get(index)->data_->value;
}
const Jupiter::StringType &Jupiter::INIFile::Section::getValue(const Jupiter::StringType &key) const
{
unsigned int size = Jupiter::INIFile::Section::data_->data.size();
Jupiter::INIFile::Section::KeyValuePair *pair;
for (unsigned int i = 0; i < size; i++)
{
pair = Jupiter::INIFile::Section::data_->data.get(i);
if (pair->getKey().equalsi(key)) return pair->getValue();
}
return Jupiter::emptyString;
}
const Jupiter::StringType &Jupiter::INIFile::Section::getValue(const char *key) const
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;
@ -153,19 +132,7 @@ Jupiter::INIFile::Section::KeyValuePair *Jupiter::INIFile::Section::getPair(unsi
return Jupiter::INIFile::Section::data_->data.get(index);
}
Jupiter::INIFile::Section::KeyValuePair *Jupiter::INIFile::Section::getPair(const Jupiter::StringType &key) const
{
unsigned int size = Jupiter::INIFile::Section::data_->data.size();
Jupiter::INIFile::Section::KeyValuePair *pair;
for (unsigned int i = 0; i < size; i++)
{
pair = Jupiter::INIFile::Section::data_->data.get(i);
if (pair->getKey().equalsi(key)) return pair;
}
return nullptr;
}
Jupiter::INIFile::Section::KeyValuePair *Jupiter::INIFile::Section::getPair(const char *key) const
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;
@ -182,19 +149,7 @@ unsigned int Jupiter::INIFile::Section::size() const
return Jupiter::INIFile::Section::data_->data.size();
}
bool Jupiter::INIFile::Section::hasKey(const Jupiter::StringType &key) const
{
unsigned int size = Jupiter::INIFile::Section::data_->data.size();
Jupiter::INIFile::Section::KeyValuePair *pair;
for (unsigned int i = 0; i < size; i++)
{
pair = Jupiter::INIFile::Section::data_->data.get(i);
if (pair->getKey().equalsi(key)) return true;
}
return false;
}
bool Jupiter::INIFile::Section::hasKey(const char *key) const
bool Jupiter::INIFile::Section::hasKey(const Jupiter::ReadableString &key) const
{
unsigned int size = Jupiter::INIFile::Section::data_->data.size();
Jupiter::INIFile::Section::KeyValuePair *pair;
@ -206,7 +161,7 @@ bool Jupiter::INIFile::Section::hasKey(const char *key) const
return false;
}
bool Jupiter::INIFile::Section::set(Jupiter::StringType &key, Jupiter::StringType &value)
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;
@ -226,54 +181,14 @@ bool Jupiter::INIFile::Section::set(Jupiter::StringType &key, Jupiter::StringTyp
return true;
}
bool Jupiter::INIFile::Section::set(const char *key, const char *value)
{
unsigned int size = Jupiter::INIFile::Section::data_->data.size();
Jupiter::INIFile::Section::KeyValuePair *pair;
for (unsigned int i = 0; i < size; i++)
{
pair = Jupiter::INIFile::Section::data_->data.get(i);
if (pair->getKey().equalsi(key))
{
pair->data_->value = value;
return false;
}
}
pair = new Jupiter::INIFile::Section::KeyValuePair();
pair->data_->key = key;
pair->data_->value = value;
Jupiter::INIFile::Section::data_->data.add(pair);
return true;
}
void Jupiter::INIFile::Section::setName(const Jupiter::StringType &name)
{
Jupiter::INIFile::Section::data_->name = name;
}
void Jupiter::INIFile::Section::setName(const char *name)
void Jupiter::INIFile::Section::setName(const Jupiter::ReadableString &name)
{
Jupiter::INIFile::Section::data_->name = name;
}
bool Jupiter::INIFile::Section::remove(Jupiter::StringType &key)
bool Jupiter::INIFile::Section::remove(const Jupiter::ReadableString &key)
{
unsigned int size = Jupiter::INIFile::Section::data_->data.size();
for (unsigned int i = 0; i < size; i++)
{
if (Jupiter::INIFile::Section::data_->data.get(i)->getKey().equalsi(key))
{
delete Jupiter::INIFile::Section::data_->data.remove(i);
return true;
}
}
return false;
}
bool Jupiter::INIFile::Section::remove(const char *key)
{
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++)
{
if (Jupiter::INIFile::Section::data_->data.get(i)->getKey().equalsi(key))
{
@ -307,8 +222,7 @@ Jupiter::INIFile::INIFile(const INIFile &source)
{
Jupiter::INIFile::data_ = new Jupiter::INIFile::Data;
Jupiter::INIFile::data_->fName = source.data_->fName;
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++)
{
Jupiter::INIFile::data_->data.add(new Jupiter::INIFile::Section(*source.data_->data.get(i)));
}
@ -329,43 +243,45 @@ void Jupiter::INIFile::flushData()
unsigned int Jupiter::INIFile::readFile(const char *fileName)
{
unsigned int count = 0;
Jupiter::INIFile::data_->fName = fileName;
FILE *file = fopen(Jupiter::INIFile::data_->fName.c_str(), "rb");
if (file == nullptr) return Jupiter::ERROR_INDICATOR;
char fileBuffer[8192];
char *section;
//int nSection = 0;
while (!feof(file))
Jupiter::File file;
if (file.load(fileName) == false) return Jupiter::ERROR_INDICATOR;
Jupiter::INIFile::data_->fName = file.getFileName();
const char *ptr;
const char *ptrEnd;
Jupiter::ReferenceString section;
for (unsigned int index = 0; index != file.getLineCount(); index++)
{
if (fgets(fileBuffer, sizeof(fileBuffer), file))
const Jupiter::ReadableString &line = file.getLine(index);
// check if line is a comment.
ptr = line.ptr();
ptrEnd = ptr + line.size();
while (ptr != ptrEnd && isspace(*ptr)) ptr++;
if (ptr == ptrEnd) continue; // line is completely whitespace.
if (*ptr == '[') // changing sections.
{
section.set(Jupiter::ReferenceString::getWord(++ptr, 0, "]"));
}
else if (line.contains('=')) // key/value pair.
{
do1:
if (fileBuffer[strspn(fileBuffer, WHITESPACE)] != ';' && fileBuffer[strspn(fileBuffer, WHITESPACE)] == '[')
size_t eqlPos = line.find('=');
const char *endOfKey = line.ptr() + eqlPos;
while (endOfKey != ptr)
{
section = charToChar(fileBuffer, strspn(fileBuffer, WHITESPACE) + 1, strcspn(fileBuffer, "]"));
do2:
if (fgets(fileBuffer, sizeof(fileBuffer), file))
{
size_t eqlpos = strcspn(fileBuffer, "=");
if (fileBuffer[strspn(fileBuffer, WHITESPACE)] != ';' && eqlpos != strlen(fileBuffer))
{
size_t a = strspn(fileBuffer, WHITESPACE);
char *tkey = charToChar(fileBuffer, a, a + strcspn(fileBuffer + a, WHITESPACE "="));
char *tval = charToChar(fileBuffer, eqlpos + 1 + strspn(fileBuffer + eqlpos + 1, WHITESPACE), strcspn(fileBuffer, ENDL));
if (Jupiter::INIFile::set(section, tkey, tval)) count++;
delete[] tval;
delete[] tkey;
}
}
if (!feof(file))
{
if (fileBuffer[strspn(fileBuffer, WHITESPACE)] != '[') goto do2;
else goto do1;
}
ptr--;
if (!isspace(*endOfKey)) break;
}
const char *startOfValue = line.ptr() + eqlPos + 1;
while (startOfValue != ptrEnd && isspace(*startOfValue)) startOfValue++;
Jupiter::INIFile::set(section, Jupiter::ReferenceString(ptr, endOfKey - ptr), Jupiter::ReferenceString(startOfValue, ptrEnd - startOfValue));
}
}
fclose(file);
return count;
}
@ -415,7 +331,7 @@ bool Jupiter::INIFile::sync()
return Jupiter::INIFile::sync(Jupiter::INIFile::data_->fName.c_str());
}
bool Jupiter::INIFile::set(const char *section, const char *key, const char *value)
bool Jupiter::INIFile::set(const Jupiter::ReadableString &section, const Jupiter::ReadableString &key, const Jupiter::ReadableString &value)
{
unsigned int i = 0;
unsigned int size = Jupiter::INIFile::data_->data.size();
@ -436,7 +352,7 @@ bool Jupiter::INIFile::set(const char *section, const char *key, const char *val
return true;
}
bool Jupiter::INIFile::remove(const char *section, const char *key)
bool Jupiter::INIFile::remove(const Jupiter::ReadableString &section, const Jupiter::ReadableString &key)
{
unsigned int size = Jupiter::INIFile::data_->data.size();
Jupiter::INIFile::Section *sec;
@ -459,7 +375,7 @@ Jupiter::INIFile::Section *Jupiter::INIFile::getSection(unsigned int index) cons
return Jupiter::INIFile::data_->data.get(index);
}
unsigned int Jupiter::INIFile::getSectionIndex(const char *section) const
unsigned int Jupiter::INIFile::getSectionIndex(const Jupiter::ReadableString &section) const
{
unsigned int size = Jupiter::INIFile::data_->data.size();
for (unsigned int i = 0; i < size; i++)
@ -474,14 +390,14 @@ unsigned int Jupiter::INIFile::getSectionLength(unsigned int index) const
return Jupiter::INIFile::data_->data.get(index)->size();
}
const char *Jupiter::INIFile::getKey(const char *section, unsigned int index) const
const Jupiter::ReadableString &Jupiter::INIFile::getKey(const Jupiter::ReadableString &section, unsigned int index) const
{
unsigned int sectionIndex = Jupiter::INIFile::getSectionIndex(section);
if (sectionIndex == Jupiter::INVALID_INDEX) return nullptr;
return Jupiter::INIFile::data_->data.get(sectionIndex)->getPair(index)->getKey().c_str();
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 char *section, const char *key) const
unsigned int Jupiter::INIFile::getKeyIndex(const Jupiter::ReadableString &section, const Jupiter::ReadableString &key) const
{
unsigned int sectionIndex = Jupiter::INIFile::getSectionIndex(section);
if (sectionIndex == Jupiter::INVALID_INDEX) return Jupiter::INVALID_INDEX;
@ -491,7 +407,7 @@ unsigned int Jupiter::INIFile::getKeyIndex(const char *section, const char *key)
return Jupiter::INVALID_INDEX;
}
const char *Jupiter::INIFile::get(const char *section, const char *key, const char *defaultValue) const
const Jupiter::ReadableString &Jupiter::INIFile::get(const Jupiter::ReadableString &section, const Jupiter::ReadableString &key, const Jupiter::ReadableString &defaultValue) const
{
unsigned int size = Jupiter::INIFile::data_->data.size();
Jupiter::INIFile::Section *sect;
@ -503,75 +419,62 @@ const char *Jupiter::INIFile::get(const char *section, const char *key, const ch
{
Jupiter::INIFile::Section::KeyValuePair *pair = sect->getPair(key);
if (pair == nullptr) return defaultValue;
return pair->getValue().c_str();
return pair->getValue();
}
}
return defaultValue;
}
bool Jupiter::INIFile::getBool(const char *section, const char *key, bool defaultValue) const
bool Jupiter::INIFile::getBool(const Jupiter::ReadableString &section, const Jupiter::ReadableString &key, bool defaultValue) const
{
const char *val = Jupiter::INIFile::get(section, key);
if (val != nullptr)
{
if (streqli(val, "FALSE")) return false;
if (streql(val, "0")) return false;
if (streqli(val, "OFF")) return false;
return true;
}
const Jupiter::ReadableString &val = Jupiter::INIFile::get(section, key);
if (val.size() != 0) return val.asBool();
return defaultValue;
}
/*char Jupiter::INIFile::getChar(const char *section, const char *key) const
{
const char *val = Jupiter::INIFile::get(section,key);
if (val != nullptr) return val[0];
return 0;
}*/
short Jupiter::INIFile::getShort(const char *section, const char *key, short defaultValue) const
short Jupiter::INIFile::getShort(const Jupiter::ReadableString &section, const Jupiter::ReadableString &key, short defaultValue) const
{
return (short)Jupiter::INIFile::getInt(section, key, defaultValue);
}
int Jupiter::INIFile::getInt(const char *section, const char *key, int defaultValue) const
int Jupiter::INIFile::getInt(const Jupiter::ReadableString &section, const Jupiter::ReadableString &key, int defaultValue) const
{
const char *val = Jupiter::INIFile::get(section, key);
if (val != nullptr) return atoi(val);
const Jupiter::ReadableString &val = Jupiter::INIFile::get(section, key);
if (val.size() != 0) return val.asInt();
return defaultValue;
}
long Jupiter::INIFile::getLong(const char *section, const char *key, long defaultValue) const
long Jupiter::INIFile::getLong(const Jupiter::ReadableString &section, const Jupiter::ReadableString &key, long defaultValue) const
{
const char *val = Jupiter::INIFile::get(section, key);
if (val != nullptr) return strtol(val, nullptr, 0);
const Jupiter::ReadableString &val = Jupiter::INIFile::get(section, key);
if (val.size() != 0) return val.asInt();
return defaultValue;
}
long long Jupiter::INIFile::getLongLong(const char *section, const char *key, long long defaultValue) const
long long Jupiter::INIFile::getLongLong(const Jupiter::ReadableString &section, const Jupiter::ReadableString &key, long long defaultValue) const
{
const char *val = Jupiter::INIFile::get(section, key);
if (val != nullptr) return strtoll(val, nullptr, 0);
const Jupiter::ReadableString &val = Jupiter::INIFile::get(section, key);
if (val.size() != 0) return val.asInt();
return defaultValue;
}
float Jupiter::INIFile::getFloat(const char *section, const char *key, float defaultValue) const
float Jupiter::INIFile::getFloat(const Jupiter::ReadableString &section, const Jupiter::ReadableString &key, float defaultValue) const
{
const char *val = Jupiter::INIFile::get(section, key);
if (val != nullptr) return strtof(val, nullptr);
const Jupiter::ReadableString &val = Jupiter::INIFile::get(section, key);
if (val.size() != 0) return float(val.asDouble());
return defaultValue;
}
double Jupiter::INIFile::getDouble(const char *section, const char *key, double defaultValue) const
double Jupiter::INIFile::getDouble(const Jupiter::ReadableString &section, const Jupiter::ReadableString &key, double defaultValue) const
{
const char *val = Jupiter::INIFile::get(section, key);
if (val != nullptr) return strtod(val, nullptr);
const Jupiter::ReadableString &val = Jupiter::INIFile::get(section, key);
if (val.size() != 0) return val.asDouble();
return defaultValue;
}
long double Jupiter::INIFile::getLongDouble(const char *section, const char *key, long double defaultValue) const
long double Jupiter::INIFile::getLongDouble(const Jupiter::ReadableString &section, const Jupiter::ReadableString &key, long double defaultValue) const
{
const char *val = Jupiter::INIFile::get(section, key);
if (val != nullptr) return strtold(val, nullptr);
const Jupiter::ReadableString &val = Jupiter::INIFile::get(section, key);
if (val.size() != 0) return val.asDouble();
return defaultValue;
}

110
Jupiter/INIFile.h

@ -24,7 +24,7 @@
*/
#include "Jupiter.h"
#include "String_Type.h"
#include "Reference_String.h"
#include "InvalidIndex.h"
namespace Jupiter
@ -55,28 +55,21 @@ namespace Jupiter
*
* @return Reference to the key String in the pair.
*/
const Jupiter::StringType &getKey() const;
const Jupiter::ReadableString &getKey() const;
/**
* @brief Fetches the value in the key-value pair.
*
* @return Reference to the value String in the pair.
*/
const Jupiter::StringType &getValue() const;
const Jupiter::ReadableString &getValue() const;
/**
* @brief Sets the value of a key-value pair.
*
* @param value Value to set the key-value pair to.
*/
void setValue(const Jupiter::StringType &value);
/**
* @brief Sets the value of a key-value pair.
*
* @param value Value to set the key-value pair to.
*/
void setValue(const char *value);
void setValue(const Jupiter::ReadableString &value);
/**
* @brief Default constructor for the KeyValuePair class.
@ -103,7 +96,7 @@ namespace Jupiter
*
* @return Name of the section in a String.
*/
const Jupiter::StringType &getName() const;
const Jupiter::ReadableString &getName() const;
/**
* @brief Fetches the value of a key-value pair at a specified index.
@ -111,15 +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::StringType &getValue(unsigned int index) const;
/**
* @brief Fetches the value of a key-value pair.
*
* @param key Key of the key-value pair.
* @return Value of a key-value pair, or an empty string if none is found.
*/
const Jupiter::StringType &getValue(const Jupiter::StringType &key) const;
const Jupiter::ReadableString &getValue(unsigned int index) const;
/**
* @brief Fetches the value of a key-value pair.
@ -127,7 +112,7 @@ namespace Jupiter
* @param key Key of the key-value pair.
* @return Value of a key-value pair, or an empty string if none is found.
*/
const Jupiter::StringType &getValue(const char *key) const;
const Jupiter::ReadableString &getValue(const Jupiter::ReadableString &key) const;
/**
* @brief Fetches a key-value pair at a specified index.
@ -143,15 +128,7 @@ namespace Jupiter
* @param key Key of the key-value pair.
* @return A key-value pair if a match is found, nullptr otherwise.
*/
Jupiter::INIFile::Section::KeyValuePair *getPair(const Jupiter::StringType &key) const;
/**
* @brief Fetches a key-value pair.
*
* @param key Key of the key-value pair.
* @return A key-value pair if a match is found, nullptr otherwise.
*/
Jupiter::INIFile::Section::KeyValuePair *getPair(const char *key) const;
Jupiter::INIFile::Section::KeyValuePair *getPair(const Jupiter::ReadableString &key) const;
/**
* @brief Fetches the number of key-value pairs in the section.
@ -166,24 +143,7 @@ namespace Jupiter
* @param key Key of the key-value pair to search for.
* @return True if a match is found, false otherwise.
*/
bool hasKey(const Jupiter::StringType &key) const;
/**
* @brief Checks if a section has a key-value pair with a specified key.
*
* @param key Key of the key-value pair to search for.
* @return True if a match is found, false otherwise.
*/
bool hasKey(const char *key) const;
/**
* @brief Sets a key-value pair's value to a specified value. Creates a new pair if none is found.
*
* @param key Key of the key-value pair to search for or create.
* @param value Value to set the key-value pair to.
* @return True if a new key-value pair was created, false otherwise.
*/
bool set(Jupiter::StringType &key, Jupiter::StringType &value);
bool hasKey(const Jupiter::ReadableString &key) const;
/**
* @brief Sets a key-value pair's value to a specified value. Creates a new pair if none is found.
@ -192,29 +152,14 @@ namespace Jupiter
* @param value Value to set the key-value pair to.
* @return True if a new key-value pair was created, false otherwise.
*/
bool set(const char *key, const char *value);
/**
* @brief Sets the name of a section.
*
* @param name String to set the section name to.
*/
void setName(const Jupiter::StringType &name);
bool set(const Jupiter::ReadableString &key, const Jupiter::ReadableString &value);
/**
* @brief Sets the name of a section.
*
* @param name String to set the section name to.
*/
void setName(const char *name);
/**
* @brief Removes a key-value pair from the section.
*
* @param key Key of the key-value pair to remove.
* @return True if a key-value pair was removed, false otherwise.
*/
bool remove(Jupiter::StringType &key);
void setName(const Jupiter::ReadableString &name);
/**
* @brief Removes a key-value pair from the section.
@ -222,7 +167,7 @@ namespace Jupiter
* @param key Key of the key-value pair to remove.
* @return True if a key-value pair was removed, false otherwise.
*/
bool remove(const char *key);
bool remove(const Jupiter::ReadableString &key);
/**
* @brief Default constructor for the Section class.
@ -232,8 +177,7 @@ namespace Jupiter
/**
* @brief Name-specifiable constructors for the Section class
*/
Section(const Jupiter::StringType &name);
Section(const char *name);
Section(const Jupiter::ReadableString &name);
/**
* @brief Copy constructor for the Section class.
@ -299,7 +243,7 @@ namespace Jupiter
* @param keyValue String containing new key value.
* @return True if a new key was added, false otherwise.
*/
bool set(const char *section, const char *keyName, const char *keyValue);
bool set(const Jupiter::ReadableString &section, const Jupiter::ReadableString &keyName, const Jupiter::ReadableString &keyValue);
/**
* @brief Removes a key. Does not modify any files.
@ -308,7 +252,7 @@ namespace Jupiter
* @param keyName String containing key name.
* @return True if an entry was removed, false otherwise.
*/
bool remove(const char *section, const char *keyName);
bool remove(const Jupiter::ReadableString &section, const Jupiter::ReadableString &keyName);
/**
* @brief Returns the number of sections in memory.
@ -331,7 +275,7 @@ 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 char *section) const;
unsigned int getSectionIndex(const Jupiter::ReadableString &section) const;
/**
* @brief Returns the number of keys in a section.
@ -347,7 +291,7 @@ namespace Jupiter
* @param keyIndex Index of the key.
* @return Key name on success, nullptr otherwise.
*/
const char *getKey(const char *section, unsigned int index) const;
const Jupiter::ReadableString &getKey(const Jupiter::ReadableString &section, unsigned int index) const;
/**
* @brief Returns the index of a key.
@ -356,7 +300,7 @@ namespace Jupiter
* @param key String containing key name.
* @return Index of key if it exists, INVALID_INDEX (-1) otherwise.
*/
unsigned int getKeyIndex(const char *section, const char *key) const;
unsigned int getKeyIndex(const Jupiter::ReadableString &section, const Jupiter::ReadableString &key) const;
/**
* @brief Returns the value of a key.
@ -366,7 +310,7 @@ namespace Jupiter
* @param defaultValue Value to return if none is found.
* @return Pointer to a string containing key value if it exists, defaultValue otherwise.
*/
const char *get(const char *section, const char *key, const char *defaultValue = nullptr) const;
const Jupiter::ReadableString &get(const Jupiter::ReadableString &section, const Jupiter::ReadableString &key, const Jupiter::ReadableString &defaultValue = Jupiter::ReferenceString::empty) const;
/**
* @brief Translates and returns the value of a key as a boolean.
@ -376,7 +320,7 @@ namespace Jupiter
* @param defaultValue Value to return if none is found.
* @return true if key exists and is not "false" or "0", defaultValue otherwise.
*/
bool getBool(const char *section, const char *key, bool defaultValue = false) const;
bool getBool(const Jupiter::ReadableString &section, const Jupiter::ReadableString &key, bool defaultValue = false) const;
/**
* @brief Translates and returns the value of a key as a short.
@ -386,7 +330,7 @@ namespace Jupiter
* @param defaultValue Value to return if none is found.
* @return short value of the key if it exits, defaultValue otherwise.
*/
short getShort(const char *section, const char *key, short defaultValue = 0) const;
short getShort(const Jupiter::ReadableString &section, const Jupiter::ReadableString &key, short defaultValue = 0) const;
/**
* @brief Translates and returns the value of a key as an int.
@ -396,7 +340,7 @@ namespace Jupiter
* @param defaultValue Value to return if none is found.
* @return int value of the key if it exits, defaultValue otherwise.
*/
int getInt(const char *section, const char *key, int defaultValue = 0) const;
int getInt(const Jupiter::ReadableString &section, const Jupiter::ReadableString &key, int defaultValue = 0) const;
/**
* @brief Translates and returns the value of a key as a long.
@ -406,7 +350,7 @@ namespace Jupiter
* @param defaultValue Value to return if none is found.
* @return long value of the key if it exits, defaultValue otherwise.
*/
long getLong(const char *section, const char *key, long defaultValue = 0) const;
long getLong(const Jupiter::ReadableString &section, const Jupiter::ReadableString &key, long defaultValue = 0) const;
/**
* @brief Translates and returns the value of a key as a long long.
@ -416,7 +360,7 @@ namespace Jupiter
* @param defaultValue Value to return if none is found.
* @return long long value of the key if it exits, defaultValue otherwise.
*/
long long getLongLong(const char *section, const char *key, long long defaultValue = 0) const;
long long getLongLong(const Jupiter::ReadableString &section, const Jupiter::ReadableString &key, long long defaultValue = 0) const;
/**
* @brief Translates and returns the value of a key as a float.
@ -426,7 +370,7 @@ namespace Jupiter
* @param defaultValue Value to return if none is found.
* @return float value of the key if it exits, defaultValue otherwise.
*/
float getFloat(const char *section, const char *key, float defaultValue = 0) const;
float getFloat(const Jupiter::ReadableString &section, const Jupiter::ReadableString &key, float defaultValue = 0) const;
/**
* @brief Translates and returns the value of a key as a double.
@ -436,7 +380,7 @@ namespace Jupiter
* @param defaultValue Value to return if none is found.
* @return double value of the key if it exits, defaultValue otherwise.
*/
double getDouble(const char *section, const char *key, double defaultValue = 0) const;
double getDouble(const Jupiter::ReadableString &section, const Jupiter::ReadableString &key, double defaultValue = 0) const;
/**
* @brief Translates and returns the value of a key as a long double.
@ -446,7 +390,7 @@ namespace Jupiter
* @param defaultValue Value to return if none is found.
* @return long double value of the key if it exits, defaultValue otherwise.
*/
long double getLongDouble(const char *section, const char *key, long double defaultValue = 0) const;
long double getLongDouble(const Jupiter::ReadableString &section, const Jupiter::ReadableString &key, long double defaultValue = 0) const;
/**
* @brief Default constructor for the INIFile class.

Loading…
Cancel
Save