1 /* 2 * Martian: Motor para creaci�n de videojuegos con SDL y OpenGL 3 * Copyright (C) 2007 Javier P�rez Pacheco 4 * 5 * Este motor tiene licencia Creative Commons y se permite 6 * su modificacion y utilizacion libremente siempre y cuando se 7 * cite al autor original y se comparta con la misma licencia. 8 * No se permite su uso comercial. 9 * 10 * Para mas informacion visite la web: 11 * http://creativecommons.org/licenses/by-nc-sa/2.0/es/ 12 * 13 * PROGRAMADOR 14 * Javier P�rez Pacheco 15 * Cadiz (Spain) 16 * javielinux@gmail.com 17 * 18 */ 19 #include "language.h" 20 21 namespace Martian { 22 23 /***************************** 24 ** 25 ** CLASE LANGUAGE 26 ** 27 ******************************/ 28 29 Language* Language::instance = NULL; 30 Language()31 Language::Language() { 32 } 33 GetInstance()34 Language* Language::GetInstance () { 35 if ( instance == NULL ) { 36 instance = new Language(); 37 } 38 return instance; 39 } 40 setLang(string c)41 void Language::setLang ( string c ) { 42 if (c!="") { 43 lang = c; 44 dictionary.clear(); 45 char file[128]; 46 sprintf(file, "%s/languages.xml", DATA_DIR); 47 parseXML(file, lang); 48 } 49 } 50 getText(string ref)51 string Language::getText ( string ref ) { 52 int i; 53 for (i=0; i<(int)dictionary.size(); i++) { 54 if (dictionary[i].reference == ref) { 55 return dictionary[i].text; 56 } 57 } 58 printf("Language: '%s' don't find\n", ref.c_str()); 59 return "-"; 60 } 61 getFilename(string file,string ext)62 string Language::getFilename(string file, string ext) { 63 return file + "_" + lang + "." + ext; 64 } 65 addPhrase(string ref,string txt)66 void Language::addPhrase(string ref, string txt) { 67 Phrase f; 68 f.reference = ref; 69 f.text = txt; 70 dictionary.push_back(f); 71 } 72 73 74 /***************************** 75 ** 76 ** XML 77 ** 78 ******************************/ 79 startXML(void * userData,const char * el,const char ** attr)80 static void startXML(void *userData, const char *el, const char **attr) { 81 int i; 82 DictionaryXML* data = (DictionaryXML*)userData; 83 84 if (data->inPhrase) { 85 if (strcmp(el, data->lang.c_str()) == 0) { 86 for (i = 0; attr[i]; i += 2) { 87 if (strcmp(attr[i], "value") == 0) { 88 data->text = attr[i+1]; 89 } 90 } 91 Language::GetInstance()->addPhrase(data->reference, data->text); 92 } 93 } else { 94 if (strcmp(el, "phrase") == 0) { 95 data->inPhrase = true; 96 for (i = 0; attr[i]; i += 2) { 97 if (strcmp(attr[i], "name") == 0) { 98 data->reference = attr[i+1]; 99 } 100 } 101 } 102 } 103 } 104 endXML(void * userData,const char * el)105 static void endXML(void *userData, const char *el) 106 { 107 DictionaryXML* data = (DictionaryXML*)userData; 108 109 if (strcmp(el, "phrase") == 0) { 110 data->inPhrase = false; 111 } 112 113 } 114 parseXML(char fileXML[128],string l)115 void parseXML(char fileXML[128], string l) { 116 char buffer[8192]; 117 int done; 118 119 DictionaryXML data; 120 data.lang = l; 121 data.inPhrase = false; 122 123 XML_Parser p = XML_ParserCreate("UTF-8"); 124 if (! p) { 125 printf("It could not have sufficient memory parser\n"); 126 } 127 128 XML_SetUserData(p, &data); 129 XML_SetElementHandler(p, startXML, endXML); 130 131 FILE *file = fopen(fileXML, "r"); 132 if(!file) 133 printf("Error opening file XML\n"); 134 135 do 136 { 137 size_t len = fread(buffer, 1, sizeof(buffer), file); 138 done = len < sizeof(buffer); 139 if(!XML_Parse(p, buffer, len, done)){ 140 printf("Error making the parse\n"); 141 } 142 //parse_error(&data, XML_ErrorString(XML_GetErrorCode(data.parser))); 143 } 144 while(!done); 145 fclose(file); 146 } 147 148 } 149