Debian Codegraph @

src/misc/messages.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2002-2010 The DOSBox Team
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18
19 /* $Id: messages.cpp,v 1.23 2009-06-17 08:52:35 qbix79 Exp $ */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include "dosbox.h"
25 #include "cross.h"
26 #include "support.h"
27 #include "setup.h"
28 #include "control.h"
29 #include <list>
30 #include <string>
31 using namespace std;
32
33
34
35 #define LINE_IN_MAXLEN 2048
36
37 struct MessageBlock {
38 string name;
39 string val;
40 MessageBlock(const char* _name, const char* _val):
41 name(_name),val(_val){}
42 };
43
44 static list<MessageBlock> Lang;
45 typedef list<MessageBlock>::iterator itmb;
46
47 void MSG_Add(const char * _name, const char* _val) {
48 /* Find the message */
49 for(itmb tel=Lang.begin();tel!=Lang.end();tel++) {
50 if((*tel).name==_name) {
51 // LOG_MSG("double entry for %s",_name); //Message file might be loaded before default text messages
52 return;
53 }
54 }
55 /* if the message doesn't exist add it */
56 Lang.push_back(MessageBlock(_name,_val));
57 }
58
59 void MSG_Replace(const char * _name, const char* _val) {
60 /* Find the message */
61 for(itmb tel=Lang.begin();tel!=Lang.end();tel++) {
62 if((*tel).name==_name) {
63 Lang.erase(tel);
64 break;
65 }
66 }
67 /* Even if the message doesn't exist add it */
68 Lang.push_back(MessageBlock(_name,_val));
69 }
70
71 static void LoadMessageFile(const char * fname) {
72 if (!fname) return;
73 if(*fname=='\0') return;//empty string=no languagefile
74 FILE * mfile=fopen(fname,"rt");
75 /* This should never happen and since other modules depend on this use a normal printf */
76 if (!mfile) {
77 E_Exit("MSG:Can't load messages: %s",fname);
78 }
79 char linein[LINE_IN_MAXLEN];
80 char name[LINE_IN_MAXLEN];
81 char string[LINE_IN_MAXLEN*10];
82 /* Start out with empty strings */
83 name[0]=0;string[0]=0;
84 while(fgets(linein, LINE_IN_MAXLEN, mfile)!=0) {
85 /* Parse the read line */
86 /* First remove characters 10 and 13 from the line */
87 char * parser=linein;
88 char * writer=linein;
89 while (*parser) {
90 if (*parser!=10 && *parser!=13) {
91 *writer++=*parser;
92 }
93 *parser++;
94 }
95 *writer=0;
96 /* New string name */
97 if (linein[0]==':') {
98 string[0]=0;
99 strcpy(name,linein+1);
100 /* End of string marker */
101 } else if (linein[0]=='.') {
102 /* Replace/Add the string to the internal langaugefile */
103 /* Remove last newline (marker is \n.\n) */
104 size_t ll = strlen(string);
105 if(ll && string[ll - 1] == '\n') string[ll - 1] = 0; //Second if should not be needed, but better be safe.
106 MSG_Replace(name,string);
107 } else {
108 /* Normal string to be added */
109 strcat(string,linein);
110 strcat(string,"\n");
111 }
112 }
113 fclose(mfile);
114 }
115
116 const char * MSG_Get(char const * msg) {
117 for(itmb tel=Lang.begin();tel!=Lang.end();tel++){
118 if((*tel).name==msg)
119 {
120 return (*tel).val.c_str();
121 }
122 }
123 return "Message not Found!\n";
124 }
125
126
127 void MSG_Write(const char * location) {
128 FILE* out=fopen(location,"w+t");
129 if(out==NULL) return;//maybe an error?
130 for(itmb tel=Lang.begin();tel!=Lang.end();tel++){
131 fprintf(out,":%s\n%s\n.\n",(*tel).name.c_str(),(*tel).val.c_str());
132 }
133 fclose(out);
134 }
135
136 void MSG_Init(Section_prop * section) {
137 std::string file_name;
138 if (control->cmdline->FindString("-lang",file_name,true)) {
139 LoadMessageFile(file_name.c_str());
140 } else {
141 Prop_path* pathprop = section->Get_path("language");
142 if(pathprop) LoadMessageFile(pathprop->realpath.c_str());
143 }
144 }