// originally part of KeyTrans.cpp //
/////////////////////////////////////////////////////////
+#include "tmapldr.h"
+#include "tnconfig.h"
+
#ifdef __BORLANDC__
#include <fstream.h>
#else
#include <fstream>
#endif
-#include "tmapldr.h"
-#include "tnerror.h"
-#include "tnconfig.h"
-
// It's probably a good idea to turn off the "identifier was truncated" warning
// in MSVC (Paul Brannan 5/25/98)
#ifdef _MSC_VER
// AVS
// skip inline comments, empty lines
static char * getline(istream& i, char* buf, int size){
-
+
int len = 0;
-
+
while (1) {
memset(buf,0,size);
if (i.eof()) break;
i.getline(buf,size,'\n');
-
+
while (buf[len]) {
if ( /*(buf[len]>=0) &&*/ buf[len]< ' ' ) buf[len] = ' ';
len++;
};
len = 0;
-
+
// not so fast, but work ;)
while ( buf[len] ) {
if ( (buf[len] == ' ') && (buf[len+1] == ' ')) {
memmove(buf+len, buf+len+1, strlen(buf+len));
} else len++;
};
-
+
if (buf[0] == ' ') memmove(buf, buf+1, size-1);
-
+
// empty or comment
if ((buf[0]==0)||(buf[0]==';')) continue;
-
+
len = 0; // look for comment like this one
while (buf[len])
if ((buf[len] == '/') && (buf[len+1] == '/')) buf[len] = 0;
else len++;
-
+
if (len && (buf[len-1] == ' ')) {
len--;
buf[len]=0;
};
// in case for comment like this one (in line just a comment)
if (buf[0]==0) continue;
-
+
break;
};
return (buf);
//AVS
// use string as FIFO queue for lines
static int getline(string&str, char* buf, size_t sz) {
-
+
if ( !str.length() ) return 0;
const char * p = strchr(str.c_str(),'\n');
unsigned int len; // Changed to unsigned (Paul Brannan 6/23/98)
len = str.length();
else
len = p - str.c_str();
-
+
len = len<sz?len:sz-1;
-
+
strncpy(buf,str.c_str(), len);
buf[len]=0;
// DJGPP also uses erase rather than remove (Paul Brannan 6/23/98)
unsigned char retval = 0;
int base = 10;
int readed = 0;
-
+
if ( (*str == 'x') || (*str == 'X') ) {
base = 16;
readed++;
};
-
+
while (readed != 3 && str[readed]) {
unsigned char ch = toupper(str[readed]);
if ( isdigit(ch) ) {
if (stricmp(Next_Token, "SCROLLLOCK") == 0) return SCROLLLOCK_ON;
if (stricmp(Next_Token, "CAPSLOCK" ) == 0) return CAPSLOCK_ON;
if (stricmp(Next_Token, "ENHANCED" ) == 0) return ENHANCED_KEY;
-
+
// Paul Brannan 5/27/98
if (stricmp(Next_Token, "APP_KEY" ) == 0) return APP_KEY;
// Paul Brannan 6/28/98
if (stricmp(Next_Token, "APP3_KEY" ) == 0) return APP3_KEY;
// Paul Brannan 12/9/98
if (stricmp(Next_Token, "APP4_KEY" ) == 0) return APP4_KEY;
-
+
return 0;
}
char* Fix_Tok(char * tok) {
static char s[256];
int i,j,n;
-
+
// setmem is nonstandard; memset is standard (Paul Brannan 5/25/98)
memset(s, 0, 256);
// setmem(s, 256, 0);
// load globals to 'globals'
// in buf must be a [global] part of input file
int TMapLoader::LoadGlobal(string& buf) {
-
+
char wbuf[128];
while ( buf.length() ) {
wbuf[0]=0;
if ( wbuf[0]==0 ) break;
char* Name = strtok(wbuf, TOKEN_DELIMITERS);
if ( stricmp(Name, "[global]")==0 ) continue;
-
+
char* Value = strtok(NULL, TOKEN_DELIMITERS);
if ( Value == NULL ) {
// cerr << "[global] -> no value for " << Name << endl;
strcpy(wbuf,buf);
char* ptr = strtok(wbuf, TOKEN_DELIMITERS);
if ( ptr == NULL ) return NULL;
-
+
int i = KeyTrans.LookOnGlobal(ptr);
if ( i == INT_MAX ) return NULL;
-
+
vk_code = KeyTrans.GetGlobalCode(i);
-
+
control = 0;
DWORD st;
while (1) {
if ((ptr == NULL) || ((st = Fix_ControlKeyState(ptr)) == 0)) break;
control |= st;
};
-
+
if ( ptr == NULL ) return NULL;
-
+
return Fix_Tok(ptr);
};
// load keymap to current map
// be aware - buf must passed by value, its destroyed
int TMapLoader::LoadKeyMap(string buf) {
-
+
char wbuf[128];
WORD vk_code;
DWORD control;
if (!getline(buf,wbuf,sizeof(wbuf))) break;
if ( wbuf[0]==0 ) break;
if ( strnicmp(wbuf,"[keymap",7)==0 ) continue;
-
+
char * keydef = ParseKeyDef(wbuf,vk_code,control);
if ( keydef != NULL ) {
if(!KeyTrans.AddKeyDef(vk_code, control, TN_CR)) return 0;
} else if(!strnicmp(keydef, "\\tn_crlf", strlen("\\tn_crlf"))) {
if(!KeyTrans.AddKeyDef(vk_code, control, TN_CRLF)) return 0;
- } else
+ } else
if(!KeyTrans.AddKeyDef(vk_code,control,keydef)) return 0;
// else DeleteKeyDef() ???? - I'm not sure...
}
char wbuf[128];
char charmapname[128];
charmapname[0] = 0;
-
+
// xlat.init(); now it done by KeyTranslator::Load()
-
+
while ( buf.length() ) {
wbuf[0]=0;
if (!getline(buf,wbuf,sizeof(wbuf))) break;
};
char * host = strtok(wbuf, " ");
char * console = strtok(NULL, " ");
-
+
int bHost;
int bConsole;
-
+
if ( host == NULL || console == NULL ) {
// cerr << charmapname << " -> Bad structure" << endl;
printm(0, FALSE, MSG_KEYBADSTRUCT, charmapname);
bHost = getbyte(host+1);
else
bHost = (unsigned char)host[0];
-
+
if ( strlen(console) > 1 && console[0] == '\\' )
bConsole = getbyte(console+1);
else
bConsole = (unsigned char)console[0];
-
+
if ( bHost <= 0 || bConsole <= 0 ) {
// cerr << charmapname << " -> Bad chars? "
// << host << " -> " << console << endl;
// ignore long comment [comment] ... [end comment]
// recursive!
int getLongComment(istream& is, char* wbuf, size_t sz) {
-
+
int bufLen;
while ( is ) {
wbuf[0] = 0;
bufLen = strlen(wbuf);
if ( wbuf[0] == '[' && wbuf[bufLen-1] == ']' ) {
string temps(wbuf);
-
+
if (!normalizeSplitter(temps)) {
// cerr << "Unexpected line '" << temps << "'\n";
printm(0, FALSE, MSG_KEYUNEXPLINE, temps.c_str());
int TMapLoader::Load(const char * filename, const char * szActiveEmul) {
char buf[256];
int bufLen;
-
+
ifstream inpfile(filename);
KeyTrans.DeleteAllDefs();
Charmap.init();
-
+
// it is an array for store [...] ... [end ...] parts from file
stringArray SA(0,0,sizeof(string));
int AllOk = 0;
-
+
while ( inpfile ) {
-
+
getline(inpfile, buf, 255);
bufLen = strlen(buf);
if ( !bufLen ) continue;
-
+
if ( buf[0] == '[' && buf[bufLen-1] == ']' ) {
// is a part splitter [...]
string temps(buf);
-
+
if (!normalizeSplitter(temps)) {
printm(0, FALSE, MSG_KEYUNEXPLINE, temps.c_str());
AllOk = 0;
#endif
continue;
};
-
-
+
+
string back = temps;
// prepare line for make it as [end ...]
// and check it
printm(0, FALSE, MSG_KEYUNEXPTOK, back.c_str());
break;
};
-
+
back.insert(1,"END "); // now it looks like [END ...]
#ifdef KEYDEBUG
printit(temps.c_str());
#endif
-
+
int ok = 0;
// fetch it to temps
while ( 1 ) {
if ( buf[0] == '[' && buf[bufLen-1] == ']' ) {
string t(buf);
if ( !normalizeSplitter(t) ) break;
-
+
if ( stricmp(t.c_str(),back.c_str()) == 0 ) {
ok = 1;
break;
};
-
+
// AVS 31.12.97 fix [comment] block inside another block
if ( stricmp(t.c_str(),"[comment]") == 0 &&
getLongComment(inpfile, buf, sizeof(buf)) ) continue;
-
+
break;
};
temps += "\n";
break;
};
};
-
+
inpfile.close();
-
+
if ( !AllOk ) return 0;
-
+
// now all file are in SA, comments are stripped
-
+
int i = LookForPart(SA, "global", "");
if ( i == INT_MAX ) {
// cerr << "No [GLOBAL] definition!" << endl;
if ( !LoadGlobal(SA[i]) ) {
return 0;
};
-
+
// look for need configuration
i = LookForPart(SA, "config", szActiveEmul);
if ( i == INT_MAX ) {
// cerr << "use configuration: " << szActiveEmul << endl;
printm(0, FALSE, MSG_KEYUSECONFIG, szActiveEmul);
BOOL hadKeys = FALSE;
-
+
string config = SA[i];
// parse it
while ( config.length() ) {
printit("\t"); printit(buf); printit("\n");
char * mapdef = strtok(buf,":");
char * switchKey = strtok(NULL,"\n");
-
+
if ( !KeyTrans.mapArray.IsEmpty() && switchKey == NULL ) {
// cerr << "no switch Key for '" << mapdef
// << "'" << endl;
hadKeys = LoadKeyMap(SA[i]); // load it
};
};
-
+
}
else if ( strnicmp(buf,"charmap",7) == 0 ) {
printit("\t"); printit(buf); printit("\n");
Charmap.init();
};
/* strtok(buf," ");
-
+
char* name = strtok(NULL," ");
if ( name == NULL ) {
cerr << "No name for CHARMAP" << endl;