Archived
1
0
Fork 0
This repository has been archived on 2025-09-02. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
skcblitz/locale.h

151 lines
2.8 KiB
C
Raw Permalink Normal View History

2025-09-02 14:43:36 +02:00
#ifdef LOCALE_LOADER
#define STR(v, f, d) loc_set(&sys.str_table[n*LOCALE_ELEM], STRINGIZE(v), sys.str_format[n], smap_get(map, STRINGIZE(v))); n++;
#undef LOCALE_LOADER
void loc_read(smap_t *map);
void loc_load(const char *filename) {
char *lines;
char *line = NULL;
smap_t map;
int pos;
if(!file_sread(&lines, filename)) {
return;
}
smap_init(&map, 8, MEM_SYSTEM);
while(line = strtok(line ? NULL : lines, "\n")) {
for(pos = 0; line[pos]; pos++) {
if(line[pos] == '=')
break;
}
if(line[pos]) {
line[pos++] = 0;
smap_put(&map, line, line+pos);
}
}
loc_read(&map);
smap_clear(&map);
mem_free(lines);
}
void loc_read(smap_t *map) {
#endif
#ifdef LOCALE_DEFAULTS
#define STR(v, f, d) loc_set(&sys.str_table[n*LOCALE_ELEM], STRINGIZE(v), sys.str_format[n], d); n++;
#undef LOCALE_DEFAULTS
#define LOCALE_LOADER
void loc_def() {
#endif
#ifdef LOCALE_VARS
#define STR(v, f, d) STR_ ## v = &sys.str_table[n*LOCALE_ELEM]; sys.str_format[n++] = f;
#undef LOCALE_VARS
#define LOCALE_DEFAULTS
void loc_init() {
#endif
#ifndef LOCALE_BASE
#define STR(v, f, d) const char * STR_ ## v;
uint file_read_hdr(byte **data, const char *filename, const char *hdr, uint hdrsize, byte flags);
void loc_set(char *str, const char *name, const char *fmt, const char *loc) {
byte fstr[16];
byte pos = 0;
byte lpos = 0;
byte narg = 0;
char c;
if((!loc) || (loc[0] == 0)) {
sprintf(str, fmt[0] ? "$%s$ [%s]" : "$%s$", name, fmt);
return;
}
while(c = fmt[pos++]) {
if(c == ' ') {
fstr[narg++] = lpos;
lpos = pos;
}
}
if(fmt[lpos]) {
fstr[narg++] = lpos;
}
pos = lpos = 0;
while(c = *(loc++)) {
if(pos == LOCALE_ELEM - 1)
break;
if(lpos) {
if((c >= '1') && (c <= '9')) {
lpos = 1 + c - '1';
}
else if((c >= 'a') && (c <= 'g')) {
lpos = 10 + c - 'a';
}
else if((c >= 'A') && (c <= 'G')) {
lpos = 10 + c - 'A';
}
else {
lpos = 0;
}
if(lpos && (lpos <= narg)) {
if(pos >= LOCALE_ELEM - ((lpos < 10) ? 4 : 5))
break;
str[pos++] = '%';
str[pos++] = (lpos < 10) ? ('0' + lpos) : '1';
if(lpos >= 10)
str[pos++] = ('0' + (lpos - 10));
str[pos++] = '$';
for(lpos = fstr[lpos-1];; lpos++) {
c = fmt[lpos];
if((c == 0) || (c == ' ')) {
break;
}
else if(pos == LOCALE_ELEM - 1) {
while(str[--pos] != '%') {
;
}
str[pos] = 0;
return;
}
str[pos++] = c;
}
lpos = 0;
continue;
}
}
else if(c == '$') {
lpos = 1;
continue;
}
if(c == '%') {
if(pos == LOCALE_ELEM - 2)
break;
str[pos++] = '%';
str[pos++] = '%';
}
else {
str[pos++] = c;
}
}
str[pos] = 0;
}
#define LOCALE_VARS
#else
int n = 0;
#endif
#include "strings.h"
#ifdef LOCALE_BASE
}
#else
#define LOCALE_BASE
#endif
#undef STR