JSON *ParseJSONFromFile(const char* file_name) { FILE *fp = NULL; char *data = NULL; JSON *rtn = NULL; long l_size = 0; char *buffer = NULL; size_t result = 0; fp = fopen(file_name, "r"); if (fp == NULL) { printf("Exception: Cannot open file \"%s\".\n", file_name); exit(1); } /*Get the size of the file*/ fseek(fp, 0, SEEK_END); l_size = ftell(fp); rewind(fp); buffer = (char *)malloc(sizeof(char) * l_size); if (buffer == NULL) { printf("Exception: Memory error.\n"); exit(2); } result = fread(buffer, 1, l_size, fp); if (result != l_size) { printf("Exception: Reading error.\n"); exit(3); } char *tmp = DeleteSpaces(buffer); free(buffer); buffer = tmp; rtn = ParseJSON(buffer); free(buffer); fclose(fp); return rtn; }
//öppna filen och lagra värdena i en std::Map bool ConfigManager::ReadFile(const std::string &FileName) { //Stream that reads through the file std::ifstream stream; //Open the stream stream.open(m_sdirectory + FileName); //Validation if the stream was able to open the file if (!stream.is_open()) { return false; } //A dynamic array to store all the lines in the file std::vector<std::string>FileLines; //Segment the files content into seperate lines if (stream.is_open()) { while (!stream.eof()) { std::string Line; std::getline(stream, Line, '\n'); FileLines.push_back(Line); } //If there were no lines, then just skip if (FileLines.size() < 1) { } //Otherwise read through the lines one by one and look for "=" signs else { for (unsigned int i = 0; i < FileLines.size(); i++) { std::string Key; std::string Value; std::size_t WordLength = FileLines[i].find('='); //If there was a "=" sign on that line if (WordLength != std::string::npos) { //take away any possible spaces FileLines[i] = DeleteSpaces(FileLines[i]); std::size_t WordLength = FileLines[i].find('='); //Key is the string from position 0 to the position of the "=" sign Key = FileLines[i].substr(0, WordLength); //Value is the string from the "=" sign and to the end of the line Value = FileLines[i].substr(WordLength + 1); //Insert the Key and the Value in the map if (Value.size() > 0 && Key.size() > 0) FileValues.insert(std::pair<std::string, std::string>(Key, Value)); else Log::Error("Either the key or the value was missing in file: " + FileName + ", line: " + std::to_string(i)); } } } } //Close the stream stream.close(); return true; }
JSON *ParseJSON(const char *value) { JSON *rtn = NULL; StrSlice *ss = NULL; StrSlice *iter = NULL; int len = strlen(value); int index = 0; char *value_deletespace = DeleteSpaces(value); switch(value[0]) { case '-': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '0': //To number return CreateNumber(FormatNumber(value_deletespace)); free(value_deletespace); break; case '\"': // To string return CreateString(FormatString(value)); break; case '{': rtn = CreateObject(); ss = GetObjectSlices(value); iter = ss; if (ss->length == 0) { DeleteStrSlice(ss); return rtn; } while (iter != NULL) { AddItemToObject(rtn, FormatString(iter->str), ParseJSON(DeleteSpaces(iter->next->str))); iter = iter->next->next; } DeleteStrSlice(ss); return rtn; break; case '[': rtn = CreateArray(); ss = GetArraySlices(value); iter = ss; if (ss->length == 0) { DeleteStrSlice(ss); return rtn; } while (iter != NULL) { AddItemToArray(rtn, ParseJSON(DeleteSpaces(iter->str))); iter = iter->next; } DeleteStrSlice(ss); return rtn; break; case 'n': if (strcmp(value_deletespace, "null") == 0) { free(value_deletespace); return CreateNULL(); } else { printf("Exception: Invalid Syntax \"%s\"", value); return NULL; } case 't': if (strcmp(value_deletespace, "true") == 0) { free(value_deletespace); return CreateTrue(); } else { printf("Exception: Invalid Syntax \"%s\"", value); return NULL; } break; case 'f': if (strcmp(value_deletespace, "false") == 0) { free(value_deletespace); return CreateFalse(); } else { printf("Exception: Invalid Syntax \"%s\"", value); return NULL; } break; } }