wip: ini parse baseline
This commit is contained in:
parent
8e93120555
commit
8e07b1efdd
9 changed files with 356 additions and 27 deletions
75
src/settings.h
Normal file
75
src/settings.h
Normal file
|
|
@ -0,0 +1,75 @@
|
|||
#pragma once
|
||||
#include "global.h"
|
||||
|
||||
namespace ini {
|
||||
|
||||
//Trims spaces, LF and CRLF
|
||||
static inline char* trimAndAllocate(const char* in, uint64_t len = 0) {
|
||||
if (!in) return nullptr;
|
||||
|
||||
uint64_t startingPos = 0, lastPos = 0;
|
||||
bool foundStart = false;
|
||||
for(int i = 0; ;i++) {
|
||||
char c = in[i];
|
||||
if ((len > 0 && startingPos == (len - 1)) || (len > 0 && lastPos == (len - 1))) return nullptr;
|
||||
if ((c != ' ' || c != '\r' || c != '\n') && !foundStart) {
|
||||
foundStart = true;
|
||||
lastPos = startingPos;
|
||||
}
|
||||
if (foundStart && (c == ' ' || c == '\r' || c == '\n')) {
|
||||
break;
|
||||
}
|
||||
if(!foundStart)
|
||||
startingPos++;
|
||||
else lastPos++;
|
||||
}
|
||||
if(!(lastPos - startingPos)) return nullptr;
|
||||
|
||||
char* trimmedString = (char*)calloc(lastPos - startingPos + 1, sizeof(char));
|
||||
memcpy(trimmedString, in + startingPos, lastPos - startingPos);
|
||||
return trimmedString;
|
||||
}
|
||||
|
||||
struct Djb12Hasher {
|
||||
size_t operator()(char* str) const {
|
||||
unsigned long hash = 5381;
|
||||
int c;
|
||||
|
||||
while (c = *str++)
|
||||
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
|
||||
|
||||
return hash;
|
||||
}
|
||||
};
|
||||
|
||||
struct StrcmpEqual {
|
||||
bool operator()(char* key1, char* key2) const {
|
||||
return (!strcmp(key1, key2));
|
||||
}
|
||||
};
|
||||
|
||||
class UserSettings {
|
||||
|
||||
public:
|
||||
UserSettings(char* path = nullptr);
|
||||
~UserSettings();
|
||||
|
||||
char* const getValue(char* key, uint64_t len = 0);
|
||||
void setValue(char* key, char* value, uint64_t valueSize, uint64_t keySize); //'\0' included
|
||||
void setValue(char* key, bool value, uint64_t keySize); //'\0' included
|
||||
//void setValue(char* key, uint64_t value, uint64_t valueSize, uint64_t keySize); //'\0' included
|
||||
|
||||
protected:
|
||||
|
||||
private:
|
||||
//void* returnValue();
|
||||
|
||||
//std::map<const char*, int> values{ {"show_channels", false}, {"test", 7} };
|
||||
std::unordered_map<char*, char*, Djb12Hasher, StrcmpEqual> values;
|
||||
char* textContents = nullptr;
|
||||
uint64_t textContentsSize = 0;
|
||||
char* pos = "true";
|
||||
char* neg = "false";
|
||||
};
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue