📄 lang-support.c
字号:
if (!seek) { return string; } trans=seek->data; return trans->string; }}void lang_merge_glade (char *filename) { struct stat st; char *buffer; char *pos=NULL; int fd; return_if_fail (filename != NULL); return_if_fail (access (filename, R_OK) != -1); stat (filename, &st); if (st.st_size == 0) return; fd=open (filename, O_RDONLY); if (fd == -1) { perror ("Could not open file for reading"); return; } buffer=(char *)calloc (st.st_size, sizeof (char)); if (read (fd, buffer, st.st_size) < st.st_size) { fprintf (stderr, "Read Error!\n"); free (buffer); return; } pos=strstr (buffer, "N_(\""); for (;pos;) { char *tmp, *tmp2; pos+=strlen ("N_(\""); tmp2=strstr (pos, "\");"); tmp=(char *)calloc (tmp2 - pos + 2, sizeof (char)); memmove (tmp, pos, tmp2 - pos); /* check to see if theres more than one in this */ if (lang_find_nonliteral (tmp, '"')) { /* yes it does */ char *tmp3=lang_find_nonliteral (tmp, '"'); char *last=tmp; char *string; while (tmp3) { string=(char *)calloc (tmp3 - last + 2, sizeof (char)); if (*last=='"') { memmove (string, last+1, tmp3 - last - 1); } else { memmove (string, last, tmp3 - last); } last=tmp3; if (!last) break; tmp3=lang_find_nonliteral (last, '"'); last=tmp3; if (!last) break; tmp3=lang_find_nonliteral (last, '"'); /* have to do this twice */ lang_add_native (string); free (string); } } else { /* no it doesnt */ lang_add_native (tmp); } free (tmp); pos=strstr (pos, "N_(\""); } free (buffer); close (fd);}void lang_write_string_node (FILE *f, LangNode *node, int id) { return_if_fail (node != NULL); if (node->string) { char *tmp=lang_prepare_string (node->string); fprintf (f, " {\"%s\", /* string id %d */\n", tmp, id); free (tmp); } else { fprintf (f, " {NULL, /* string id %d */\n", id); } { int id; llist *seek; for (id=1;id<MAXLANGUAGES-1;id++) { for (seek=node->trans;seek;seek=seek->next) { LangTrans *trans=seek->data; if (trans->langnum==id) break; } if (seek) { LangTrans *trans=seek->data; char *tmp=lang_prepare_string (trans->string); fprintf (f, " \"%s\", /* %s */\n", tmp, langnames[id]); free (tmp); } else { fprintf (f, " NULL, /* non-existant %s */\n", langnames[id]); } } seek=lang_llist_find_custom (node->trans, (void *)id, (CompareFunc)lang_search_custom2); if (seek) { LangTrans *trans=seek->data; char *tmp=lang_prepare_string (trans->string); fprintf (f, " \"%s\"}, /* %s*/\n\n", tmp, langnames[id]); free (tmp); } else { fprintf (f, " NULL}, /* %s*/\n\n", langnames[id]); } } }void lang_save_external (char *filename) { FILE *f; llist *seek1; f=fopen (filename, "w"); if (!f) { fprintf (stderr, "Could not open %s for writing", filename); return; } for (seek1=lang_trans;seek1;seek1=seek1->next) { LangNode *node=seek1->data; llist *seek; char *tmp; fprintf (f, "<node>\n"); tmp=lang_prepare_string (node->string); fprintf (f, "native=\"%s\"\n", tmp); free (tmp); for (seek=node->trans;seek;seek=seek->next) { LangTrans *trans=seek->data; tmp=lang_prepare_string (trans->string); fprintf (f, "%s=\"%s\"\n", languages[trans->langnum], tmp); free (tmp); } fprintf (f, "\n"); } fclose (f);}int lang_get_percent (int language) { int total=lang_llist_length (lang_trans); int lang=0; float percent; llist *seek; for (seek=lang_trans;seek;seek=seek->next) { LangNode *node=seek->data; if (lang_llist_find_custom (node->trans, (void *)language, (CompareFunc)lang_search_custom2)) lang++; } percent=(float)lang/(float)total; return (int)(percent * 100);}void lang_save_external_languages (char *filename, int language) { FILE *f; llist *seek1; char percent='%'; f=fopen (filename, "w"); if (!f) { perror ("Unable to open database file for writing"); return; } fprintf (f, "# This language is %d%c complete\n\n", lang_get_percent (language), percent); for (seek1=lang_trans;seek1;seek1=seek1->next) { LangNode *node=seek1->data; char *tmp; if (!lang_llist_find_custom (node->trans, (void *)language, (CompareFunc)lang_search_custom2)) { fprintf (f, "<node>\n"); tmp=lang_prepare_string (node->string); fprintf (f, "native=\"%s\"\n", tmp); free (tmp); fprintf (f, "%s=\"Untranslated\"\n", languages[language]); fprintf (f, "\n"); } } fclose (f);}void lang_load_external (char *filename) { FILE *f; char *buffer; struct stat st; f=fopen (filename, "r"); if (!f) { fprintf (stderr, "Unable to open %s", filename); return; } stat (filename, &st); if (st.st_size <= 0) { fprintf (stderr, "Refusing to read an empty file\n"); return; } buffer=(char *)calloc (st.st_size, sizeof (char)); if (fread (buffer, sizeof (char), st.st_size, f) < st.st_size) { free (buffer); fprintf (stderr, "Read error on %s", filename); } { /* parse scope */ char *pos; pos=strstr (buffer, "<node>\n"); while (pos) { char *npos=strstr (pos + 7, "<node>\n"); char *node; char *native, *langtmp; if (npos) { node=(char *)calloc (npos - pos + 2, sizeof (char)); memmove (node, pos, npos - pos); } else { node=(char *)calloc (strlen (pos)+2, sizeof (char)); memmove (node, pos, strlen (pos)); } node+=7; native=strstr (node, "native=\""); if (native) { char *tmp; LangNode *lnode; native+=strlen ("native=\""); langtmp=lang_find_nonliteral (native, '"'); if (!langtmp) goto cantdonuttin; tmp=(char *)calloc (langtmp-native+2, sizeof (char)); memmove (tmp, native, langtmp-native); native=lang_convert_from_literal (tmp); lnode=lang_add_native (native); free (native); free (tmp); /* parse out the language thingys */ { int id; char *tmp2, *tmp3; tmp=(char *)calloc (6, sizeof (char)); for (id=0;id<MAXLANGUAGES;id++) { sprintf (tmp, "%s=\"", languages[id]); tmp2=strstr (node, tmp); if (tmp2) { tmp2+=strlen (tmp); langtmp=lang_find_nonliteral (tmp2, '"'); if (langtmp) { tmp3=(char *)calloc (langtmp - tmp2 + 2, sizeof (char)); memmove (tmp3, tmp2, langtmp - tmp2); tmp2=lang_convert_from_literal (tmp3); if (strcasecmp (tmp2, "Untranslated")!=0) { lang_add_trans (lnode, tmp2, id); } free (tmp3); free (tmp2); } } } free (tmp); } } /* can't do anything without a native string */cantdonuttin: node-=7; free (node); pos=npos; } } free (buffer); fclose (f);}void lang_load_external_append_only (char *filename) { FILE *f; char *buffer; struct stat st; f=fopen (filename, "r"); if (!f) { fprintf (stderr, "Unable to open %s", filename); return; } stat (filename, &st); if (st.st_size <= 0) { fprintf (stderr, "Refusing to read an empty file\n"); return; } buffer=(char *)calloc (st.st_size, sizeof (char)); if (fread (buffer, sizeof (char), st.st_size, f) < st.st_size) { free (buffer); fprintf (stderr, "Read error on %s", filename); perror (""); } { /* parse scope */ char *pos; pos=strstr (buffer, "<node>\n"); while (pos) { char *npos=strstr (pos + 7, "<node>\n"); char *node; char *native, *langtmp; if (npos) { node=(char *)calloc (npos - pos + 2, sizeof (char)); memmove (node, pos, npos - pos); } else { node=(char *)calloc (strlen (pos)+2, sizeof (char)); memmove (node, pos, strlen (pos)); } node+=7; native=strstr (node, "native=\""); if (native) { char *tmp; LangNode *lnode; native+=strlen ("native=\""); langtmp=lang_find_nonliteral (native, '"'); if (!langtmp) goto cantdonuttin; tmp=(char *)calloc (langtmp-native+2, sizeof (char)); memmove (tmp, native, langtmp-native); native=lang_convert_from_literal (tmp); lnode=lang_find_node (native); if (!lnode) goto cantdonuttin; free (native); free (tmp); /* parse out the language thingys */ { int id; char *tmp2, *tmp3; tmp=(char *)calloc (6, sizeof (char)); for (id=0;id<MAXLANGUAGES;id++) { sprintf (tmp, "%s=\"", languages[id]); tmp2=strstr (node, tmp); if (tmp2) { tmp2+=strlen (tmp); langtmp=lang_find_nonliteral (tmp2, '"'); if (langtmp) { tmp3=(char *)calloc (langtmp - tmp2 + 2, sizeof (char)); memmove (tmp3, tmp2, langtmp - tmp2); tmp2=lang_convert_from_literal (tmp3); if (strcasecmp (tmp2, "Untranslated")!=0) { lang_add_trans (lnode, tmp2, id); } free (tmp3); free (tmp2); } } } free (tmp); } } /* can't do anything without a native string */cantdonuttin: node-=7; free (node); pos=npos; } } free (buffer); fclose (f);}/* does the same thing as write_database, but does it from the linked list */void lang_save_internal (char *filename) { FILE *f; return_if_fail (filename != NULL); { char *move; move=(char *)calloc (strlen (filename) * 2 + 10, sizeof (char)); sprintf (move, "mv %s %s~", filename, filename); system (move); free (move); } if (!(f=fopen (filename, "w"))) { perror ("Error writing to file"); return; } {/* start writing to the file */ int i; llist *seek; fprintf (f, "/* WARNING! DO NOT INCLUDE THIS FILE INTO ANYTHING. */ \n\n\n"); fprintf (f, "#define MAXLANGUAGES %d\n\n", MAXLANGUAGES + lang_llist_length (extralangs)); fprintf (f, "/* this defines which lines correspond to which langauge */\n"); fprintf (f, "static char *languages[MAXLANGUAGES]= {"); for (i=0;i < MAXLANGUAGES;i++) { if (i < MAXLANGUAGES-1) { fprintf (f, " \"%s\",", languages[i]); } else { if (extralangs) { fprintf (f, " \"%s\",", languages[i]); } else { fprintf (f, " \"%s\"", languages[i]); } } } for (seek=extralangs;seek;seek=seek->next) { if (seek->next) { fprintf (f, " \"%s\",", (char *)seek->data); } else { fprintf (f, " \"%s\"", (char *)seek->data); } } fprintf (f, " };\n"); fprintf (f, "static char *langnames[MAXLANGUAGES]= {"); for (i=0;i< MAXLANGUAGES;i++) { if (i < MAXLANGUAGES-1) { fprintf (f, " \"%s\",", langnames[i]); } else { if (extralangs) { fprintf (f, " \"%s\",", langnames[i]); } else { fprintf (f, " \"%s\"", langnames[i]); } } } for (seek=extralangs;seek;seek=seek->next) { if (seek->next) { fprintf (f, " \"unknown\","); } else { fprintf (f, " \"unknown\""); } } fprintf (f, " };\n"); } { int id=0; llist *seek; fprintf (f, "static char *translations[][MAXLANGUAGES]={\n"); for (seek=lang_trans;seek;seek=seek->next) { lang_write_string_node (f, seek->data, id); id++; } lang_write_new (f, NULL); } fclose (f); }void lang_shutdown () {#ifdef LANG_DEBUG { /* first remove the deleteables */ llist *seek; char *tmp=(char *)calloc (1024, sizeof (char)); for (seek=deleteables;seek;seek=seek->next) { sprintf (tmp, "lang-input/%s", (char *)seek->data); unlink (tmp); free (seek->data); } lang_llist_free (deleteables); deleteables=NULL; free (tmp); } { /* make sure that the output directory exists */ DIR *dir=opendir ("lang-output"); if (!dir) { mkdir ("lang-output", S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR); } closedir (dir); } { /* now write databases for the strings we don't know about */ char *tmp=(char *)calloc (25, sizeof (char)); int i; for (i=0;i<MAXLANGUAGES;i++) { sprintf (tmp, "lang-output/%s.lang", languages[i]); lang_save_external_languages (tmp, i); } free (tmp); }#endif { /* then free everything up */ llist *seek1; for (seek1=lang_trans;seek1;seek1=seek1->next) { LangNode *node=seek1->data; llist *seek2; for (seek2=node->trans;seek2;seek2=seek2->next) { LangTrans *trans=seek2->data; free (trans->string); free (trans); } lang_llist_free (node->trans); free (node->string); free (node); } lang_llist_free (lang_trans); lang_llist_free (extralangs); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -