#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;
};
int SplitString(const char* str, DataUnit* unit){
char* p = strchr(str, '=');
if(NULL == p){
return -1;
}
int len1 = p - str;
p = strchr(str, '\"');
if(NULL == p){
return -1;
}
int len2 = p - str;
p = strchr(p + 1, '\"');
if(NULL == p){
return -1;
}
int len3 = p - str;
unit->key = (char*)malloc(len1 + 1);
memset(unit->key, 0, len1 + 1);
strncpy(unit->key, str, len1);
unit->value = (char*)malloc(len3 - len2);
memset(unit->value, 0, len3 - len2);
strncpy(unit->value, str + len2 + 1, len3 - len2 - 1);
return 0;
}
int readConfigFile(string file_path){
vector<string> str_vec;
str_vec.clear();
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* one = (DataUnit*) malloc(sizeof(DataUnit));
memset(one, 0, sizeof(DataUnit));
one->previous = head;
one->next = NULL;
string str;
while (!input.eof()){
input>>str;
DataUnit* unit = (DataUnit*) malloc(sizeof(DataUnit));
memset(unit, 0, sizeof(DataUnit));
if(!SplitString(str.c_str(), unit)){
unit->next = one->next;
unit->previous = one->previous;
one->previous->next = unit;
one->previous = unit;
}
else{
free(unit);
}
}
input.close();
one->next = head->next;
for(; one->next != NULL; one = one->next){
cout<<one->next->key<<" "<<one->next->value<<endl;
}
free(one);
return 0;
}
int main(int argc, char** argv){
if (argc != 2){
return -1;
}
string file_path = argv[1];
readConfigFile(file_path);
return 0;
}