71 lines
1.8 KiB
C++
71 lines
1.8 KiB
C++
#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:
|
|
static UserSettings* createSettings(const char* path = nullptr, bool create = false);
|
|
~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
|
|
|
|
bool save(const char* path);
|
|
protected:
|
|
|
|
private:
|
|
UserSettings(char* text = nullptr);
|
|
std::unordered_map<char*, char*, Djb12Hasher, StrcmpEqual> values;
|
|
char* pos = "true";
|
|
char* neg = "false";
|
|
};
|
|
|
|
}
|