dosbox-wii/src/misc/messages.cpp

137 lines
3.5 KiB
C++
Raw Normal View History

2009-05-02 23:03:37 +02:00
/*
* Copyright (C) 2002 The DOSBox Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "dosbox.h"
#include "cross.h"
#include "support.h"
#include "setup.h"
2009-05-02 23:20:05 +02:00
#include <list>
#include <string>
using namespace std;
2009-05-02 23:03:37 +02:00
#define LINE_IN_MAXLEN 1024
2009-05-02 23:20:05 +02:00
struct MessageBlock {
string name;
string val;
MessageBlock(const char* _name, const char* _val):
name(_name),val(_val){}
2009-05-02 23:03:37 +02:00
};
2009-05-02 23:20:05 +02:00
static list<MessageBlock> Lang;
typedef list<MessageBlock>::iterator itmb;
2009-05-02 23:03:37 +02:00
2009-05-02 23:20:05 +02:00
void MSG_Add(const char * _name, const char* _val) {
/* Find the message */
for(itmb tel=Lang.begin();tel!=Lang.end();tel++) {
if((*tel).name==_name) {
return;
}
}
/* Even if the message doesn't exist add it */
Lang.push_back(MessageBlock(_name,_val));
}
2009-05-02 23:03:37 +02:00
2009-05-02 23:20:05 +02:00
void MSG_Replace(const char * _name, const char* _val) {
/* Find the message */
for(itmb tel=Lang.begin();tel!=Lang.end();tel++) {
if((*tel).name==_name) {
itmb teln=tel;
teln++;
Lang.erase(tel,teln);
break;
}
}
/* Even if the message doesn't exist add it */
Lang.push_back(MessageBlock(_name,_val));
}
static void LoadMessageFile(const char * fname) {
if (!fname) return;
if(*fname=='\0') return;//empty string=no languagefile
2009-05-02 23:03:37 +02:00
FILE * mfile=fopen(fname,"rb");
2009-05-02 23:20:05 +02:00
/* This should never happen and since other modules depend on this use a normal printf */
2009-05-02 23:03:37 +02:00
if (!mfile) {
2009-05-02 23:12:18 +02:00
E_Exit("MSG:Can't load messages: %s",fname);
2009-05-02 23:03:37 +02:00
}
char linein[LINE_IN_MAXLEN];
char name[LINE_IN_MAXLEN];
char string[LINE_IN_MAXLEN*10];
/* Start out with empty strings */
name[0]=0;string[0]=0;
while(fgets(linein, LINE_IN_MAXLEN, mfile)!=0) {
/* Parse the read line */
/* First remove characters 10 and 13 from the line */
char * parser=linein;
char * writer=linein;
while (*parser) {
if (*parser!=10 && *parser!=13) {
*writer++=*parser;
}
*parser++;
}
*writer=0;
/* New string name */
if (linein[0]==':') {
string[0]=0;
strcpy(name,linein+1);
/* End of string marker */
} else if (linein[0]=='.') {
2009-05-02 23:20:05 +02:00
/* Replace/Add the string to the internal langaugefile */
MSG_Replace(name,string);
2009-05-02 23:03:37 +02:00
} else {
/* Normal string to be added */
strcat(string,linein);
strcat(string,"\n");
}
}
fclose(mfile);
}
2009-05-02 23:20:05 +02:00
const char * MSG_Get(char const * msg) {
for(itmb tel=Lang.begin();tel!=Lang.end();tel++){
if((*tel).name==msg)
{
return (*tel).val.c_str();
}
2009-05-02 23:03:37 +02:00
}
2009-05-02 23:20:05 +02:00
return "Message not Found!\n";
2009-05-02 23:03:37 +02:00
}
2009-05-02 23:20:05 +02:00
void MSG_Write(const char * location) {
FILE* out=fopen(location,"w+b");
if(out==NULL) return;//maybe an error?
for(itmb tel=Lang.begin();tel!=Lang.end();tel++){
fprintf(out,":%s\n%s.\n",(*tel).name.c_str(),(*tel).val.c_str());
}
fclose(out);
}
2009-05-02 23:03:37 +02:00
2009-05-02 23:20:05 +02:00
void MSG_Init(Section_prop * section) {
std::string file_name;
if (control->cmdline->FindString("-lang",file_name)) {
LoadMessageFile(file_name.c_str());
} else LoadMessageFile(section->Get_string("language"));
2009-05-02 23:03:37 +02:00
}