#include <iostream>
#include <fstream>
#include <vector>
#include <string.h>
#include <stdlib.h>
using namespace std;
typedef struct DataUnit DataUnit;
struct DataUnit {
char* key;
char* value;
DataUnit* previous;
DataUnit* next;
};
char* GetKey(char* p, char** begin, char** end) {
char* a = p;
for (; *a == ' '; a--) {
}
*end = a;
a--;
for (; *a != ' '; a--) {
}
*begin = ++a;
return *begin;
}
char* GetValue(char* p, char** begin, char** end) {
char* a = p;
for (; *a != '\"'; a++) {
}
*begin = ++a;
a++;
for (; *a != '\"'; a++) {
}
*end = a;
return *begin;
}
DataUnit* SplitString(const char* source_string, DataUnit* head) {
DataUnit* one = head;
char* str = const_cast<char*>(source_string);
char* begin = NULL;
char* end = NULL;
char* p = NULL;
do {
p = strchr(str, '=');
if (NULL == p) {
return one;
}
DataUnit* unit = (DataUnit*) malloc(sizeof(DataUnit));
memset(unit, 0, sizeof(DataUnit));
GetKey(p, &begin, &end);
unit->key = (char*) malloc(end - begin + 1);
memset(unit->key, 0, end - begin + 1);
strncpy(unit->key, begin, end - begin);
GetValue(p, &begin, &end);
unit->value = (char*) malloc(end - begin + 1);
memset(unit->value, 0, end - begin + 1);
strncpy(unit->value, begin, end - begin);
unit->previous = one;
one->next = unit;
one = unit;
str = end++;
} while (p != NULL);
return one;
}
int readConfigFile(string file_path) {
ifstream input;
input.open(file_path.c_str());
if (!input.is_open()) {
cout << "Open file: " << file_path << "fail~~" << endl;
return -1;
}
DataUnit* head = (DataUnit*) malloc(sizeof(DataUnit));
memset(head, 0, sizeof(DataUnit));
DataUnit* node = head;
string str;
while (!input.eof()) {
getline(input, str);
node = SplitString(str.c_str(), node);
}
input.close();
DataUnit one;
one.next = head->next;
for (; one.next != NULL; one = *(one.next)) {
cout << one.next->key << " " << one.next->value << endl;
}
return 0;
}
int main(int argc, char** argv) {
if (argc != 2) {
return -1;
}
string file_path = argv[1];
readConfigFile(file_path);
return 0;
}