123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- const String DB_root = "/db/";
- String DBCleanInput(const String& inputString) {
- String trimmedString = inputString;
-
- trimmedString.replace("/", "");
-
- trimmedString.trim();
- return trimmedString;
- }
- void DBInit() {
- DBNewTable("auth");
- DBNewTable("pref");
- }
- void DBNewTable(String tableName) {
- tableName = DBCleanInput(tableName);
- if (!SD.exists(DB_root + tableName)) {
- SD.mkdir(DB_root + tableName);
- }
- }
- bool DBTableExists(String tableName) {
- tableName = DBCleanInput(tableName);
- return SD.exists(DB_root + tableName);
- }
- bool DBWrite(String tableName, String key, String value) {
- if (!DBTableExists(tableName)) {
- return false;
- }
- tableName = DBCleanInput(tableName);
- key = DBCleanInput(key);
- String fsDataPath = DB_root + tableName + "/" + key;
- if (SD.exists(fsDataPath)) {
-
- SD.remove(fsDataPath);
- }
-
- File targetEntry = SD.open(fsDataPath, FILE_WRITE);
- targetEntry.print(value);
- targetEntry.close();
- return true;
- }
- String DBRead(String tableName, String key) {
- if (!DBTableExists(tableName)) {
- return "";
- }
- tableName = DBCleanInput(tableName);
- key = DBCleanInput(key);
- String fsDataPath = DB_root + tableName + "/" + key;
- if (!SD.exists(fsDataPath)) {
-
- return "";
- }
- String value = "";
- File targetEntry = SD.open(fsDataPath, FILE_READ);
- while (targetEntry.available()) {
- value = value + targetEntry.readString();
- }
- targetEntry.close();
- return value;
- }
- bool DBKeyExists(String tableName, String key) {
- if (!DBTableExists(tableName)) {
- return false;
- }
- tableName = DBCleanInput(tableName);
- key = DBCleanInput(key);
- String fsDataPath = DB_root + tableName + "/" + key;
- return SD.exists(fsDataPath);
- }
- bool DBRemove(String tableName, String key) {
- if (!DBKeyExists(tableName, key)){
- return false;
- }
- tableName = DBCleanInput(tableName);
- key = DBCleanInput(key);
- String fsDataPath = DB_root + tableName + "/" + key;
- SD.remove(fsDataPath);
- return true;
- }
|