00001
00002
00003
00004
00005 #include <iconv.h>
00006 #include <string.h>
00007 #include "loadhashtableslocalization.h"
00008 #include "localization.h"
00009 #include "getmodules.h"
00010 #include "setgetSCIpath.h"
00011 #include "libxml/xmlreader.h"
00012 #include "../../fileio/includes/FileExist.h"
00013
00014 #define FILEERRORS "errors"
00015 #define FILEMSGS "messages"
00016 #define FILEMENUS "menus"
00017 #define FILEFORMATPATH "%s/modules/%s/languages/%s/%s.xml"
00018
00019 BOOL LoadHashTableLocalization(struct hashtable *table,char *filenamexml);
00020 static char *GetXmlFileEncoding(const char *filename);
00021 static unsigned char* ConvertEncoding(char *encodingFrom, char *encodingTo, char* inputStr);
00022
00023 BOOL LoadHashTablesLocalization(char *language)
00024 {
00025 BOOL bOK=FALSE;
00026 char *SciPath=NULL;
00027 int i=0;
00028
00029 struct hashtable *Table_Errors=NULL;
00030 struct hashtable *Table_Messages=NULL;
00031 struct hashtable *Table_Menus=NULL;
00032
00033 struct MODULESLIST *moduleslist=NULL;
00034 moduleslist=getmodules();
00035
00036 Table_Errors=GetHashTableScilabErrors();
00037 Table_Messages=GetHashTableScilabMessages();
00038 Table_Menus=GetHashTableScilabMenus();
00039
00040 SciPath=getSCIpath();
00041
00042 for(i=0;i<moduleslist->numberofModules;i++)
00043 {
00044 char *full_filename_errors=NULL;
00045 char *full_filename_messages=NULL;
00046 char *full_filename_menus=NULL;
00047
00048 full_filename_errors=(char*)MALLOC(sizeof(char)*(strlen(SciPath)+strlen(FILEFORMATPATH)+strlen(moduleslist->ModuleList[i])+strlen(FILEERRORS)+strlen(language)+1));
00049 full_filename_messages=(char*)MALLOC(sizeof(char)*(strlen(SciPath)+strlen(FILEFORMATPATH)+strlen(moduleslist->ModuleList[i])+strlen(FILEMSGS)+strlen(language)+1));
00050 full_filename_menus=(char*)MALLOC(sizeof(char)*(strlen(SciPath)+strlen(FILEFORMATPATH)+strlen(moduleslist->ModuleList[i])+strlen(FILEMENUS)+strlen(language)+1));
00051
00052 sprintf(full_filename_errors,FILEFORMATPATH,SciPath,moduleslist->ModuleList[i],language,FILEERRORS);
00053 sprintf(full_filename_messages,FILEFORMATPATH,SciPath,moduleslist->ModuleList[i],language,FILEMSGS);
00054 sprintf(full_filename_menus,FILEFORMATPATH,SciPath,moduleslist->ModuleList[i],language,FILEMENUS);
00055
00056 if (FileExist(full_filename_errors)) LoadHashTableLocalization(Table_Errors,full_filename_errors);
00057 if (FileExist(full_filename_messages)) LoadHashTableLocalization(Table_Messages,full_filename_messages);
00058 if (FileExist(full_filename_menus)) LoadHashTableLocalization(Table_Menus,full_filename_menus);
00059
00060 if (full_filename_errors) {FREE(full_filename_errors);full_filename_errors=NULL;}
00061 if (full_filename_messages) {FREE(full_filename_messages);full_filename_messages=NULL;}
00062 if (full_filename_menus) {FREE(full_filename_menus);full_filename_menus=NULL;}
00063 bOK=TRUE;
00064 }
00065
00066 if (SciPath) {FREE(SciPath); SciPath=NULL;}
00067
00068 return bOK;
00069 }
00070
00071 BOOL LoadHashTableLocalization(struct hashtable *table,char *filenamexml)
00072 {
00073 BOOL bOK=FALSE;
00074 BOOL bUTF_8_Mode=FALSE;
00075
00076 char *TAGVALUE=NULL;
00077 char *STRINGVALUE=NULL;
00078
00079 xmlTextReaderPtr reader;
00080 int ret=0;
00081 char *encoding=GetXmlFileEncoding(filenamexml);
00082
00083 if((strcmp("utf-8", encoding)==0)||(strcmp("UTF-8", encoding)==0)) bUTF_8_Mode=TRUE;
00084 else bUTF_8_Mode=FALSE;
00085
00086 reader = xmlReaderForFile(filenamexml, encoding, 0);
00087
00088 ret = xmlTextReaderRead (reader);
00089
00090 while (ret == 1)
00091 {
00092 const xmlChar *balise=NULL;
00093 xmlReaderTypes type;
00094
00095 type = xmlTextReaderNodeType (reader);
00096 if (type == XML_READER_TYPE_ELEMENT )
00097 {
00098 balise = xmlTextReaderConstName (reader);
00099
00100 if (xmlStrEqual (balise, (const xmlChar *)"tag"))
00101 {
00102 ret = xmlTextReaderRead (reader);
00103 type = xmlTextReaderNodeType (reader);
00104 if (type == XML_READER_TYPE_TEXT )
00105 {
00106 const xmlChar *node_value;
00107 if (bUTF_8_Mode)
00108 {
00109 node_value=(const xmlChar *)xmlTextReaderConstValue(reader);
00110 }
00111 else
00112 {
00113 node_value=ConvertEncoding("UTF-8",encoding,(char *)xmlTextReaderConstValue(reader));
00114 }
00115
00116 TAGVALUE=(char*)node_value;
00117 }
00118 }
00119
00120 if (xmlStrEqual (balise, (const xmlChar *)"string"))
00121 {
00122 ret = xmlTextReaderRead (reader);
00123 type = xmlTextReaderNodeType (reader);
00124 if (type == XML_READER_TYPE_TEXT )
00125 {
00126 const xmlChar *node_value;
00127 if (bUTF_8_Mode)
00128 {
00129 node_value=(const xmlChar *)xmlTextReaderConstValue(reader);
00130 }
00131 else
00132 {
00133 node_value=ConvertEncoding("UTF-8",encoding,(char *)xmlTextReaderConstValue(reader));
00134 }
00135
00136 STRINGVALUE=(char*)node_value;
00137 }
00138 }
00139 }
00140
00141 if ( (TAGVALUE) && (STRINGVALUE))
00142 {
00143
00144 if ( (strlen(TAGVALUE)>0) & (strlen(STRINGVALUE)>0) )
00145 {
00146 AppendHashTableLocalization(table,TAGVALUE,STRINGVALUE);
00147 }
00148 if (bUTF_8_Mode)
00149 {
00150 TAGVALUE=NULL;
00151 STRINGVALUE=NULL;
00152 }
00153 else
00154 {
00155 FREE(TAGVALUE);
00156 FREE(STRINGVALUE);
00157 TAGVALUE=NULL;
00158 STRINGVALUE=NULL;
00159 }
00160 }
00161
00162 ret = xmlTextReaderRead (reader);
00163 }
00164
00165 if (bUTF_8_Mode)
00166 {
00167 TAGVALUE=NULL;
00168 STRINGVALUE=NULL;
00169 }
00170 else
00171 {
00172 FREE(TAGVALUE);
00173 FREE(STRINGVALUE);
00174 TAGVALUE=NULL;
00175 STRINGVALUE=NULL;
00176 }
00177
00178 xmlFreeTextReader(reader);
00179
00180
00181
00182 xmlCleanupParser();
00183
00184 return bOK;
00185 }
00186
00187 static char *GetXmlFileEncoding(const char *filename)
00188 {
00189 FILE *stream;
00190 char *encoding;
00191 encoding=(char *)MALLOC(sizeof(char)*32);
00192
00193 if( (stream = fopen(filename, "r" )) != NULL )
00194 {
00195 char FirstLine[256];
00196 if( fgets( FirstLine, 256, stream ) == NULL)
00197 {
00198 printf( "fgets error\n" );
00199 return NULL;
00200 }
00201 else
00202 {
00203 char *pEncodingStart;
00204 char *pQuotationStart;
00205 char *pQuotationEnd;
00206 char *pTemp;
00207 size_t length;
00208
00209 pEncodingStart=strstr(FirstLine, "encoding");
00210 pQuotationStart=strchr(pEncodingStart,'"');
00211 pTemp=pQuotationStart+1;
00212 pQuotationEnd=strchr(pTemp,'"');
00213 length=pQuotationEnd-pTemp;
00214 strncpy(encoding, pTemp, length);
00215 strcpy(encoding+length,"\0");
00216 }
00217 fclose( stream );
00218 }
00219 else
00220 {
00221 printf( "ERROR:xmlfile %s was not opened or xmlfile %s doesn't exist\n", filename, filename);
00222 }
00223
00224 if(encoding==NULL)
00225 {
00226 strcpy(encoding,"utf-8");
00227 }
00228 return encoding;
00229
00230 }
00231
00232 static unsigned char* ConvertEncoding(char *encodingFrom, char *encodingTo, char* inputStr)
00233 {
00234 unsigned char *strBufOut=NULL;
00235 unsigned char *outputStr=NULL;
00236 size_t inputLen, outputLen, result;
00237 iconv_t c_pt;
00238
00239 strBufOut=(unsigned char *)MALLOC(sizeof(unsigned char)*2048);
00240
00241 if ((c_pt = iconv_open(encodingTo, encodingFrom)) == (iconv_t)(-1))
00242 {
00243 printf("iconv_open failed!\n");
00244 return NULL;
00245 }
00246
00247 iconv(c_pt, NULL, NULL, NULL, NULL);
00248
00249 inputLen = strlen(inputStr) + 1;
00250 outputLen = 2048;
00251 outputStr = strBufOut;
00252 result = iconv(c_pt, &inputStr, &inputLen, (char**)(&outputStr), &outputLen);
00253
00254 if (result == -1)
00255 {
00256 return NULL;
00257 }
00258
00259 iconv_close(c_pt);
00260 return strBufOut;
00261 }
00262