mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-25 01:41:43 +00:00
Major formatting changes (breaks diffs). No code changes.
The changes made in this commit do not affect any actual code,
they are purely aesthetic. I ran clang-format with google style
over all .h/.cpp files in wpilibc that weren't in wpilibC++Sim
or gtest, and the eclipse formatter over all of the Java files
using the Google eclipse formatting configuration.
Change-Id: I9627bca0bc103c398ecc1c5ba17467193291ae63
This commit is contained in:
@@ -27,52 +27,45 @@ static const char *kValueSuffix = "\"\n";
|
||||
/** The singleton instance */
|
||||
Preferences *Preferences::_instance = NULL;
|
||||
|
||||
Preferences::Preferences() :
|
||||
m_fileLock(NULL),
|
||||
m_fileOpStarted(NULL),
|
||||
m_tableLock(NULL),
|
||||
m_readTask("PreferencesReadTask", (FUNCPTR)Preferences::InitReadTask),
|
||||
m_writeTask("PreferencesWriteTask", (FUNCPTR)Preferences::InitWriteTask)
|
||||
{
|
||||
m_fileLock = initializeMutexRecursive();
|
||||
m_fileOpStarted = initializeSemaphore (SEMAPHORE_EMPTY);
|
||||
m_tableLock = initializeMutexRecursive();
|
||||
Preferences::Preferences()
|
||||
: m_fileLock(NULL),
|
||||
m_fileOpStarted(NULL),
|
||||
m_tableLock(NULL),
|
||||
m_readTask("PreferencesReadTask", (FUNCPTR)Preferences::InitReadTask),
|
||||
m_writeTask("PreferencesWriteTask", (FUNCPTR)Preferences::InitWriteTask) {
|
||||
m_fileLock = initializeMutexRecursive();
|
||||
m_fileOpStarted = initializeSemaphore(SEMAPHORE_EMPTY);
|
||||
m_tableLock = initializeMutexRecursive();
|
||||
|
||||
Synchronized sync(m_fileLock);
|
||||
m_readTask.Start((uint32_t)this);
|
||||
takeSemaphore(m_fileOpStarted);
|
||||
Synchronized sync(m_fileLock);
|
||||
m_readTask.Start((uint32_t) this);
|
||||
takeSemaphore(m_fileOpStarted);
|
||||
|
||||
HALReport(HALUsageReporting::kResourceType_Preferences, 0);
|
||||
HALReport(HALUsageReporting::kResourceType_Preferences, 0);
|
||||
}
|
||||
|
||||
Preferences::~Preferences()
|
||||
{
|
||||
takeMutex(m_tableLock);
|
||||
deleteMutex(m_tableLock);
|
||||
takeMutex(m_fileLock);
|
||||
deleteSemaphore(m_fileOpStarted);
|
||||
deleteMutex(m_fileLock);
|
||||
Preferences::~Preferences() {
|
||||
takeMutex(m_tableLock);
|
||||
deleteMutex(m_tableLock);
|
||||
takeMutex(m_fileLock);
|
||||
deleteSemaphore(m_fileOpStarted);
|
||||
deleteMutex(m_fileLock);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the one and only {@link Preferences} object
|
||||
* @return pointer to the {@link Preferences}
|
||||
*/
|
||||
Preferences *Preferences::GetInstance()
|
||||
{
|
||||
if (_instance == NULL)
|
||||
_instance = new Preferences;
|
||||
return _instance;
|
||||
Preferences *Preferences::GetInstance() {
|
||||
if (_instance == NULL) _instance = new Preferences;
|
||||
return _instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a vector of all the keys
|
||||
* @return a vector of the keys
|
||||
*/
|
||||
std::vector<std::string> Preferences::GetKeys()
|
||||
{
|
||||
return m_keys;
|
||||
}
|
||||
std::vector<std::string> Preferences::GetKeys() { return m_keys; }
|
||||
|
||||
/**
|
||||
* Returns the string at the given key. If this table does not have a value
|
||||
@@ -81,10 +74,9 @@ std::vector<std::string> Preferences::GetKeys()
|
||||
* @param defaultValue the value to return if none exists in the table
|
||||
* @return either the value in the table, or the defaultValue
|
||||
*/
|
||||
std::string Preferences::GetString(const char *key, const char *defaultValue)
|
||||
{
|
||||
std::string value = Get(key);
|
||||
return value.empty() ? defaultValue : value;
|
||||
std::string Preferences::GetString(const char *key, const char *defaultValue) {
|
||||
std::string value = Get(key);
|
||||
return value.empty() ? defaultValue : value;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -96,11 +88,11 @@ std::string Preferences::GetString(const char *key, const char *defaultValue)
|
||||
* @param defaultValue the value to return if none exists in the table
|
||||
* @return The size of the returned string
|
||||
*/
|
||||
int Preferences::GetString(const char *key, char *value, int valueSize, const char *defaultValue)
|
||||
{
|
||||
std::string stringValue = GetString(key, defaultValue);
|
||||
stringValue.copy(value, valueSize);
|
||||
return stringValue.size();
|
||||
int Preferences::GetString(const char *key, char *value, int valueSize,
|
||||
const char *defaultValue) {
|
||||
std::string stringValue = GetString(key, defaultValue);
|
||||
stringValue.copy(value, valueSize);
|
||||
return stringValue.size();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -110,13 +102,11 @@ int Preferences::GetString(const char *key, char *value, int valueSize, const ch
|
||||
* @param defaultValue the value to return if none exists in the table
|
||||
* @return either the value in the table, or the defaultValue
|
||||
*/
|
||||
int Preferences::GetInt(const char *key, int defaultValue)
|
||||
{
|
||||
std::string value = Get(key);
|
||||
if (value.empty())
|
||||
return defaultValue;
|
||||
int Preferences::GetInt(const char *key, int defaultValue) {
|
||||
std::string value = Get(key);
|
||||
if (value.empty()) return defaultValue;
|
||||
|
||||
return strtol(value.c_str(), NULL, 0);
|
||||
return strtol(value.c_str(), NULL, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -126,13 +116,11 @@ int Preferences::GetInt(const char *key, int defaultValue)
|
||||
* @param defaultValue the value to return if none exists in the table
|
||||
* @return either the value in the table, or the defaultValue
|
||||
*/
|
||||
double Preferences::GetDouble(const char *key, double defaultValue)
|
||||
{
|
||||
std::string value = Get(key);
|
||||
if (value.empty())
|
||||
return defaultValue;
|
||||
double Preferences::GetDouble(const char *key, double defaultValue) {
|
||||
std::string value = Get(key);
|
||||
if (value.empty()) return defaultValue;
|
||||
|
||||
return strtod(value.c_str(), NULL);
|
||||
return strtod(value.c_str(), NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -142,13 +130,11 @@ double Preferences::GetDouble(const char *key, double defaultValue)
|
||||
* @param defaultValue the value to return if none exists in the table
|
||||
* @return either the value in the table, or the defaultValue
|
||||
*/
|
||||
float Preferences::GetFloat(const char *key, float defaultValue)
|
||||
{
|
||||
std::string value = Get(key);
|
||||
if (value.empty())
|
||||
return defaultValue;
|
||||
float Preferences::GetFloat(const char *key, float defaultValue) {
|
||||
std::string value = Get(key);
|
||||
if (value.empty()) return defaultValue;
|
||||
|
||||
return strtod(value.c_str(), NULL);
|
||||
return strtod(value.c_str(), NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -158,39 +144,38 @@ float Preferences::GetFloat(const char *key, float defaultValue)
|
||||
* @param defaultValue the value to return if none exists in the table
|
||||
* @return either the value in the table, or the defaultValue
|
||||
*/
|
||||
bool Preferences::GetBoolean(const char *key, bool defaultValue)
|
||||
{
|
||||
std::string value = Get(key);
|
||||
if (value.empty())
|
||||
return defaultValue;
|
||||
bool Preferences::GetBoolean(const char *key, bool defaultValue) {
|
||||
std::string value = Get(key);
|
||||
if (value.empty()) return defaultValue;
|
||||
|
||||
if (value.compare("true") == 0)
|
||||
return true;
|
||||
else if (value.compare("false") == 0)
|
||||
return false;
|
||||
if (value.compare("true") == 0)
|
||||
return true;
|
||||
else if (value.compare("false") == 0)
|
||||
return false;
|
||||
|
||||
wpi_setWPIErrorWithContext(ParameterOutOfRange, "Boolean value does not contain \"true\" or \"false\"");
|
||||
return false;
|
||||
wpi_setWPIErrorWithContext(
|
||||
ParameterOutOfRange,
|
||||
"Boolean value does not contain \"true\" or \"false\"");
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the long (int64_t) at the given key. If this table does not have a value
|
||||
* Returns the long (int64_t) at the given key. If this table does not have a
|
||||
* value
|
||||
* for that position, then the given defaultValue value will be returned.
|
||||
* @param key the key
|
||||
* @param defaultValue the value to return if none exists in the table
|
||||
* @return either the value in the table, or the defaultValue
|
||||
*/
|
||||
int64_t Preferences::GetLong(const char *key, int64_t defaultValue)
|
||||
{
|
||||
std::string value = Get(key);
|
||||
if (value.empty())
|
||||
return defaultValue;
|
||||
int64_t Preferences::GetLong(const char *key, int64_t defaultValue) {
|
||||
std::string value = Get(key);
|
||||
if (value.empty()) return defaultValue;
|
||||
|
||||
// Ummm... not available in our VxWorks...
|
||||
//return strtoll(value.c_str(), NULL, 0);
|
||||
int64_t intVal;
|
||||
sscanf(value.c_str(), "%lld", &intVal);
|
||||
return intVal;
|
||||
// Ummm... not available in our VxWorks...
|
||||
// return strtoll(value.c_str(), NULL, 0);
|
||||
int64_t intVal;
|
||||
sscanf(value.c_str(), "%lld", &intVal);
|
||||
return intVal;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -200,24 +185,23 @@ int64_t Preferences::GetLong(const char *key, int64_t defaultValue)
|
||||
* have any whitespace nor an equals sign</p>
|
||||
*
|
||||
* <p>This will <b>NOT</b> save the value to memory between power cycles,
|
||||
* to do that you must call {@link Preferences#Save() Save()} (which must be used with care).
|
||||
* to do that you must call {@link Preferences#Save() Save()} (which must be
|
||||
* used with care).
|
||||
* at some point after calling this.</p>
|
||||
* @param key the key
|
||||
* @param value the value
|
||||
*/
|
||||
void Preferences::PutString(const char *key, const char *value)
|
||||
{
|
||||
if (value == NULL)
|
||||
{
|
||||
wpi_setWPIErrorWithContext(NullParameter, "value");
|
||||
return;
|
||||
}
|
||||
if (std::string(value).find_first_of("\"") != std::string::npos)
|
||||
{
|
||||
wpi_setWPIErrorWithContext(ParameterOutOfRange, "value contains illegal characters");
|
||||
return;
|
||||
}
|
||||
Put(key, value);
|
||||
void Preferences::PutString(const char *key, const char *value) {
|
||||
if (value == NULL) {
|
||||
wpi_setWPIErrorWithContext(NullParameter, "value");
|
||||
return;
|
||||
}
|
||||
if (std::string(value).find_first_of("\"") != std::string::npos) {
|
||||
wpi_setWPIErrorWithContext(ParameterOutOfRange,
|
||||
"value contains illegal characters");
|
||||
return;
|
||||
}
|
||||
Put(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -226,16 +210,16 @@ void Preferences::PutString(const char *key, const char *value)
|
||||
* <p>The key may not have any whitespace nor an equals sign</p>
|
||||
*
|
||||
* <p>This will <b>NOT</b> save the value to memory between power cycles,
|
||||
* to do that you must call {@link Preferences#Save() Save()} (which must be used with care)
|
||||
* to do that you must call {@link Preferences#Save() Save()} (which must be
|
||||
* used with care)
|
||||
* at some point after calling this.</p>
|
||||
* @param key the key
|
||||
* @param value the value
|
||||
*/
|
||||
void Preferences::PutInt(const char *key, int value)
|
||||
{
|
||||
char buf[32];
|
||||
snprintf(buf, 32, "%d", value);
|
||||
Put(key, buf);
|
||||
void Preferences::PutInt(const char *key, int value) {
|
||||
char buf[32];
|
||||
snprintf(buf, 32, "%d", value);
|
||||
Put(key, buf);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -244,16 +228,16 @@ void Preferences::PutInt(const char *key, int value)
|
||||
* <p>The key may not have any whitespace nor an equals sign</p>
|
||||
*
|
||||
* <p>This will <b>NOT</b> save the value to memory between power cycles,
|
||||
* to do that you must call {@link Preferences#Save() Save()} (which must be used with care)
|
||||
* to do that you must call {@link Preferences#Save() Save()} (which must be
|
||||
* used with care)
|
||||
* at some point after calling this.</p>
|
||||
* @param key the key
|
||||
* @param value the value
|
||||
*/
|
||||
void Preferences::PutDouble(const char *key, double value)
|
||||
{
|
||||
char buf[32];
|
||||
snprintf(buf, 32, "%f", value);
|
||||
Put(key, buf);
|
||||
void Preferences::PutDouble(const char *key, double value) {
|
||||
char buf[32];
|
||||
snprintf(buf, 32, "%f", value);
|
||||
Put(key, buf);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -262,16 +246,16 @@ void Preferences::PutDouble(const char *key, double value)
|
||||
* <p>The key may not have any whitespace nor an equals sign</p>
|
||||
*
|
||||
* <p>This will <b>NOT</b> save the value to memory between power cycles,
|
||||
* to do that you must call {@link Preferences#Save() Save()} (which must be used with care)
|
||||
* to do that you must call {@link Preferences#Save() Save()} (which must be
|
||||
* used with care)
|
||||
* at some point after calling this.</p>
|
||||
* @param key the key
|
||||
* @param value the value
|
||||
*/
|
||||
void Preferences::PutFloat(const char *key, float value)
|
||||
{
|
||||
char buf[32];
|
||||
snprintf(buf, 32, "%f", value);
|
||||
Put(key, buf);
|
||||
void Preferences::PutFloat(const char *key, float value) {
|
||||
char buf[32];
|
||||
snprintf(buf, 32, "%f", value);
|
||||
Put(key, buf);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -280,14 +264,14 @@ void Preferences::PutFloat(const char *key, float value)
|
||||
* <p>The key may not have any whitespace nor an equals sign</p>
|
||||
*
|
||||
* <p>This will <b>NOT</b> save the value to memory between power cycles,
|
||||
* to do that you must call {@link Preferences#Save() Save()} (which must be used with care)
|
||||
* to do that you must call {@link Preferences#Save() Save()} (which must be
|
||||
* used with care)
|
||||
* at some point after calling this.</p>
|
||||
* @param key the key
|
||||
* @param value the value
|
||||
*/
|
||||
void Preferences::PutBoolean(const char *key, bool value)
|
||||
{
|
||||
Put(key, value ? "true" : "false");
|
||||
void Preferences::PutBoolean(const char *key, bool value) {
|
||||
Put(key, value ? "true" : "false");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -296,16 +280,16 @@ void Preferences::PutBoolean(const char *key, bool value)
|
||||
* <p>The key may not have any whitespace nor an equals sign</p>
|
||||
*
|
||||
* <p>This will <b>NOT</b> save the value to memory between power cycles,
|
||||
* to do that you must call {@link Preferences#Save() Save()} (which must be used with care)
|
||||
* to do that you must call {@link Preferences#Save() Save()} (which must be
|
||||
* used with care)
|
||||
* at some point after calling this.</p>
|
||||
* @param key the key
|
||||
* @param value the value
|
||||
*/
|
||||
void Preferences::PutLong(const char *key, int64_t value)
|
||||
{
|
||||
char buf[32];
|
||||
snprintf(buf, 32, "%lld", value);
|
||||
Put(key, buf);
|
||||
void Preferences::PutLong(const char *key, int64_t value) {
|
||||
char buf[32];
|
||||
snprintf(buf, 32, "%lld", value);
|
||||
Put(key, buf);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -317,13 +301,13 @@ void Preferences::PutLong(const char *key, int64_t value)
|
||||
* be called every run of {@link IterativeRobot#TeleopPeriodic()}, etc.</p>
|
||||
*
|
||||
* <p>The actual writing of the file is done in a separate thread.
|
||||
* However, any call to a get or put method will wait until the table is fully saved before continuing.</p>
|
||||
* However, any call to a get or put method will wait until the table is fully
|
||||
* saved before continuing.</p>
|
||||
*/
|
||||
void Preferences::Save()
|
||||
{
|
||||
Synchronized sync(m_fileLock);
|
||||
m_writeTask.Start((uint32_t)this);
|
||||
takeSemaphore(m_fileOpStarted);
|
||||
void Preferences::Save() {
|
||||
Synchronized sync(m_fileLock);
|
||||
m_writeTask.Start((uint32_t) this);
|
||||
takeSemaphore(m_fileOpStarted);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -331,27 +315,21 @@ void Preferences::Save()
|
||||
* @param key the key
|
||||
* @return if there is a value at the given key
|
||||
*/
|
||||
bool Preferences::ContainsKey(const char *key)
|
||||
{
|
||||
return !Get(key).empty();
|
||||
}
|
||||
bool Preferences::ContainsKey(const char *key) { return !Get(key).empty(); }
|
||||
|
||||
/**
|
||||
* Remove a preference
|
||||
* @param key the key
|
||||
*/
|
||||
void Preferences::Remove(const char *key)
|
||||
{
|
||||
m_values.erase(std::string(key));
|
||||
std::vector<std::string>::iterator it = m_keys.begin();
|
||||
for (; it != m_keys.end(); it++)
|
||||
{
|
||||
if (it->compare(key) == 0)
|
||||
{
|
||||
m_keys.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
void Preferences::Remove(const char *key) {
|
||||
m_values.erase(std::string(key));
|
||||
std::vector<std::string>::iterator it = m_keys.begin();
|
||||
for (; it != m_keys.end(); it++) {
|
||||
if (it->compare(key) == 0) {
|
||||
m_keys.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -359,15 +337,13 @@ void Preferences::Remove(const char *key)
|
||||
* @param key the key
|
||||
* @return the value (or empty if none exists)
|
||||
*/
|
||||
std::string Preferences::Get(const char *key)
|
||||
{
|
||||
Synchronized sync(m_tableLock);
|
||||
if (key == NULL)
|
||||
{
|
||||
wpi_setWPIErrorWithContext(NullParameter, "key");
|
||||
return std::string("");
|
||||
}
|
||||
return m_values[std::string(key)];
|
||||
std::string Preferences::Get(const char *key) {
|
||||
Synchronized sync(m_tableLock);
|
||||
if (key == NULL) {
|
||||
wpi_setWPIErrorWithContext(NullParameter, "key");
|
||||
return std::string("");
|
||||
}
|
||||
return m_values[std::string(key)];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -375,29 +351,27 @@ std::string Preferences::Get(const char *key)
|
||||
* @param key the key
|
||||
* @param value the value
|
||||
*/
|
||||
void Preferences::Put(const char *key, std::string value)
|
||||
{
|
||||
Synchronized sync(m_tableLock);
|
||||
if (key == NULL)
|
||||
{
|
||||
wpi_setWPIErrorWithContext(NullParameter, "key");
|
||||
return;
|
||||
}
|
||||
void Preferences::Put(const char *key, std::string value) {
|
||||
Synchronized sync(m_tableLock);
|
||||
if (key == NULL) {
|
||||
wpi_setWPIErrorWithContext(NullParameter, "key");
|
||||
return;
|
||||
}
|
||||
|
||||
if (std::string(key).find_first_of("=\n\r \t\"") != std::string::npos)
|
||||
{
|
||||
wpi_setWPIErrorWithContext(ParameterOutOfRange, "key contains illegal characters");
|
||||
return;
|
||||
}
|
||||
if (std::string(key).find_first_of("=\n\r \t\"") != std::string::npos) {
|
||||
wpi_setWPIErrorWithContext(ParameterOutOfRange,
|
||||
"key contains illegal characters");
|
||||
return;
|
||||
}
|
||||
|
||||
std::pair<StringMap::iterator, bool> ret =
|
||||
m_values.insert(StringMap::value_type(key, value));
|
||||
if (ret.second)
|
||||
m_keys.push_back(key);
|
||||
else
|
||||
ret.first->second = value;
|
||||
std::pair<StringMap::iterator, bool> ret =
|
||||
m_values.insert(StringMap::value_type(key, value));
|
||||
if (ret.second)
|
||||
m_keys.push_back(key);
|
||||
else
|
||||
ret.first->second = value;
|
||||
|
||||
NetworkTable::GetTable(kTableName)->PutString(key, value);
|
||||
NetworkTable::GetTable(kTableName)->PutString(key, value);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -405,215 +379,181 @@ void Preferences::Put(const char *key, std::string value)
|
||||
* This will be called in its own thread when the preferences singleton is
|
||||
* first created.
|
||||
*/
|
||||
void Preferences::ReadTaskRun()
|
||||
{
|
||||
Synchronized sync(m_tableLock);
|
||||
giveSemaphore(m_fileOpStarted);
|
||||
void Preferences::ReadTaskRun() {
|
||||
Synchronized sync(m_tableLock);
|
||||
giveSemaphore(m_fileOpStarted);
|
||||
|
||||
std::string comment;
|
||||
std::string comment;
|
||||
|
||||
FILE *file = NULL;
|
||||
file = fopen(kFileName, "r");
|
||||
FILE *file = NULL;
|
||||
file = fopen(kFileName, "r");
|
||||
|
||||
if (file != NULL)
|
||||
{
|
||||
std::string buffer;
|
||||
while (true)
|
||||
{
|
||||
char value;
|
||||
do
|
||||
{
|
||||
value = fgetc(file);
|
||||
} while (value == ' ' || value == '\t');
|
||||
if (file != NULL) {
|
||||
std::string buffer;
|
||||
while (true) {
|
||||
char value;
|
||||
do {
|
||||
value = fgetc(file);
|
||||
} while (value == ' ' || value == '\t');
|
||||
|
||||
if (value == '\n' || value == ';')
|
||||
{
|
||||
if (value == '\n')
|
||||
{
|
||||
comment += "\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer.clear();
|
||||
for (; value != '\n' && !feof(file); value = fgetc(file))
|
||||
buffer += value;
|
||||
buffer += '\n';
|
||||
comment += buffer;
|
||||
}
|
||||
}
|
||||
else if (value == '[')
|
||||
{
|
||||
// Find the end of the section and the new line after it and throw it away
|
||||
for (; value != ']' && !feof(file); value = fgetc(file));
|
||||
for (; value != '\n' && !feof(file); value = fgetc(file));
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer.clear();
|
||||
for (; value != '=' && !feof(file); )
|
||||
{
|
||||
buffer += value;
|
||||
do
|
||||
{
|
||||
value = fgetc(file);
|
||||
} while (value == ' ' || value == '\t');
|
||||
}
|
||||
std::string name = buffer;
|
||||
buffer.clear();
|
||||
if (value == '\n' || value == ';') {
|
||||
if (value == '\n') {
|
||||
comment += "\n";
|
||||
} else {
|
||||
buffer.clear();
|
||||
for (; value != '\n' && !feof(file); value = fgetc(file))
|
||||
buffer += value;
|
||||
buffer += '\n';
|
||||
comment += buffer;
|
||||
}
|
||||
} else if (value == '[') {
|
||||
// Find the end of the section and the new line after it and throw it
|
||||
// away
|
||||
for (; value != ']' && !feof(file); value = fgetc(file))
|
||||
;
|
||||
for (; value != '\n' && !feof(file); value = fgetc(file))
|
||||
;
|
||||
} else {
|
||||
buffer.clear();
|
||||
for (; value != '=' && !feof(file);) {
|
||||
buffer += value;
|
||||
do {
|
||||
value = fgetc(file);
|
||||
} while (value == ' ' || value == '\t');
|
||||
}
|
||||
std::string name = buffer;
|
||||
buffer.clear();
|
||||
|
||||
bool shouldBreak = false;
|
||||
bool shouldBreak = false;
|
||||
|
||||
do
|
||||
{
|
||||
value = fgetc(file);
|
||||
} while (value == ' ' || value == '\t');
|
||||
do {
|
||||
value = fgetc(file);
|
||||
} while (value == ' ' || value == '\t');
|
||||
|
||||
if (value == '"')
|
||||
{
|
||||
for (value = fgetc(file); value != '"' && !feof(file); value = fgetc(file))
|
||||
buffer += value;
|
||||
if (value == '"') {
|
||||
for (value = fgetc(file); value != '"' && !feof(file);
|
||||
value = fgetc(file))
|
||||
buffer += value;
|
||||
|
||||
// Clear the line
|
||||
while (fgetc(file) != '\n' && !feof(file));
|
||||
}
|
||||
else
|
||||
{
|
||||
for (; value != '\n' && !feof(file);)
|
||||
{
|
||||
buffer += value;
|
||||
do
|
||||
{
|
||||
value = fgetc(file);
|
||||
} while (value == ' ' || value == '\t');
|
||||
}
|
||||
if (feof(file))
|
||||
shouldBreak = true;
|
||||
}
|
||||
// Clear the line
|
||||
while (fgetc(file) != '\n' && !feof(file))
|
||||
;
|
||||
} else {
|
||||
for (; value != '\n' && !feof(file);) {
|
||||
buffer += value;
|
||||
do {
|
||||
value = fgetc(file);
|
||||
} while (value == ' ' || value == '\t');
|
||||
}
|
||||
if (feof(file)) shouldBreak = true;
|
||||
}
|
||||
|
||||
std::string value = buffer;
|
||||
std::string value = buffer;
|
||||
|
||||
if (!name.empty() && !value.empty())
|
||||
{
|
||||
m_keys.push_back(name);
|
||||
m_values.insert(std::pair<std::string, std::string>(name, value));
|
||||
NetworkTable::GetTable(kTableName)->PutString(name, value);
|
||||
if (!name.empty() && !value.empty()) {
|
||||
m_keys.push_back(name);
|
||||
m_values.insert(std::pair<std::string, std::string>(name, value));
|
||||
NetworkTable::GetTable(kTableName)->PutString(name, value);
|
||||
|
||||
if (!comment.empty())
|
||||
{
|
||||
m_comments.insert(std::pair<std::string, std::string>(name, comment));
|
||||
comment.clear();
|
||||
}
|
||||
}
|
||||
if (!comment.empty()) {
|
||||
m_comments.insert(
|
||||
std::pair<std::string, std::string>(name, comment));
|
||||
comment.clear();
|
||||
}
|
||||
}
|
||||
|
||||
if (shouldBreak)
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
wpi_setErrnoErrorWithContext("Opening preferences file");
|
||||
}
|
||||
if (shouldBreak) break;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
wpi_setErrnoErrorWithContext("Opening preferences file");
|
||||
}
|
||||
|
||||
if (file != NULL)
|
||||
fclose(file);
|
||||
if (file != NULL) fclose(file);
|
||||
|
||||
if (!comment.empty())
|
||||
m_endComment = comment;
|
||||
if (!comment.empty()) m_endComment = comment;
|
||||
|
||||
NetworkTable::GetTable(kTableName)->PutBoolean(kSaveField, false);
|
||||
NetworkTable::GetTable(kTableName)->AddTableListener(this);
|
||||
NetworkTable::GetTable(kTableName)->PutBoolean(kSaveField, false);
|
||||
NetworkTable::GetTable(kTableName)->AddTableListener(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Internal method that actually writes the table to a file.
|
||||
* This is called in its own thread when {@link Preferences#Save() Save()} is called.
|
||||
* This is called in its own thread when {@link Preferences#Save() Save()} is
|
||||
* called.
|
||||
*/
|
||||
void Preferences::WriteTaskRun()
|
||||
{
|
||||
Synchronized sync(m_tableLock);
|
||||
giveSemaphore(m_fileOpStarted);
|
||||
void Preferences::WriteTaskRun() {
|
||||
Synchronized sync(m_tableLock);
|
||||
giveSemaphore(m_fileOpStarted);
|
||||
|
||||
FILE *file = NULL;
|
||||
file = fopen(kFileName, "w");
|
||||
FILE *file = NULL;
|
||||
file = fopen(kFileName, "w");
|
||||
|
||||
fputs("[Preferences]\n", file);
|
||||
std::vector<std::string>::iterator it = m_keys.begin();
|
||||
for (; it != m_keys.end(); it++)
|
||||
{
|
||||
std::string key = *it;
|
||||
std::string value = m_values[key];
|
||||
std::string comment = m_comments[key];
|
||||
fputs("[Preferences]\n", file);
|
||||
std::vector<std::string>::iterator it = m_keys.begin();
|
||||
for (; it != m_keys.end(); it++) {
|
||||
std::string key = *it;
|
||||
std::string value = m_values[key];
|
||||
std::string comment = m_comments[key];
|
||||
|
||||
if (!comment.empty())
|
||||
fputs(comment.c_str(), file);
|
||||
if (!comment.empty()) fputs(comment.c_str(), file);
|
||||
|
||||
fputs(key.c_str(), file);
|
||||
fputs(kValuePrefix, file);
|
||||
fputs(value.c_str(), file);
|
||||
fputs(kValueSuffix, file);
|
||||
}
|
||||
fputs(key.c_str(), file);
|
||||
fputs(kValuePrefix, file);
|
||||
fputs(value.c_str(), file);
|
||||
fputs(kValueSuffix, file);
|
||||
}
|
||||
|
||||
if (!m_endComment.empty())
|
||||
fputs(m_endComment.c_str(), file);
|
||||
if (!m_endComment.empty()) fputs(m_endComment.c_str(), file);
|
||||
|
||||
if (file != NULL)
|
||||
fclose(file);
|
||||
if (file != NULL) fclose(file);
|
||||
|
||||
NetworkTable::GetTable(kTableName)->PutBoolean(kSaveField, false);
|
||||
NetworkTable::GetTable(kTableName)->PutBoolean(kSaveField, false);
|
||||
}
|
||||
|
||||
static bool isKeyAcceptable(const std::string& value) {
|
||||
for (unsigned int i = 0; i < value.length(); i++) {
|
||||
char letter = value.at(i);
|
||||
switch (letter) {
|
||||
case '=':
|
||||
case '\n':
|
||||
case '\r':
|
||||
case ' ':
|
||||
case '\t':
|
||||
case '[':
|
||||
case ']':
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
static bool isKeyAcceptable(const std::string &value) {
|
||||
for (unsigned int i = 0; i < value.length(); i++) {
|
||||
char letter = value.at(i);
|
||||
switch (letter) {
|
||||
case '=':
|
||||
case '\n':
|
||||
case '\r':
|
||||
case ' ':
|
||||
case '\t':
|
||||
case '[':
|
||||
case ']':
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void Preferences::ValueChanged(ITable* table, const std::string& key, EntryValue value, bool isNew)
|
||||
{
|
||||
if (key==kSaveField)
|
||||
{
|
||||
if (table->GetBoolean(kSaveField, false))
|
||||
Save();
|
||||
}
|
||||
else
|
||||
{
|
||||
Synchronized sync(m_tableLock);
|
||||
void Preferences::ValueChanged(ITable *table, const std::string &key,
|
||||
EntryValue value, bool isNew) {
|
||||
if (key == kSaveField) {
|
||||
if (table->GetBoolean(kSaveField, false)) Save();
|
||||
} else {
|
||||
Synchronized sync(m_tableLock);
|
||||
|
||||
if (!isKeyAcceptable(key) || table->GetString(key, "").find('"')!=std::string::npos)
|
||||
{
|
||||
if(m_values.find(key) != m_values.end()){
|
||||
m_values.erase(key);
|
||||
std::vector<std::string>::iterator it = m_keys.begin();
|
||||
for (; it != m_keys.end(); it++)
|
||||
{
|
||||
if (key==*it)
|
||||
{
|
||||
m_keys.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
table->PutString(key, "\"");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
std::pair<StringMap::iterator, bool> ret =
|
||||
m_values.insert(StringMap::value_type(key, table->GetString(key, "")));
|
||||
if (ret.second)
|
||||
m_keys.push_back(key);
|
||||
else
|
||||
ret.first->second = table->GetString(key, "");
|
||||
}
|
||||
}
|
||||
if (!isKeyAcceptable(key) ||
|
||||
table->GetString(key, "").find('"') != std::string::npos) {
|
||||
if (m_values.find(key) != m_values.end()) {
|
||||
m_values.erase(key);
|
||||
std::vector<std::string>::iterator it = m_keys.begin();
|
||||
for (; it != m_keys.end(); it++) {
|
||||
if (key == *it) {
|
||||
m_keys.erase(it);
|
||||
break;
|
||||
}
|
||||
}
|
||||
table->PutString(key, "\"");
|
||||
}
|
||||
} else {
|
||||
std::pair<StringMap::iterator, bool> ret = m_values.insert(
|
||||
StringMap::value_type(key, table->GetString(key, "")));
|
||||
if (ret.second)
|
||||
m_keys.push_back(key);
|
||||
else
|
||||
ret.first->second = table->GetString(key, "");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user