#ifndef WEBDEPLOY_CTEXT_H #define WEBDEPLOY_CTEXT_H #pragma once #include #include #include #include #define MAX_LINE_LENGTH 1024 #define MAX_CONFIGS 100 namespace CText{ typedef struct { char key[MAX_LINE_LENGTH]; char value[MAX_LINE_LENGTH]; bool isValid; } ConfigPair; inline int parseLine(char *line, ConfigPair *pair) { char *token; token = strtok(line, ":"); memset(pair->value, 0, sizeof(pair->value)); memset(pair->key, 0, sizeof(pair->key)); if (token == nullptr) { return 0; // Line is invalid } strcpy(pair->key, token); token = strtok(nullptr, "\n"); if (token == nullptr) { return 0; // No value found } strcpy(pair->value, token); pair->isValid = true; return 1; } inline void trim(char *str) { char *end; // Trim leading space while (isspace((unsigned char)*str)) str++; if (*str == 0) { // All spaces? return; } // Trim trailing space end = str + strlen(str) - 1; while (end > str && isspace((unsigned char)*end)) end--; // Write new null terminator character end[1] = '\0'; } inline bool ReadConfig(ConfigPair* configs,const char * filePath){ FILE *fp; char line[MAX_LINE_LENGTH] = {0}; int configCount = 0; fp = fopen(filePath, "r"); if (fp == nullptr) { perror("Failed to open file"); return false; } while (fgets(line, MAX_LINE_LENGTH, fp)) { // Trim leading and trailing whitespace trim(line); // Skip empty lines or comments if (line[0] == '\0' || line[0] == '#') { continue; } configs[configCount].isValid = false; if (parseLine(line, &configs[configCount])) { configCount++; } } fclose(fp); return true; } inline std::map ReadConfig(const char * filePath){ std::map config; FILE *fp; char line[MAX_LINE_LENGTH] = {0}; ConfigPair configs; fp = fopen(filePath, "r"); if (fp == nullptr) { perror("Failed to open file"); return {}; } while (fgets(line, MAX_LINE_LENGTH, fp)) { // Trim leading and trailing whitespace trim(line); // Skip empty lines or comments if (line[0] == '\0' || line[0] == '#') { continue; } configs.isValid = false; if (parseLine(line, &configs)) { config[configs.key] = configs.value; } } fclose(fp); return config; } } #endif