📄 str2ch.cpp
字号:
/*
* Start of Zoran Standard Header
* Copyright (c) 2002 - 2004 Zoran Corporation
*
*
* All rights reserved. Proprietary and confidential.
*
* DESCRIPTION for str2ch.cpp
* Generates lstring/genstr.c and incl/genstr.h
*
* NEW HISTORY COMMENT (description must be followed by a blank line)
* <Enter change description here>
* ===== HISTORY of changes
*
* 12/May/04 #4 bdodge add reading and generation of text box sizes, removed exclusion of quotes from token.
* 24/Mar/04 #15 asampat Use sizeof in malloc call.
* 24/Mar/04 #14 asampat Check malloc return.
* 02/Mar/05 JCogan, reduce language options to two, English and nonEnglish
*
* End of Zoran Standard Header
*/
// str2ch.cpp : Defines the entry point for the console application.
// This program is used to extract the various language strings and
// string IDs used by the front panel. The strings and IDs are provided
// in the input file "strings.txt". Generated language strings are stored
// in the output file "genstr.c" and generated string IDs are stored in the
// output file "genstr.h".
#ifdef _WIN32
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#include <Windows.h>
#else
#endif
#include <wchar.h>
#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#ifndef _WIN32
typedef unsigned char BYTE, *LPBYTE;
typedef unsigned short WORD, *LPWORD;
typedef unsigned long DWORD, *LPDWORD;
typedef void VOID, *LPVOID;
typedef char CHAR, *LPSTR;
typedef const char *LPCSTR;
typedef wchar_t WCHAR, *LPWSTR;
typedef const wchar_t *LPCWSTR;
#ifdef UNICODE
typedef wchar_t tchar_t;
#define _T(a) L ## a
#else
typedef char tchar_t;
#define _T(a) a
#endif
typedef tchar_t TCHAR, *LPTSTR;
typedef const tchar_t *LPCTSTR;
#endif
char * REV= "$Id: //depot/imgeng/sw/sw_gw/ui/fs/str2ch.cpp $";
#define BUF_SIZE 50000
#define MAX_STRINGID_LEN 100
#define MAX_STRINGS 1000
WCHAR iobuf[BUF_SIZE];
LPWSTR stringIds[MAX_STRINGS];
char ifname[1024] = "strings.txt";
typedef enum
{
English,
NonEnglish,
MAX_LANGUAGE
}
Language;
static LPCSTR langString[] =
{
"English",
"NonEnglish",
};
LPWSTR fgetUCS2s(LPWSTR buf, int size, FILE* fp)
{
static BYTE iob[4000];
int i, a, b;
DWORD c;
char xb[4000];
for(i = 0; i < size; i++)
{
a = fgetc(fp);
b = fgetc(fp);
// printf("got %08X %08X\n", a, b);
if(b == EOF) return NULL;
c = (((DWORD)b << 8) | ((DWORD)a << 0));
buf[i] = (WCHAR)c;
xb[i] = (char)c;
if(c == 10) break;
}
xb[i] = 0;
buf[i] = 0;
//printf("xb=%s=\n", xb);
//fwprintf(stderr, L"dline=%ls=", buf);
return (LPWSTR)buf;
}
LPWSTR GetToken(LPWSTR& pt)
{
static WCHAR tb[BUF_SIZE];
LPWSTR pb;
for(pb = tb; *pt && *pt != L'\t' && *pt != L'\r' && *pt != L'\n';)
{
// remove wrapping quotes, leave "" as "
// e.g. "hello its ""me"" becomes hello its "me"
//
if(*pt == L'\"')
{
pt++;
if(!*pt || *pt == L'\t' || *pt == L'\r' || *pt == L'\n')
break;
}
*pb++ = *pt++;
}
// remove terminating '\t'
pt++;
while(pb > tb && *(pb - 1) == L' ')
pb--;
*pb = L'\0';
return tb;
}
#define BYTE_SWAPPED 1
LPSTR Encode(LPCWSTR pt, int esc)
{
static char sb[BUF_SIZE];
LPSTR ps;
unsigned char utfbuf[8], ic;
unsigned char* src = (unsigned char*)pt;
unsigned short ca, cb, cc, cd;
WCHAR uc, nc;
int j, k;
size_t i, len;
len = wcslen(pt);
for(ps = sb, i = 0; i < len; i++)
{
/* first get the unicode character
*/
ca = (unsigned char)*src++;
cb = (unsigned char)*src++;
#if BYTE_SWAPPED
nc = ca | (cb << 8);
#else
nc = (ca << 8) | cb;
#endif
if(sizeof(WCHAR) == 4) // it is on Linux, etc.
{
cc = (unsigned char)*src++;
cd = (unsigned char)*src++;
#if BYTE_SWAPPED
uc = cc | (cd << 8);
nc = nc | (uc << 16);
#else
uc = (cc << 8) | cd;
nc = (nc << 8) | uc;
#endif
}
/* next, UTF-8 encode it
*/
j = 0;
if(nc < 0x80) {
utfbuf[j++] = (unsigned char)nc;
}
else if(nc < 0x800) {
utfbuf[j++] = 0xC0 | (nc >> 6);
utfbuf[j++] = 0x80 | (nc & 0x3F);
}
else if(nc < 0x10000) {
utfbuf[j++] = 0xE0 | (nc >> 12);
utfbuf[j++] = 0x80 | ((nc >> 6) & 0x3F);
utfbuf[j++] = 0x80 | (nc & 0x3F);
}
else if(nc < 0x200000) {
utfbuf[j++] = 0xF0 | (nc >> 18);
utfbuf[j++] = 0x80 | ((nc >> 12) & 0x3F);
utfbuf[j++] = 0x80 | ((nc >> 6) & 0x3F);
utfbuf[j++] = 0x80 | (nc & 0x3F);
}
for(k = 0; k < j; k++)
{
ic = utfbuf[k];
if(
! esc ||
(ic >= 40 && ic <= 60) ||
(ic >= 62 && ic <= 126 && ic != 92)
)
{
*ps++ = ic;
}
else
{
int len;
len = sprintf(ps, "\\%s%o", (ic < 0100) ? "0" : "", ic);
ps += len;
}
}
}
*ps = '\0';
return sb;
}
void PrintStringId(LPCWSTR pt, FILE *out, int nl)
{
int len = 0;
LPWSTR tmp;
int i, num_occ;
static int numIds = 0;
fprintf(out, "\tgid");
stringIds[numIds] = (LPWSTR) malloc(sizeof(LPWSTR)*MAX_STRINGID_LEN);
tmp = stringIds[numIds];
if(tmp == NULL)
{
fprintf(stderr, "malloc failed\n");
return;
}
// print each char, ignore some special chars
while(pt && *pt != L'\0')
{
if( (*pt >= L'a' && *pt <= L'z') || (*pt >= L'A' && *pt <= L'Z') ||
(*pt >= L'0' && *pt <= L'9') )
{
fprintf(out, "%c", (char)*pt);
*tmp++ = *pt;
}
// replace special char '1/2' with _5
if(*pt == 0xbd)
{
fprintf(out, "_5");
*tmp++ = L'_';
*tmp++ = L'5';
}
// replace spaces with '_'
if(*pt == L' ')
{
// max. length of string ids
if(len > 35)
{
break;
}
else
{
fprintf(out, "_");
*tmp++ = L'_';
}
}
pt++;
len++;
}
num_occ = 0;
*tmp = L'\0';
for(i = 0; i < numIds; i++)
{
if(wcscmp(stringIds[i], stringIds[numIds]) == 0)
num_occ++;
}
if(num_occ)
{
fprintf(out, "_");
fprintf(out, "%d", num_occ + 1);
}
if(nl) fprintf(out, ",\n");
numIds++;
}
int GenerateStringIds(void)
{
FILE *outH, *in;
LPCSTR ofnameH = "genstr.h";
LPWSTR pt, pl;
int i = 0;
if((outH = fopen(ofnameH, "w")) == NULL)
{
fprintf(stderr, "Can't open file %s\n", ofnameH);
return -1;
}
if((in = fopen(ifname, "rb")) == NULL)
{
fprintf(stderr, "Can't open file %s\n", ifname);
return -1;
}
fprintf(outH, "/*\n");
fprintf(outH, " * Generated using the file %s\n", REV);
fprintf(outH, " * Do not edit.\n");
fprintf(outH, " */\n");
fprintf(outH, "\ntypedef enum {\n");
// ignore title line for the columns.
#ifndef _WIN32
fgetUCS2s(iobuf, BUF_SIZE, in);
#else
fgetws(iobuf, BUF_SIZE, in);
#endif
fprintf(outH, "\tgidBlank,\n");
#ifndef _WIN32
while((pl = fgetUCS2s(iobuf, BUF_SIZE, in)) != NULL)
#else
while((pl = fgetws(iobuf, BUF_SIZE, in)) != NULL)
#endif
{
pt = GetToken(pl);
PrintStringId(pt, outH, 1);
}
fprintf(outH, "} aStringIDGenerated;\n");
fclose(in);
return 0;
}
int GenerateBoxTable(void)
{
FILE *outC, *in;
LPCSTR ofnameH = "gensbox.h";
LPCSTR ofnameC = "gensbox.c";
LPWSTR pt, pl;
WCHAR str[1024];
int w, h, s, skip;
if((outC = fopen(ofnameH, "w")) == NULL)
{
fprintf(stderr, "Can't open file %s\n", ofnameC);
return -1;
}
fprintf(outC, "/*\n");
fprintf(outC, " * Generated using the file %s\n", REV);
fprintf(outC, " * Do not edit.\n");
fprintf(outC, " */\n");
fprintf(outC, "\ntypedef struct tag_text_box {\n");
fprintf(outC, "\tshort\t\t\tw;\t/* width of box */\n");
fprintf(outC, "\tunsigned char\th;\t/* height of box */\n");
fprintf(outC, "\tunsigned char\ts;\t/* non-zero = string can scroll in box */\n");
fprintf(outC, "}\nTEXT_BOX, *PTEXT_BOX;\n\n");
fprintf(outC, "extern const TEXT_BOX g_textbox[];\n");
fclose(outC);
if((outC = fopen(ofnameC, "w")) == NULL)
{
fprintf(stderr, "Can't open file %s\n", ofnameC);
return -1;
}
if((in = fopen(ifname, "rb")) == NULL)
{
fprintf(stderr, "Can't open file %s\n", ifname);
return -1;
}
fprintf(outC, "/*\n");
fprintf(outC, " * Generated using the file %s\n", REV);
fprintf(outC, " * Do not edit.\n");
fprintf(outC, " */\n");
fprintf(outC, "#include \"gensbox.h\"\n\n");
fprintf(outC, "\nconst TEXT_BOX g_textbox[] = {\n");
// ignore title line for the columns.
#ifndef _WIN32
fgetUCS2s(iobuf, BUF_SIZE, in);
#else
fgetws(iobuf, BUF_SIZE, in);
#endif
fprintf(outC, "\t{ %3d, %3d, %3d },\t/* gidBlank */\n", 1, 24, 0);
#ifndef _WIN32
while((pl = fgetUCS2s(iobuf, BUF_SIZE, in)) != NULL)
#else
while((pl = fgetws(iobuf, BUF_SIZE, in)) != NULL)
#endif
{
// first token used to make string ID
pt = GetToken(pl);
wcscpy(str, pt);
for(skip = English; skip < MAX_LANGUAGE; skip++)
{
pt = GetToken(pl);
}
// last 3 tokens are box
pt = GetToken(pl);
w = wcstol(pt, NULL, 0);
pt = GetToken(pl);
h = wcstol(pt, NULL, 0);
pt = GetToken(pl);
s = wcstol(pt, NULL, 0);
fprintf(outC, "\t{ %3d, %3d, %1d },\t/*", w, h, s);
PrintStringId(str, outC, 0);
fprintf(outC, "\t\t\t*/\n");
}
fprintf(outC, "};\n");
fclose(outC);
fclose(in);
return 0;
}
LPWSTR MakeLangStringSane(LPWSTR ps)
{
static WCHAR tmpBuf[BUF_SIZE];
int addStr = 0, addDec = 0;
LPWSTR ls;
int added = 0;
int addPercent = 0;
int i = 0, j = 0;
ls = tmpBuf;
while(*ps != L'\0')
{
if(*ps == L'<')
{
// Replace with %d if<X>, <Y>, <Z> otherwise %s.
if((*(ps+1) == L'X') || (*(ps+1) == L'Y') || (*(ps+1) == L'Z'))
{
addDec = 1;
}
else
addStr = 1;
ps++;
}
else if(*ps == L'>')
{
addDec = addStr = 0;
added = 0;
j = 0;
// Check ifa % follows <>.
while(*(ps+j) != L'\0')
{
if(*(ps+j) == L'%')
addPercent = 1;
j++;
}
ps++;
}
else if((addDec == 1 || addStr == 1) && added == 0)
{
ls[i++] = L'%';
ls[i++] = addDec == 1 ? L'd' : L's';
added = 1;
}
else if(added == 0)
{
if(*ps != L' ' && addPercent == 1)
{
addPercent = 0;
ls[i++] = L'%';
}
ls[i++] = *ps++;
}
else if(added == 1)
{
ps++;
}
}
ls[i] = L'\0';
return ls;
}
// Returns 0 on success, -1 otherwise.
int ExtractLangStrings(void)
{
FILE *in, *outC, *html;
LPCSTR ofnameC = "genstr.c";
LPCSTR tfnameC = "gensbox.c";
int skip;
LPSTR ps;
Language lang;
LPWSTR pt, pl;
if((outC = fopen(ofnameC, "w")) == NULL)
{
fprintf(stderr, "Can't open file %s\n", ofnameC);
return -1;
}
if((html = fopen("test.html", "w")) == NULL)
{
fprintf(stderr, "Can't open file %s, not making html\n", "test.html");
}
fprintf(outC, "/*\n");
fprintf(outC, " * Generated using the file %s\n", REV);
fprintf(outC, " * Do not edit.\n");
fprintf(outC, " */\n\n");
fprintf(outC, "#include \"arch.h\"\n");
fprintf(outC, "#include \"lstring.h\"\n\n");
if(html)
fprintf(html, "<html><meta HTTP-EQUIV=\"content-type\" CONTENT=\"text/html; charset=UTF-8\"><body>\n");
for(lang = English; lang < MAX_LANGUAGE; lang = (Language)((int)lang + 1))
{
if((in = fopen(ifname, "rb")) == NULL)
{
fprintf(stderr, "Can't open file %s\n", ifname);
return -1;
}
fprintf(outC, "const char * g_str%s[] = // strings in %s\n{\n",
langString[lang], langString[lang]);
if(html)
fprintf(html, "<h3>Text Strings for %s</h3>\n",
langString[lang]);
// ignore title line for the columns.
#ifndef _WIN32
fgetUCS2s(iobuf, BUF_SIZE, in);
#else
fgetws(iobuf, BUF_SIZE, in);
#endif
// Print "" to represent a blank string in each lang table
fprintf(outC, "\t\"\",\n");
#ifndef _WIN32
while((pl = fgetUCS2s(iobuf, BUF_SIZE, in)) != NULL)
#else
while((pl = fgetws(iobuf, BUF_SIZE, in)) != NULL)
#endif
{
// skip first column (used only for id)
pt = GetToken(pl);
for(skip = English; skip <= lang; skip++)
{
pt = GetToken(pl);
}
pt = MakeLangStringSane(pt);
ps = Encode(pt, 1);
fprintf(outC, "\t\"%s\",\n", ps);
if(html)
{
ps = Encode(pt, 0);
fprintf(html, "%s<br>\n", ps);
}
}
// Print 0x0 to represent end of each lang table
fprintf(outC, "\t0x0\n");
fprintf(outC, "};\n\n");
fclose(in);
}
if(html)
fprintf(html, "</body></html>\n");
return 0;
}
int main(int argc, char * argv[])
{
if(argc > 1)
{
strcpy(ifname, *++argv);
}
if(!GenerateStringIds())
{
if(!GenerateBoxTable())
{
return ExtractLangStrings();
}
}
return -1;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -