2013-12-15 18:30:16 -05:00
|
|
|
/*----------------------------------------------------------------------------*/
|
2014-10-19 13:34:50 -07:00
|
|
|
/* Copyright (c) FIRST 2011. All Rights Reserved. */
|
2013-12-15 18:30:16 -05:00
|
|
|
/* Open Source Software - may be modified and shared by FRC teams. The code */
|
|
|
|
|
/* must be accompanied by the FIRST BSD license file in $(WIND_BASE)/WPILib. */
|
|
|
|
|
/*----------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
#include "Preferences.h"
|
|
|
|
|
|
2014-01-06 10:12:21 -05:00
|
|
|
//#include "NetworkCommunication/UsageReporting.h"
|
2014-05-02 17:54:01 -04:00
|
|
|
#include "HAL/cpp/Synchronized.hpp"
|
2013-12-15 18:30:16 -05:00
|
|
|
#include "WPIErrors.h"
|
2014-09-25 20:36:59 -04:00
|
|
|
#include "HAL/HAL.hpp"
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
/** The Preferences table name */
|
|
|
|
|
static const char *kTableName = "Preferences";
|
|
|
|
|
/** The value of the save field */
|
|
|
|
|
static const char *kSaveField = "~S A V E~";
|
|
|
|
|
/** The file to save to */
|
2014-09-10 14:05:51 -04:00
|
|
|
static const char *kFileName = "/home/lvuser/wpilib-preferences.ini";
|
2013-12-15 18:30:16 -05:00
|
|
|
/** The characters to put between a field and value */
|
|
|
|
|
static const char *kValuePrefix = "=\"";
|
|
|
|
|
/** The characters to put after the value */
|
|
|
|
|
static const char *kValueSuffix = "\"\n";
|
|
|
|
|
/** The singleton instance */
|
2015-06-23 04:49:51 -07:00
|
|
|
Preferences *Preferences::_instance = nullptr;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
Preferences::Preferences()
|
2015-06-24 01:06:29 -07:00
|
|
|
: m_readTask("PreferencesReadTask", (FUNCPTR)Preferences::InitReadTask),
|
2015-06-25 15:07:55 -04:00
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
HALReport(HALUsageReporting::kResourceType_Preferences, 0);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
Preferences::~Preferences() {
|
|
|
|
|
takeMutex(m_tableLock);
|
|
|
|
|
deleteMutex(m_tableLock);
|
|
|
|
|
takeMutex(m_fileLock);
|
|
|
|
|
deleteSemaphore(m_fileOpStarted);
|
|
|
|
|
deleteMutex(m_fileLock);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the one and only {@link Preferences} object
|
|
|
|
|
* @return pointer to the {@link Preferences}
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
Preferences *Preferences::GetInstance() {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (_instance == nullptr) _instance = new Preferences;
|
2015-06-25 15:07:55 -04:00
|
|
|
return _instance;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a vector of all the keys
|
|
|
|
|
* @return a vector of the keys
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
std::vector<std::string> Preferences::GetKeys() { return m_keys; }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the string at the given key. If this table does not have a value
|
|
|
|
|
* for that position, then the given defaultValue 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
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
std::string Preferences::GetString(const char *key, const char *defaultValue) {
|
|
|
|
|
std::string value = Get(key);
|
|
|
|
|
return value.empty() ? defaultValue : value;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the string at the given key. If this table does not have a value
|
|
|
|
|
* for that position, then the given defaultValue will be returned.
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @param value the buffer to copy the value into
|
|
|
|
|
* @param valueSize the size of value
|
|
|
|
|
* @param defaultValue the value to return if none exists in the table
|
|
|
|
|
* @return The size of the returned string
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
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();
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the int 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
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
int Preferences::GetInt(const char *key, int defaultValue) {
|
|
|
|
|
std::string value = Get(key);
|
|
|
|
|
if (value.empty()) return defaultValue;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-23 04:49:51 -07:00
|
|
|
return strtol(value.c_str(), nullptr, 0);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the double 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
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
double Preferences::GetDouble(const char *key, double defaultValue) {
|
|
|
|
|
std::string value = Get(key);
|
|
|
|
|
if (value.empty()) return defaultValue;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-23 04:49:51 -07:00
|
|
|
return strtod(value.c_str(), nullptr);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the float 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
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
float Preferences::GetFloat(const char *key, float defaultValue) {
|
|
|
|
|
std::string value = Get(key);
|
|
|
|
|
if (value.empty()) return defaultValue;
|
2013-12-15 18:30:16 -05:00
|
|
|
|
2015-06-23 04:49:51 -07:00
|
|
|
return strtod(value.c_str(), nullptr);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the boolean 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
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
wpi_setWPIErrorWithContext(
|
|
|
|
|
ParameterOutOfRange,
|
|
|
|
|
"Boolean value does not contain \"true\" or \"false\"");
|
|
|
|
|
return false;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2015-06-25 15:07:55 -04:00
|
|
|
* Returns the long (int64_t) at the given key. If this table does not have a
|
|
|
|
|
* value
|
2013-12-15 18:30:16 -05:00
|
|
|
* 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
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
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...
|
2015-06-23 04:49:51 -07:00
|
|
|
// return strtoll(value.c_str(), nullptr, 0);
|
2015-06-25 15:07:55 -04:00
|
|
|
int64_t intVal;
|
|
|
|
|
sscanf(value.c_str(), "%lld", &intVal);
|
|
|
|
|
return intVal;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Puts the given string into the preferences table.
|
|
|
|
|
*
|
|
|
|
|
* <p>The value may not have quotation marks, nor may the key
|
|
|
|
|
* have any whitespace nor an equals sign</p>
|
|
|
|
|
*
|
|
|
|
|
* <p>This will <b>NOT</b> save the value to memory between power cycles,
|
2015-06-25 15:07:55 -04:00
|
|
|
* to do that you must call {@link Preferences#Save() Save()} (which must be
|
|
|
|
|
* used with care).
|
2013-12-15 18:30:16 -05:00
|
|
|
* at some point after calling this.</p>
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @param value the value
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Preferences::PutString(const char *key, const char *value) {
|
2015-06-23 04:49:51 -07:00
|
|
|
if (value == nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
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);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Puts the given int into the preferences table.
|
|
|
|
|
*
|
|
|
|
|
* <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,
|
2015-06-25 15:07:55 -04:00
|
|
|
* to do that you must call {@link Preferences#Save() Save()} (which must be
|
|
|
|
|
* used with care)
|
2013-12-15 18:30:16 -05:00
|
|
|
* at some point after calling this.</p>
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @param value the value
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Preferences::PutInt(const char *key, int value) {
|
|
|
|
|
char buf[32];
|
|
|
|
|
snprintf(buf, 32, "%d", value);
|
|
|
|
|
Put(key, buf);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Puts the given double into the preferences table.
|
|
|
|
|
*
|
|
|
|
|
* <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,
|
2015-06-25 15:07:55 -04:00
|
|
|
* to do that you must call {@link Preferences#Save() Save()} (which must be
|
|
|
|
|
* used with care)
|
2013-12-15 18:30:16 -05:00
|
|
|
* at some point after calling this.</p>
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @param value the value
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Preferences::PutDouble(const char *key, double value) {
|
|
|
|
|
char buf[32];
|
|
|
|
|
snprintf(buf, 32, "%f", value);
|
|
|
|
|
Put(key, buf);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Puts the given float into the preferences table.
|
|
|
|
|
*
|
|
|
|
|
* <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,
|
2015-06-25 15:07:55 -04:00
|
|
|
* to do that you must call {@link Preferences#Save() Save()} (which must be
|
|
|
|
|
* used with care)
|
2013-12-15 18:30:16 -05:00
|
|
|
* at some point after calling this.</p>
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @param value the value
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Preferences::PutFloat(const char *key, float value) {
|
|
|
|
|
char buf[32];
|
|
|
|
|
snprintf(buf, 32, "%f", value);
|
|
|
|
|
Put(key, buf);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Puts the given boolean into the preferences table.
|
|
|
|
|
*
|
|
|
|
|
* <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,
|
2015-06-25 15:07:55 -04:00
|
|
|
* to do that you must call {@link Preferences#Save() Save()} (which must be
|
|
|
|
|
* used with care)
|
2013-12-15 18:30:16 -05:00
|
|
|
* at some point after calling this.</p>
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @param value the value
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Preferences::PutBoolean(const char *key, bool value) {
|
|
|
|
|
Put(key, value ? "true" : "false");
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Puts the given long (int64_t) into the preferences table.
|
|
|
|
|
*
|
|
|
|
|
* <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,
|
2015-06-25 15:07:55 -04:00
|
|
|
* to do that you must call {@link Preferences#Save() Save()} (which must be
|
|
|
|
|
* used with care)
|
2013-12-15 18:30:16 -05:00
|
|
|
* at some point after calling this.</p>
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @param value the value
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Preferences::PutLong(const char *key, int64_t value) {
|
|
|
|
|
char buf[32];
|
|
|
|
|
snprintf(buf, 32, "%lld", value);
|
|
|
|
|
Put(key, buf);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2014-08-05 11:17:38 -04:00
|
|
|
* Saves the preferences to a file on the RoboRIO.
|
2013-12-15 18:30:16 -05:00
|
|
|
*
|
|
|
|
|
* <p>This should <b>NOT</b> be called often.
|
2014-08-05 11:17:38 -04:00
|
|
|
* Too many writes can damage the RoboRIO's flash memory.
|
2013-12-15 18:30:16 -05:00
|
|
|
* While it is ok to save once or twice a match, this should never
|
|
|
|
|
* be called every run of {@link IterativeRobot#TeleopPeriodic()}, etc.</p>
|
|
|
|
|
*
|
|
|
|
|
* <p>The actual writing of the file is done in a separate thread.
|
2015-06-25 15:07:55 -04:00
|
|
|
* However, any call to a get or put method will wait until the table is fully
|
|
|
|
|
* saved before continuing.</p>
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Preferences::Save() {
|
|
|
|
|
Synchronized sync(m_fileLock);
|
|
|
|
|
m_writeTask.Start((uint32_t) this);
|
|
|
|
|
takeSemaphore(m_fileOpStarted);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns whether or not there is a key with the given name.
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @return if there is a value at the given key
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
bool Preferences::ContainsKey(const char *key) { return !Get(key).empty(); }
|
2013-12-15 18:30:16 -05:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Remove a preference
|
|
|
|
|
* @param key the key
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Preferences::Remove(const char *key) {
|
|
|
|
|
m_values.erase(std::string(key));
|
2015-06-23 04:49:51 -07:00
|
|
|
auto it = m_keys.begin();
|
2015-06-25 15:07:55 -04:00
|
|
|
for (; it != m_keys.end(); it++) {
|
|
|
|
|
if (it->compare(key) == 0) {
|
|
|
|
|
m_keys.erase(it);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the value at the given key.
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @return the value (or empty if none exists)
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
std::string Preferences::Get(const char *key) {
|
|
|
|
|
Synchronized sync(m_tableLock);
|
2015-06-23 04:49:51 -07:00
|
|
|
if (key == nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
wpi_setWPIErrorWithContext(NullParameter, "key");
|
|
|
|
|
return std::string("");
|
|
|
|
|
}
|
|
|
|
|
return m_values[std::string(key)];
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Puts the given value into the given key position
|
|
|
|
|
* @param key the key
|
|
|
|
|
* @param value the value
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Preferences::Put(const char *key, std::string value) {
|
|
|
|
|
Synchronized sync(m_tableLock);
|
2015-06-23 04:49:51 -07:00
|
|
|
if (key == nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The internal method to read from a file.
|
|
|
|
|
* This will be called in its own thread when the preferences singleton is
|
|
|
|
|
* first created.
|
|
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Preferences::ReadTaskRun() {
|
|
|
|
|
Synchronized sync(m_tableLock);
|
|
|
|
|
giveSemaphore(m_fileOpStarted);
|
|
|
|
|
|
|
|
|
|
std::string comment;
|
|
|
|
|
|
2015-06-23 04:49:51 -07:00
|
|
|
FILE *file = nullptr;
|
2015-06-25 15:07:55 -04:00
|
|
|
file = fopen(kFileName, "r");
|
|
|
|
|
|
2015-06-23 04:49:51 -07:00
|
|
|
if (file != nullptr) {
|
2015-06-25 15:07:55 -04:00
|
|
|
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();
|
|
|
|
|
|
|
|
|
|
bool shouldBreak = false;
|
|
|
|
|
|
|
|
|
|
do {
|
|
|
|
|
value = fgetc(file);
|
|
|
|
|
} while (value == ' ' || value == '\t');
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 (!comment.empty()) {
|
|
|
|
|
m_comments.insert(
|
|
|
|
|
std::pair<std::string, std::string>(name, comment));
|
|
|
|
|
comment.clear();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (shouldBreak) break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
wpi_setErrnoErrorWithContext("Opening preferences file");
|
|
|
|
|
}
|
|
|
|
|
|
2015-06-23 04:49:51 -07:00
|
|
|
if (file != nullptr) fclose(file);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
if (!comment.empty()) m_endComment = comment;
|
|
|
|
|
|
|
|
|
|
NetworkTable::GetTable(kTableName)->PutBoolean(kSaveField, false);
|
|
|
|
|
NetworkTable::GetTable(kTableName)->AddTableListener(this);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Internal method that actually writes the table to a file.
|
2015-06-25 15:07:55 -04:00
|
|
|
* This is called in its own thread when {@link Preferences#Save() Save()} is
|
|
|
|
|
* called.
|
2013-12-15 18:30:16 -05:00
|
|
|
*/
|
2015-06-25 15:07:55 -04:00
|
|
|
void Preferences::WriteTaskRun() {
|
|
|
|
|
Synchronized sync(m_tableLock);
|
|
|
|
|
giveSemaphore(m_fileOpStarted);
|
|
|
|
|
|
2015-06-23 04:49:51 -07:00
|
|
|
FILE *file = nullptr;
|
2015-06-25 15:07:55 -04:00
|
|
|
file = fopen(kFileName, "w");
|
|
|
|
|
|
|
|
|
|
fputs("[Preferences]\n", file);
|
2015-06-23 04:49:51 -07:00
|
|
|
auto it = m_keys.begin();
|
2015-06-25 15:07:55 -04:00
|
|
|
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);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
2015-06-23 04:49:51 -07:00
|
|
|
if (file != nullptr) fclose(file);
|
2015-06-25 15:07:55 -04:00
|
|
|
|
|
|
|
|
NetworkTable::GetTable(kTableName)->PutBoolean(kSaveField, false);
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
|
|
|
|
|
2015-06-25 15:07:55 -04:00
|
|
|
static bool isKeyAcceptable(const std::string &value) {
|
2015-06-23 04:49:51 -07:00
|
|
|
for (auto letter : value) {
|
2015-06-25 15:07:55 -04:00
|
|
|
switch (letter) {
|
|
|
|
|
case '=':
|
|
|
|
|
case '\n':
|
|
|
|
|
case '\r':
|
|
|
|
|
case ' ':
|
|
|
|
|
case '\t':
|
|
|
|
|
case '[':
|
|
|
|
|
case ']':
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return true;
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|
2015-06-25 15:07:55 -04:00
|
|
|
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);
|
2015-06-23 04:49:51 -07:00
|
|
|
auto it = m_keys.begin();
|
2015-06-25 15:07:55 -04:00
|
|
|
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, "");
|
|
|
|
|
}
|
|
|
|
|
}
|
2013-12-15 18:30:16 -05:00
|
|
|
}
|