2007-12-21

A Small Test Program

/**************************************************************
Function:ReadIniFile (Read File Type Of Ini)
Usage:ReadIniFile ( )
An independent function, mainly uses in to read the ini files
IniFileForm:
===============================================================
[Section]
Key=Value
[Section1]
Key1=Value1
[Section2]
Key2=Value2
[Section3]
Key3=Value3
[Section4]
Key4=Value4
...
===============================================================
DevelopmentEnvironment:Linux
Author:顜禜 Date:Dec,19th,2007
***************************************************************/
#include "stdio.h"
#include "glib.h"
#include "glib/gkeyfile.h"
#include < vector >
#include "string.h"
#include "iostream"

int g_size;

/*Define a vector to load datd in it,and then use it in whole project*/
using namespace std;
vector< string > vecBTCIP;
vector< string > vecClientIPRange;
vector< string > vecLevel0;
vector< string > vecLevel1;
vector< string > vecLevel2;
/*Why in here must use "string" type?*/

int ReadIniFile(const char *filename) //Use Glib to read file
{
GError *error = NULL;
GKeyFile *key_file=g_key_file_new();

if (!g_key_file_load_from_file(key_file,filename,G_KEY_FILE_NONE,&error)) //Load File
{
printf("cannot load %s",filename);
return 1;
}
else
{
printf("load %s\n",filename,"\n");
}
g_key_file_set_list_separator(key_file,';');
int Cnt=(int)g_key_file_get_integer(key_file,"Section","Key",&error);
char *Caddr=(char *)g_key_file_get_string(key_file,"Section1","Key1",&error);
char *BTCIP=(char *)g_key_file_get_string(key_file,"Section2","Key2",&error);
gsize *length; //String list length
char** Level=(char **)g_key_file_get_string_list(key_file,"Section3","Key3",&length,NULL);
char** Level1=(char **)g_key_file_get_string_list(key_file,"Section4","Key4",&length,NULL);
char** Level2=(char **)g_key_file_get_string_list(key_file,"Section5","Key5",&length,NULL);
char** Level3=(char **)g_key_file_get_string_list(key_file,"Section6","Key6",&length,NULL);

/*printf("Cnt:%d\n",Cnt,"\n");
printf("Caddr:%s\n",Caddr,"\n");
printf("BTCIP:%s\n",BTCIP,"\n");
printf("Level:%s\n",*Level,"\n");
printf("Level1:%s\n",*Level1,"\n");
printf("Level2:%s\n",*Level2,"\n");
printf("Level3:%s\n",*Level3,"\n");*/

vecBTCIP.push_back( BTCIP);
vecClientIPRange.push_back(Caddr);
vecLevel0.push_back(*Level);
vecLevel1.push_back(*Level1);
vecLevel2.push_back(*Level2);

//cout < < vecLevel2.at(0) < < endl; //Test Taking Value

g_key_file_free(key_file);
}
int main ()
{
ReadIniFile("Filename"); //Ini Filename
}


Why In Here Vector Cannot Use Char Type,Use It Would Appear Compilation Error?
Thinking And Searching This Question.

No comments: