File diff r221:56c7ed3a0581 → r222:4409829eb08f
strgen/strgen.c
Show inline comments
 
@@ -34,25 +34,25 @@ typedef struct {
 
} LanguagePackHeader;
 

	
 
typedef struct CmdStruct {
 
	const char *cmd;
 
	ParseCmdProc proc;
 
	long value;
 
} CmdStruct;
 

	
 
typedef struct LineName {
 
	struct LineName *hash_next;
 
	int value;
 
	char str[1];
 
} LineName; 
 
} LineName;
 

	
 
int _cur_line;
 
bool _warnings;
 

	
 
uint32 _hash;
 
char _lang_name[32], _lang_ownname[32];
 

	
 
#define HASH_SIZE 1023
 
LineName *_hash_head[HASH_SIZE];
 
unsigned int hash_str(const char *s) {
 
	unsigned int hash = 0;
 
	for(;*s;s++)
 
@@ -209,57 +209,57 @@ static const CmdStruct _cmd_structs[] = 
 
	// Update position
 
	{"SETX", EmitSetX, 1},
 
	{"SETXY", EmitSetXY, 2},
 

	
 
	// Font size
 
	{"TINYFONT", EmitSingleByte, 8},
 
	{"BIGFONT", EmitSingleByte, 9},
 

	
 
	// New line
 
	{"", EmitSingleByte, 10},
 

	
 
	// Colors
 
	{"BLUE", EmitSingleByte, 15},		
 
	{"BLUE", EmitSingleByte, 15},
 
	{"SILVER", EmitSingleByte, 16},
 
	{"GOLD", EmitSingleByte, 17},
 
	{"RED", EmitSingleByte, 18},
 
	{"PURPLE", EmitSingleByte, 19},
 
	{"LTBROWN", EmitSingleByte, 20},
 
	{"ORANGE", EmitSingleByte, 21},
 
	{"GREEN", EmitSingleByte, 22},
 
	{"YELLOW", EmitSingleByte, 23},
 
	{"DKGREEN", EmitSingleByte, 24},
 
	{"CREAM", EmitSingleByte, 25},
 
	{"BROWN", EmitSingleByte, 26},
 
	{"WHITE", EmitSingleByte, 27},
 
	{"LTBLUE", EmitSingleByte, 28},
 
	{"GRAY", EmitSingleByte, 29},
 
	{"DKBLUE", EmitSingleByte, 30},
 
	{"BLACK", EmitSingleByte, 31},
 
	
 

	
 
	// 0x7B=123 is the LAST special character we may use.
 

	
 
	// Numbers
 
	{"COMMA32", EmitSingleByte, 0x7B},
 
	{"COMMA16", EmitSingleByte, 0x7C},
 
	{"COMMA8", EmitSingleByte, 0x7D},
 
	{"NUMU16", EmitSingleByte, 0x7E},
 

	
 
	{"CURRENCY", EmitSingleByte, 0x7F},
 
	
 

	
 
	{"CURRCOMPACT", EmitEscapedByte, 0}, // compact currency
 
	{"INT32", EmitEscapedByte, 1}, // compact currency
 
	{"REV", EmitEscapedByte, 2}, // openttd revision string
 
	{"SHORTCARGO", EmitEscapedByte, 3}, // short cargo description, only ### tons, or ### litres
 
	
 

	
 
	{"STRINL", EmitStringInl, 0x81},
 

	
 
	{"DATE_LONG", EmitSingleByte, 0x82},
 
	{"DATE_SHORT", EmitSingleByte, 0x83},
 

	
 
	{"VELOCITY", EmitSingleByte, 0x84},
 
	{"SKIP16", EmitSingleByte, 0x85},
 
	{"SKIP", EmitSingleByte, 0x86},
 
	{"VOLUME", EmitSingleByte, 0x87},
 

	
 
	{"STRING", EmitSingleByte, 0x88},
 

	
 
@@ -379,25 +379,25 @@ bool check_commands_match(char *a, char 
 
		} while (br != NULL && (unsigned long)br <= 255);
 

	
 
		// make sure they are identical
 
		if (ar != br) return false;
 
	} while (ar);
 

	
 
	return true;
 
}
 

	
 
void handle_string(char *str, bool master) {
 
	char *s,*t,*r;
 
	int ent;
 
		
 

	
 
	if (*str == '#') {
 
		if (str[1] == '#' && str[2] != '#')
 
			handle_pragma(str + 2);
 
		return;
 
	}
 

	
 
	// Ignore comments & blank lines
 
	if (*str == ';' || *str == ' ' || *str == 0)
 
		return;
 

	
 
	s = strchr(str, ':');
 
	if (s == NULL) {
 
@@ -462,35 +462,35 @@ void handle_string(char *str, bool maste
 
			allstr[ent] = r;
 
		}
 
	}
 
}
 

	
 
uint32 my_hash_str(uint32 hash, const char *s)
 
{
 
	for(;*s;s++) {
 
		hash = ((hash << 3) | (hash >> 29)) ^ *s;
 
		if (hash & 1) hash = (hash>>1) ^ 0xDEADBEEF; else hash >>= 1;
 
	}
 
	return hash;
 
	
 

	
 
}
 

	
 
void parse_file(const char *file, bool english) {
 
	char buf[2048];
 
	int i;
 
	FILE *in;
 

	
 
	in = fopen(file, "r");
 
	if (in == NULL) { error("Cannot open file '%s'", file); }
 
	
 

	
 
	_cur_line = 1;
 
	while (fgets(buf, sizeof(buf),in) != NULL) {
 
		i = strlen(buf);
 
		while (i>0 && (buf[i-1]=='\r' || buf[i-1]=='\n' || buf[i-1] == ' ')) i--;
 
		buf[i] = 0;
 

	
 
		handle_string(buf, english);
 
		_cur_line++;
 
	}
 

	
 
	fclose(in);
 

	
 
@@ -503,25 +503,25 @@ void parse_file(const char *file, bool e
 
			if ((s=allstr[i]) != NULL) {
 
				hash ^= i * 0x717239;
 
				if (hash & 1) hash = (hash>>1) ^ 0xDEADBEEF; else hash >>= 1;
 
				hash = my_hash_str(hash, s + 1);
 

	
 
				s = s + 2 + strlen(s + 1);
 
				while ((cs = parse_command_string(&s, buf, NULL)) != 0) {
 
					if ( (uint)cs >= 256) {
 
						hash ^= (cs - _cmd_structs) * 0x1234567;
 
						if (hash & 1) hash = (hash>>1) ^ 0xF00BAA4; else hash >>= 1;
 
					}
 
				}
 
			}	
 
			}
 
		}
 
		_hash = hash;
 
	}
 
}
 

	
 
int count_inuse(int grp) {
 
	int i;
 

	
 
	for(i=0x800; --i >= 0;) {
 
		if (allstr[(grp<<11)+i] != NULL)
 
			break;
 
	}
 
@@ -570,25 +570,25 @@ void gen_output(FILE *f) {
 
		int n = count_inuse(i);
 
		in_use[i] = n;
 
		in_use_file[i] = TO_LE16(n);
 
		tot_str += n;
 
	}
 

	
 
	fwrite(in_use_file, 32*sizeof(uint16), 1, f);
 

	
 
	for(i=0; i!=32; i++) {
 
		for(j=0; j!=in_use[i]; j++) {
 
			char *s = allstr[(i<<11)+j];
 
			if (s == NULL) error("Internal error, s==NULL");
 
			
 

	
 
			write_length(f, *(uint16*)s);
 
			fwrite(s + sizeof(uint16), *(uint16*)s , 1, f);
 
			tot_str--;
 
		}
 
	}
 

	
 
	fputc(0, f); // write trailing nul character.
 

	
 
	if (tot_str != 0) {
 
		error("Internal error, tot_str != 0");
 
	}
 
}
 
@@ -633,33 +633,33 @@ void write_strings_h(const char *filenam
 
	if (out == NULL) { error("can't open tmp.xxx"); }
 

	
 
	fprintf(out, "enum {");
 

	
 
	lastgrp = 0;
 

	
 
	for(i = 0; i!=65536; i++) {
 
		if (allstr[i]) {
 
			if (lastgrp != (i >> 11)) {
 
				lastgrp = (i >> 11);
 
				fprintf(out, "};\n\nenum {");
 
			}
 
			
 

	
 
			fprintf(out, next == i ? "%s,\n" : "\n%s = 0x%X,\n", allstr[i] + 1, i);
 
			next = i + 1;
 
		}
 
	}
 

	
 
	fprintf(out, "};\n");
 

	
 
	fprintf(out, 
 
	fprintf(out,
 
		"\nenum {\n"
 
		"\tLANGUAGE_PACK_IDENT = 0x474E414C, // Big Endian value for 'LANG' (LE is 0x 4C 41 4E 47)\n"
 
		"\tLANGUAGE_PACK_VERSION = 0x%X,\n"
 
		"};\n", (unsigned int)_hash);
 

	
 

	
 
	fclose(out);
 

	
 
	if (compare_files("tmp.xxx", filename)) {
 
		// files are equal. tmp.xxx is not needed
 
		unlink("tmp.xxx");
 
	} else {
 
@@ -681,47 +681,47 @@ void write_langfile(const char *filename
 
	char param[128];
 

	
 
	f = fopen(filename, "wb");
 
	if (f == NULL) error("can't open %s", filename);
 

	
 
	memset(&hdr, 0, sizeof(hdr));
 
	for(i=0; i!=32; i++) {
 
		int n = count_inuse(i);
 
		in_use[i] = n;
 
		hdr.offsets[i] = TO_LE16(n);
 
	}
 

	
 
	// see line 655: fprintf(..."\tLANGUAGE_PACK_IDENT = 0x474E414C,...) 
 
	// see line 655: fprintf(..."\tLANGUAGE_PACK_IDENT = 0x474E414C,...)
 
	hdr.ident = TO_LE32(0x474E414C); // Big Endian value for 'LANG'
 
	hdr.version = TO_LE32(_hash);
 
	strcpy(hdr.name, _lang_name);
 
	strcpy(hdr.own_name, _lang_ownname);
 

	
 
	fwrite(&hdr, sizeof(hdr), 1, f);
 

	
 
	for(i=0; i!=32; i++) {
 
		for(j=0; j!=in_use[i]; j++) {
 
			char *s = allstr[(i<<11)+j], *str;
 

	
 
			if (s == NULL) {
 
				write_length(f, 0);
 
			} else {
 
				// move to string
 
				str = s + 2 + strlen(s + 1);
 

	
 
				if (show_todo && s[0]) {
 
					if (show_todo == 2) {
 
						fprintf(stderr, "Warning:%s: String '%s' is untranslated\n", filename, s + 1);
 
					} else {
 
						char *s = "<TODO> ";
 
						const char *s = "<TODO> ";
 
						while(*s) put_byte(*s++);
 
					}
 
				}
 

	
 
				for(;;) {
 
					cs = parse_command_string(&str, param, s[0] ? "english.lng" : filename);
 
					if (cs == NULL) break;
 
					if ( (unsigned long) cs <= 255) {
 
						put_byte( (byte) (int)cs);
 
					} else {
 
						cs->proc(param, cs->value);
 
					}
 
@@ -760,28 +760,28 @@ int main(int argc, char* argv[])
 
	if (argc > 1 && !strcmp(argv[1], "-w")) {
 
		show_todo = 2;
 
		argc--, argv++;
 
	}
 

	
 

	
 
	if (argc == 1) {
 
		// parse master file
 
		parse_file("lang/english.txt", true);
 
		if (_warnings) return 1;
 

	
 
		// write english.lng and strings.h
 
		
 

	
 
		write_langfile("lang/english.lng", 0);
 
		write_strings_h("table/strings.h");
 
		
 

	
 
	} else if (argc == 2) {
 
		parse_file("lang/english.txt", true);
 
		parse_file(argv[1], false);
 
		if (_warnings) return 1;
 
		strcpy(buf, argv[1]);
 
		r = strrchr(buf, '.');
 
		if (!r || strcmp(r, ".txt")) r = strchr(buf, 0);
 
		strcpy(r, ".lng");
 
		write_langfile(buf, show_todo);
 
	} else {
 
		fprintf(stderr, "invalid arguments\n");
 
	}