BaseConfigFile.cpp

См. документацию.
00001 #include <string>
00002 
00003 #include "BaseConfigFile.h"
00004 /*BaseConfigFile::BaseConfigFile()
00005 {
00006 }*/
00007 #include <stdio.h>
00008 string BaseConfigFile::xfCharToStr(xfChar* str){
00009     
00010     //printf("ORIGINAL STRING!!! %S\n",str);
00011     wstring ws = str;
00012     //cout<<"string data:"<<ws<<endl;
00013     /*char* n=(char*)(str);
00014     cout<<"========================"<<endl;    
00015     for (int a=0; a<xfStrlen(str)*4;++a){
00016         printf("-%d",*n);
00017         n++;
00018     }
00019     cout<<"========================"<<endl;
00020     //setlocale(LC_CTYPE, "");
00021     int nstrLen= 4 * (xfStrlen(str) + 1);
00022     xfNChar* dst=static_cast<xfNChar*> (malloc(nstrLen));
00023     xfConvertToUTF8(str,dst);
00024     printf("Нечто :%s\n",dst);
00025     string sss(dst);
00026     free(dst);*/
00027     //wcout<<"SSS1:"<<wstring(str)<<endl;
00028     //cout<<"SSS:"<<sss<<endl;
00029     string sss(ws.begin(), ws.end());
00030     return sss;
00031 }
00032 
00033 long int BaseConfigFile::xfCharToInt(xfChar* str){
00034     return xfDecToInt(str);
00035 }
00036 
00037 long int BaseConfigFile::getAttrInt(xfNode* node, long int defval){
00038     if(node==NULL){return defval;}
00039     return xfCharToInt(xfGet(node));
00040 }
00041 
00042 string BaseConfigFile::getAttrString(xfNode* node, string defval){
00043     if(node==NULL){return defval;}
00044     return xfCharToStr(xfGet(node));
00045 }
00046 
00047 BaseConfigFile::BaseConfigFile(char* filename){
00048         xfStart();
00049         conf=xfCreate();
00050         //xfSetDocEncoding(conf,XF_ENC_UTF8);
00051         int r = xfReadFile(conf,filename);
00052 
00053         if (r) {
00054           if (r == XFE_IO) printf("xflib_conf: cannot open file '%s'\n",filename);
00055           if (r == XFE_MEMORY) printf("xflib_conf: not enough memory\n");
00056           if (r == XFE_FORMAT) {
00057             printf("xflib_conf: error parsing file '%s': %S (line %ld col %ld)\n",
00058                         filename,xfGetError(conf),xfGetErrorLine(conf),xfGetErrorCol(conf));
00059           }
00060           xfDestroy(conf);
00061           exit(1);
00062         }
00063         root=xfEnter(conf->root,L"conf");
00064         moduledata_node=xfEnter(root,L"module_data");
00065 }
00066 
00067 xfNode* BaseConfigFile::getMofuleAttribute(wstring modulename, wstring attr){
00068     xfNode* moduleN= xfEnter(moduledata_node,const_cast<xfChar*>(modulename.c_str()));
00069     if (moduleN==NULL){return NULL;}
00070     return xfEnter(moduleN,const_cast<xfChar*>(attr.c_str()));
00071 }
00072 
00073 string BaseConfigFile::getModuleAttributeString(wstring modulename, wstring attr, string defval){
00074     return getAttrString(getMofuleAttribute(modulename,attr),defval);
00075 }
00076 
00077 long int BaseConfigFile::getModuleAttributeInt(wstring modulename, wstring attr,long int defval){
00078     return getAttrInt(getMofuleAttribute(modulename,attr),defval);
00079 }
00080 
00081 BaseConfigFile::~BaseConfigFile(){
00082     xfDestroy(conf);
00083     xfStop();
00084 }