swwmgz_m/lore/mklang.c

209 lines
4.9 KiB
C

/*
mklang.c : Generate LANGUAGE files for lore library entries.
This code is a mess lol.
(C)2020 Marisa Kirisame, UnSX Team.
Released under the MIT license:
Copyright (c) 2020 Marisa Kirisame
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#define _XOPEN_SOURCE 700
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <strings.h>
#include <errno.h>
#include <sys/stat.h>
#include <ftw.h>
char *lang = 0;
FILE *lf = 0;
char *txt = 0;
long txtlen = 0;
char *entname = 0;
int processentry()
{
int hadtext = 0;
long txtpos = 0;
int txtnum = 0;
char *nl = 0;
int hl = 0;
gettag:
if ( txtpos > txtlen-4 ) return hadtext;
if ( !strncmp(txt+txtpos,"TAG\n",4) )
{
txtpos += 4;
goto processtag;
}
else if ( !strncmp(txt+txtpos,"TAB\n",4) )
{
txtpos += 4;
goto processtab;
}
else if ( !strncmp(txt+txtpos,"REL\n",4) )
{
txtpos += 4;
goto processrel;
}
else if ( !strncmp(txt+txtpos,"TXT\n",4) )
{
txtpos += 4;
txtnum++;
goto processtxt;
}
return hadtext;
processtag:
nl = strchr(txt+txtpos,'\n');
if ( !nl ) return hadtext;
*nl = '\0';
hadtext = 1;
fprintf(lf,"SWWM_LORETAG_%s = \"%s\";\n",entname,txt+txtpos);
txtpos = (nl-txt)+1;
goto gettag;
processtab:
nl = strchr(txt+txtpos,'\n');
if ( !nl ) return hadtext;
*nl = '\0';
hadtext = 1;
fprintf(lf,"SWWM_LORETAB_%s = \"%s\";\n",entname,txt+txtpos);
txtpos = (nl-txt)+1;
goto gettag;
processrel:
nl = strchr(txt+txtpos,'\n');
if ( !nl ) return hadtext;
*nl = '\0';
hadtext = 1;
fprintf(lf,"SWWM_LOREREL_%s = \"%s\";\n",entname,txt+txtpos);
txtpos = (nl-txt)+1;
goto gettag;
processtxt:
hl = 0;
if ( txtnum <= 1 ) fprintf(lf,"SWWM_LORETXT_%s = \"",entname);
else fprintf(lf,"SWWM_LORETXT_%s%d = \"",entname,txtnum);
while ( txt[txtpos] )
{
if ( txt[txtpos] == '\n' )
fprintf(lf,"\\n");
else if ( txt[txtpos] == '\"' )
fprintf(lf,"\\\"");
else if ( (txt[txtpos] == '*') && (txt[txtpos+1] == '*') )
{
hl = !hl;
if ( hl ) fprintf(lf,"\\cf");
else fprintf(lf,"\\c-");
txtpos++;
}
else fputc(txt[txtpos],lf);
txtpos++;
if ( (txt[txtpos] == '\n') && ((txtpos >= txtlen-1) || !strncmp(txt+txtpos+1,"TXT\n",4) || !strncmp(txt+txtpos+1,"END\n",4)) )
{
txtpos++;
break;
}
}
fprintf(lf,"\";\n");
hadtext = 1;
goto gettag;
}
static int ftw_callback( const char *path, const struct stat *st,
const int type, struct FTW *info )
{
if ( type != FTW_F )
return FTW_CONTINUE;
if ( !strstr(path,".txt") )
return FTW_CONTINUE;
FILE *f = fopen(path,"rb");
if ( !f )
return FTW_CONTINUE;
txtlen = st->st_size;
if ( txt ) txt = realloc(txt,txtlen+1);
else txt = malloc(txtlen+1);
memset(txt,0,txtlen+1);
fread(txt,1,txtlen,f);
fclose(f);
// copy over basename and strip extension
entname = strdup(path+info->base);
char *ext = strchr(entname,'.');
if ( ext ) *ext = '\0';
char *c = entname;
while ( *c )
{
// cheapass uppercase
if ( (*c >= 0x61) && (*c <= 0x7A) )
*c -= 0x20;
c++;
}
processentry();
free(entname);
free(txt);
entname = txt = 0;
return FTW_CONTINUE;
}
#define NLANGS 8
int main( void )
{
const char langs[NLANGS][16] =
{
"default",
"es",
"jp",
"ru",
"fr",
"it",
"de",
"pl"
};
const char langfiles[NLANGS][32] =
{
"../language.def_lore",
"../language.es_lore",
"../language.jp_lore",
"../language.ru_lore",
"../language.fr_lore",
"../language.it_lore",
"../language.de_lore",
"../language.pl_lore",
};
for ( int i=0; i<NLANGS; i++ )
{
// [HACKAROUND] only generate if there's no ignore file,
// because some people don't do things the right way
char ignpath[256];
snprintf(ignpath,256,"%s/.ignoreme",langs[i]);
struct stat s;
if ( !stat(ignpath,&s) ) continue;
lf = fopen(langfiles[i],"wb");
if ( !lf ) continue;
fprintf(lf,"// this file was generated by mklang, "
"do not edit directly\n");
fprintf(lf,"[%s]\n",langs[i]);
nftw(langs[i],ftw_callback,15,FTW_ACTIONRETVAL|FTW_PHYS);
fclose(lf);
}
return 0;
}