Commit b545bdd0 authored by Vincent Wei's avatar Vincent Wei
Browse files

res2c

parent 5e41ce96
......@@ -95,4 +95,5 @@ tools/bin2c
upfeditor/upfeditor
upfeditor/upftest
vbfeditor/vbfeditor
res2c/res2c
......@@ -2,6 +2,6 @@ AUTOMAKE_OPTIONS = foreign
SUBDIRS=@STDIO_TOOLS@ @TTF_TOOLS@ @MINIGUI_TOOLS@
DIST_SUBDIRS = res common \
font-tools tools mgcfg-trans inner-res-trans ngux-tools product-id \
font-tools tools mgcfg-trans inner-res-trans res2c ngux-tools product-id \
rbfeditor vbfeditor qpfeditor upfeditor image-dumper \
ttf2qpf ttf2upf bdf2upf bdf2rbf cryptool
......@@ -94,6 +94,7 @@ AC_OUTPUT(
font-tools/Makefile
mgcfg-trans/Makefile
inner-res-trans/Makefile
res2c/Makefile
ngux-tools/Makefile
product-id/Makefile
cryptool/Makefile
......
bin_PROGRAMS = res2c
res2c_SOURCES = res2c.cc resource.c resource.h
Tools:
1. Program res2c can convert resource package(generated by mstudio) to c file.
Usage: ./res2c --project test --resname test.res
The options:
--project(-p) Required.
--resname(-r) Optional.
2. Program cfg2c can convert configuration file to c file.
Usage:
./cfg2c [file.cfg] [dest.c]
Options include:
--version : Prints the version number
--help : Display this help'''
#!/usr/bin/python
#Filename: cfg2c.py
import string, sys
if sys.version_info < (3, 0):
import ConfigParser
else:
import configparser
def printHeader(fp, c_name):
fp.write("/*\n"
" * Filename: %s\n"
" * This file is generated automatically by tools, please don't edit.\n"
" * \n"
" * Copyright (C) 2009 Feynman Software\n"
" * All rights reserved by Feynman Software.\n"
" */\n\n\n"
"#include <stdio.h>\n"
"#include <stdlib.h>\n"
"#include <string.h>\n"
"#include <minigui/common.h>\n"
"#include <minigui/minigui.h>\n"
"#include <minigui/gdi.h>\n"
"#include <minigui/window.h>\n\n\n" % c_name)
def printSection(fp, cf):
section = {} #dict
for sect in cf.sections():
size = 0
prefix = sect.replace('.', '_');
keyname = '_%s_keys' % prefix
valuename = '_%s_values' % prefix
fp.write('static char* %s[] = {\n' % keyname);
for key, value in cf.items(sect):
fp.write(' \"%s\",\n' % key);
size += 1
fp.write('};\n\n');
fp.write('static char* %s[] = {\n' % valuename);
for key, value in cf.items(sect):
fp.write(' \"%s\",\n' % value);
fp.write('};\n\n');
sect_info = (size, keyname, valuename)
section[sect] = sect_info;
fp.write("static ETCSECTION ncs_etc_section[] = {\n")
for key, info in section.items():
fp.write(' { 0, %s,\n'
' \"%s\",\n'
' %s,\n'
' %s },\n\n'
% (info[0], key, info[1], info[2]))
fp.write('};\n\n');
fp.write("static ETC_S ncs_etc = {\n"
" 0,\n"
" sizeof(ncs_etc_section)/sizeof(ETCSECTION),\n"
" ncs_etc_section\n"
"};\n\n\n"
"GHANDLE ncsGetEtcCfg(void)\n"
"{\n"
" return (GHANDLE)&ncs_etc;\n"
"}\n")
def help():
help_txt = '''
This program convert configuration file to c file.
Usage:
./cfg2c [file.cfg] [dest.c]
Options include:
--version : Prints the version number
--help : Display this help'''
print('%s' % help_txt)
def parse_argument():
global cfg_name
global c_name
if len(sys.argv) < 2:
help()
sys.exit()
if sys.argv[1].startswith('--'):
option = sys.argv[1][2:]
if option == 'version':
print('Version 1.0')
elif option == 'help':
help()
sys.exit()
else:
if len(sys.argv) < 3:
help()
sys.exit()
cfg_name = sys.argv[1]
if cfg_name.endswith('.cfg'):
c_name = sys.argv[2]
else:
help()
sys.exit()
def cfg2c():
parse_argument()
if sys.version_info < (3, 0):
cf = ConfigParser.ConfigParser()
else:
cf = configparser.ConfigParser()
cf.read(cfg_name)
fp = open(c_name, 'w+')
printHeader(fp, c_name)
printSection(fp, cf)
fp.close()
cfg2c()
#ifdef WIN32
#include <windows.h>
void *win_mmap(const char *file)
{
HANDLE obj;
HANDLE hFile;
int fileSize;
void *data = NULL;
hFile = CreateFile(file, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ,
NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE){
return NULL;
}
fileSize = GetFileSize( hFile, NULL);
obj = CreateFileMapping( hFile, NULL, PAGE_READWRITE,
0, fileSize, NULL);
GetLastError();
if (obj){
data = MapViewOfFile( obj, FILE_MAP_WRITE, 0, 0, 0);
}
CloseHandle(obj);
CloseHandle(hFile);
return data;
}
void win_munmap(void *mem)
{
UnmapViewOfFile(mem);
}
#endif
\ No newline at end of file
#ifdef WIN32
void * win_mmap(const char *file);
void win_munmap(void *mem);
#endif
\ No newline at end of file
/**
* $Id$
*
* Author: XiaoweiYan
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "resource.h"
#include <string>
#include <set>
#ifndef WIN32
#include <getopt.h>
#endif
#define OUTDIR outdir
#ifdef WIN32
#include <stdarg.h>
static inline void _PRINTF(const char* format, ...)
{
#ifdef _DEBUG
va_list va;
va_start(va, format);
vprintf(format, va);
#endif
}
#else
#ifdef _DEBUG
#define _PRINTF(format...) fprintf(stderr, format)
#else
#define _PRINTF(format...)
#endif
#endif
#ifdef WIN32
static char *basename(char* path){
const char* str;
if(path == NULL)
return NULL;
str = strrchr(path, '\\');
if(!str)
str = strrchr(path, '/');
if(!str)
return path;
return (char*)str + 1;
}
#else
#include <libgen.h>
#endif
/*=================================================================*/
static char project[1024] = {};
static char resname[1024] = {};
static char fullres[1024] = {};
static char projname[256] = {};
static char outdir[1024] = "incore-res";
static void set_proj_name(char* project)
{
if (strcmp(project, ".") == 0 || strcmp(project, "./") == 0) {
strcpy (projname, basename(getenv("PWD")));
return;
}
char *p = strrchr(project, '/');
if(!p)
p = strrchr(project, '\\');
if (p) {
int len = strlen(project) - 1;
if (p == (project + len)) {
project[len] = '\0';
set_proj_name(project);
}
else {
strcpy(projname, p+1);
}
}
else
strcpy(projname, project);
}
static void set_package_file(char* project, char* resname)
{
if (!project || !resname || strcmp(project, "") == 0)
return;
if (strcmp(resname, "") == 0) {
sprintf (fullres, "%s/res/%s.res", project, projname);
}
else
sprintf (fullres, "%s/res/%s", project, resname);
_PRINTF("----- fullres:[%s] \n", fullres);
}
#ifndef WIN32
struct option longopts[] = {
{ "project", required_argument, NULL, 'p'},
{ "resname", required_argument, NULL, 'r'},
{ "outdir", optional_argument, NULL, 'd'},
{ 0, 0, 0, 0},
};
#endif
static int parseArgs(int argc, char* argv[])
{
#ifndef WIN32
int c;
while ((c = getopt_long(argc, argv, "p:r:d:", longopts, NULL)) != -1)
{
switch (c) {
case 'p':
if (optarg)
strcpy(project, optarg);
break;
case 'r':
if (optarg)
strcpy(resname, optarg);
break;
case 'd':
if (optarg)
strcpy(outdir, optarg);
break;
}
}
#else
int i;
#define CHECKARG(long_arg, short_arg) \
((argv[i][0] == '-' && strcmp(&argv[i][2], long_arg) == 0) \
|| strcmp(&argv[i][1], short_arg)==0)
for(i = 0; i < argc; i ++)
{
if(argv[i][0] == '-') {
if(CHECKARG("project", "p")){
strcpy(project, argv[++i]);
}
else if(CHECKARG("resname", "r")) {
strcpy(resname, argv[++i]);
}
else if(CHECKARG("outdir", "d")) {
strcpy(outdir, argv[++i]);
}
}
}
#undef CHECKARG
#endif
if (strcmp(project, "") == 0) {
return 1;
}
_PRINTF("\n----- project:[%s], resname:[%s] \n", project, resname);
//current path
if (strncmp(project, ".", strlen(project)) == 0
|| strncmp(project, "./", strlen(project)) == 0) {
strcpy (project, getenv("PWD"));
}
set_proj_name(project);
set_package_file(project, resname);
return 0;
}
static char* getProjName()
{
return projname;
}
static char* getFullProjName()
{
return project;
}
static char* getResName()
{
return resname;
}
static char* getFullResName()
{
return fullres;
}
/*=================================================================*/
using namespace std;
static set <string> images;
static const char* add_export(const char* filename)
{
int idx = 0;
int i;
const char* begin;
const char* ext;
static char strname[1024];
begin = strrchr(filename, '/');
if(begin == NULL)
begin = filename;
else
begin += 1;
ext = strrchr(begin, '.');
i = (ext?(ext-begin):strlen(begin))+10;
strname[idx++] = '_';
if(ext){
for(i=1;ext[i]; i++)
strname[idx++] = ext[i];
strname[idx++] = '_';
}
for(i=0;(ext && &begin[i]<ext)||(!ext && begin[i]); i++)
{
if(isalnum(begin[i]))
strname[idx++] = begin[i];
else
strname[idx++] = '_';
}
if(strname[idx-1] == '_')
strcpy(strname+idx, "data");
else
strcpy(strname+idx, "_data");
return strname;
}
#define BUFSIZE 16384 /* Increase buffer size by this amount */
typedef unsigned char Bytef;
typedef unsigned long uLongf;
static void bin2c(const char* in, const char* out, const char* varname)
{
int i, j;
FILE *outfile=NULL; /* The output file*/
char outname[1024];
Bytef *source=NULL; /* Buffer containing uncompressed data */
Bytef *dest=NULL; /* Buffer containing compressed data */
uLongf sourceBufSize=0; /* Buffer size */
uLongf sourceLen; /* Length of uncompressed data */
uLongf destLen; /* Length of compressed data */
FILE *infile=NULL; /* The input file containing binary data */
char *tmpname;
infile = fopen (in, "rb");
if (infile == NULL) return;
if (out) {
outfile = fopen (out, "w");
}
else {
sprintf (outname, "%s/%s/%s.c.data", getFullProjName(), OUTDIR, add_export(in));
//sprintf (outname, "%s.c", in);
outfile = fopen (outname, "w");
}
fprintf (stderr, "res2c=>%s to c[%s]\n", in, out?out:outname);
if (outfile == NULL) {
fprintf (stderr, "res2c=> can't open %s for writing\n", out?out:outname);
return;
}
sourceLen = 0;
while (!feof (infile)) {
if (sourceLen + BUFSIZE > sourceBufSize) {
sourceBufSize += BUFSIZE;
source = (Bytef*)realloc (source, sourceBufSize);
if (source == NULL) return;
}
sourceLen += fread (source+sourceLen, 1, BUFSIZE, infile);
if (ferror (infile)) return;
}
fclose (infile);
if(sourceLen <= 0)
{
fclose(outfile);
return ;
}
destLen = sourceLen;
dest = source;
tmpname = (char*)strrchr(out?out:outname, '/');
if (tmpname)
tmpname += 1;
fprintf (outfile, "/*\n"
" * Filename: %s \n"
" * This file is generated automatically by res2c, please don't edit.\n"
" *\n"
" * Copyright (C) 2009 Feynman Software\n"
" * All rights reserved by Feynman Software.\n"
" */\n\n#ifdef _MGNCS_INCORE_RES\n\n", tmpname? tmpname : (out?out:outname));
/* Output dest buffer as C source code to outfile */
fprintf (outfile, "static const unsigned char %s[] = {\n",
varname ? varname : add_export(in));
for (j=0; j<destLen-1; j++) {
switch (j%8) {
case 0:
fprintf (outfile, " 0x%02x, ", ((unsigned) dest[j]) & 0xffu);
break;
case 7:
fprintf (outfile, "0x%02x,\n", ((unsigned) dest[j]) & 0xffu);
break;
default:
fprintf (outfile, "0x%02x, ", ((unsigned) dest[j]) & 0xffu);
break;
}
}
if ((destLen-1)%8 == 0) fprintf (outfile, " 0x%02x\n};\n\n", ((unsigned) dest[destLen-1]) & 0xffu);
else fprintf (outfile, "0x%02x\n};\n\n", ((unsigned) dest[destLen-1]) & 0xffu);
fprintf(outfile, "#endif /* _MGNCS_INCORE_RES */\n\n");
free (source);
fclose (outfile);
}
#define RES_KEY unsigned long
RES_KEY Str2Key(const char* str)
{
int i,l;
RES_KEY ret = 0;
unsigned short *s;
if(str == NULL)
return (RES_KEY)-1;
l = (strlen(str)+1) / 2;
s = (unsigned short*) str;
for(i=0; i<l; i++)
ret ^= (s[i]<<(i&0x0f));
return ret;
}
static int pack2c(void)
{
FILE *resfile;
char cname[1024];
sprintf (cname, "%s/%s/mgncs_incore_package.c", getFullProjName(),OUTDIR);
bin2c(getFullResName(), cname, "_mgncs_incore_package");
resfile = fopen (cname, "a+");
if (resfile == NULL) return 1;
fprintf (resfile,
"#include <minigui/common.h>\n"
"#include <minigui/minigui.h>\n"
"#include <minigui/gdi.h>\n"
"#include <minigui/window.h>\n"
"#include <mgncs/mgncs.h>\n\n"
"#ifdef _MGNCS_INCORE_RES\n"
"#include \"_incore_images.c.data\"\n\n"
"HPACKAGE ncsLoadIncoreResPackage(void)\n"
"{\n"
" if(sizeof(_mgncs_incore_images) > 0 ) \n"
" { \n"
" if(AddInnerRes(_mgncs_incore_images, \n"
" sizeof(_mgncs_incore_images)/sizeof(INNER_RES), FALSE) != RES_RET_OK)\n"
" return HPACKAGE_NULL;\n"
" }\n"
" return ncsLoadResPackageFromMem((const void*)_mgncs_incore_package, sizeof(_mgncs_incore_package));\n"
"}\n\n"
"#endif\n");
fclose (resfile);
}
static int image2c(set<string> filelist)
{
set <string>::iterator it;
FILE *resfile;
char cname[1024];
sprintf (cname, "%s/%s/_incore_images.c.data", getFullProjName(), OUTDIR);
//an image file to a c file
for (it = filelist.begin(); it != filelist.end(); it++) {
char name[1024];
sprintf(name, "%s/%s", getFullProjName(), it->c_str());
//in, out, varname
bin2c(name, NULL, NULL);
}
//incore image data section
resfile = fopen (cname, "w+");
if (resfile == NULL) return 1;
fprintf (resfile, "/*\n"
" * Filename: %s \n"