2012年(27)
分类:
2012-10-31 11:02:09
Rom.c中的程序主要是对rom方式存放的web page进行处理。
Webcomp.c主要是对将web page转换为rom形式,转换后的web page为webrom.c
Webrom.c文件中存放着各个页面。
websRomPageIndexType websRomPageIndex[]是一个数组,结构体为:
typedef struct {
char_t *path; /* Web page URL path */
unsigned char *page; /* Web page data */
int size; /* Size of web page in bytes */
int pos; /* Current read position */
} websRomPageIndexType;
例如:
websRomPageIndexType websRomPageIndex[] = {
{ T("/css/style.css"), page_0, 3578 },
{ T("/css/tree.css"), page_1, 300 },
{ T("/eth.asp"), page_2, 6404 },
{ T("/ethglobal.asp"), page_3, 2927 },
{ T("/global.asp"), page_4, 868 },
{ T("/images/empty.gif"), page_5, 834 },
{ T("/images/join.gif"), page_6, 870 },
{ T("/images/joinbottom.gif"), page_7, 856 },
{ T("/images/line.gif"), page_8, 856 },
{ T("/images/logo.gif"), page_9, 1545 },
{ T("/images/minus.gif"), page_10, 872 },
{ T("/images/minusbottom.gif"), page_11, 868 },
{ T("/images/nolines_minus.gif"), page_12, 859 },
{ T("/images/nolines_plus.gif"), page_13, 862 },
{ T("/images/plus.gif"), page_14, 873 },
{ T("/images/plusbottom.gif"), page_15, 869 },
{ T("/index.asp"), page_16, 2965 },
{ T("/index.htm"), page_17, 3119 },
{ T("/js/common.js"), page_18, 1103 },
…..
{ 0, 0, 0 },
}
分别对应page的路径、page数据的字符数组、page中字符数量。
static unsigned char page_0[] = {
98,111,100,121, 44, 32,116, 97, 98,108,101, 44, 32,116, 97, 98,
108,101, 32,116,100, 32,123, 10, 32, 32, 32, 32,102,111,110,116,
45,102, 97,109,105,108,121, 58, 32, 34, 72,101,108,118,101,116,
105, 99, 97, 34, 59, 10,125, 10, 10, 98,111,100,121, 32,123, 10,
32, 32, 32, 32,102,111,110,116, 45,115,105,122,101, 58, 32, 57,
112,116, 59, 10, 32, 32, 32, 32, 98, 97, 99,107,103,114,111,117,
110,100, 45, 99,111,108,111,114, 58, 32, 35, 68, 65, 69, 56, 70,
。。。。
}
Page_0就是"/css/style.css"的数据。
int websRomOpen()
功能:打开rom模块
说明:
-----------------------------------------------------------------------------------------------------------------
void websRomClose()
功能:关闭rom模块。
说明:
-----------------------------------------------------------------------------------------------------------------
int websRomPageOpen(webs_t wp, char_t *path, int mode, int perm)
功能:打开一个page
说明:
-----------------------------------------------------------------------------------------------------------------
int websRomPageStat(char_t *path, websStatType *sbuf)
功能:查询page的stat信息。
说明:
-----------------------------------------------------------------------------------------------------------------
int websRomPageReadData(webs_t wp, char *buf, int nBytes)
功能:读取rom中page到buf中。
说明:
-----------------------------------------------------------------------------------------------------------------
long websRomPageSeek(webs_t wp, long offset, int origin)
功能:移动到page中的某个位置
说明:
-----------------------------------------------------------------------------------------------------------------
Webcomp.c中的程序在linux下能编译通过、跑起来。
int main(int argc, char* argv[])
功能:主程序
说明:输入命令时需要两个参数,./webcomp prefix filelist >webrom.c,prefix是page所在的目录,filelist是page目录名称文件,>webrom.c是生成的rom文件。
一般可以这样
find ../web1 –name "*.*" > websfile
./webcomp ../web1 websfile > ../webrom.c
websfile中的内容是:
web1/css/style.css
web1/css/tree.css
web1/eth.asp
web1/ethglobal.asp
web1/global.asp
web1/images/empty.gif
….
-----------------------------------------------------------------------------------------------------------------
static void usage()
功能:说明语句。
说明:
-----------------------------------------------------------------------------------------------------------------
static int compile(char *fileList, char *prefix)
功能:转换生成rom文件的主程序。
说明:生成webrom.c文件
-----------------------------------------------------------------------------------------------------------------
由于感觉webcomp.c只是linux下程序,所以修改了一下,转换成windows下的程序。
/********************************* Includes ***********************************/
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
/**************************** Forward Declarations ****************************/
typedef char char_t;
typedef struct stat gstat_t;
#define FNAMESIZE 254 /* Max length of file names */
#define gctime ctime
#define gstat stat
#define gopen open
#define gstrlen strlen
#define MAX_FILENAME 500
static int compile(char_t *fileList, char_t *prefix);
static void usage();
int searchFile(char *pathname, FILE *fp);
int makeFile(char *pathname, char *filename);
/*********************************** Code *************************************/
/*
* Main program for webpack test harness
*/
int searchFile(char *pathname, FILE *fp)
{
struct _finddata_t data;
char path[MAX_FILENAME];
long handle = 0;
char subpath[MAX_FILENAME];
int length = 0;
/******************************************************/
strcpy(path, pathname);
strcat(path, "\\*");
handle = _findfirst(path,&data);
if (-1 == handle)
{
_findclose(handle);
return 0;
}
if ((0 != strcmp(data.name, "."))
&& (0 != strcmp(data.name, ".."))
&& (0 != strcmp(data.name,"thumbs.db")))
{
strcpy(subpath, pathname);
length = strlen(pathname);
if (pathname[length-1] != '\\')
{
strcat(subpath,"\\");
}
strcat(subpath, data.name);
if (_A_SUBDIR == (data.attrib&_A_SUBDIR))
{
searchFile(subpath, fp);
}
else
{
fprintf(fp,subpath, strlen(subpath));
fprintf(fp, "\n", strlen("\n"));
}
}
while(0 == _findnext(handle, &data))
{
if ((0 != strcmp(data.name, "."))
&& (0 != strcmp(data.name, ".."))
&& (0 != strcmp(data.name,"thumbs.db")))
{
strcpy(subpath, pathname);
length = strlen(pathname);
if (pathname[length-1] != '\\')
{
strcat(subpath,"\\");
}
strcat(subpath, data.name);
if (_A_SUBDIR == data.attrib)
{
searchFile(subpath, fp);
}
else
{
fprintf(fp,subpath, strlen(subpath));
fprintf(fp, "\n", strlen("\n"));
}
}
}
_findclose(handle);
return 0;
}
int makeFile(char *pathname, char *filename)
{
FILE *fp = NULL;
fp = fopen("websfile", "w+");
searchFile(pathname, fp);
fclose(fp);
strcpy(filename, "websfile");
return 0;
}
int main(int argc, char_t* argv[])
{
char_t *fileList, *prefix;
char_t fileName[MAX_FILENAME];
fileList = NULL;
if (argc != 2) {
usage();
}
memset(fileName, 0, sizeof(fileName));
prefix = argv[1];//dir
makeFile(prefix, fileName);
if (compile(fileName, prefix) < 0) {
return -1;
}
return 0;
}
/******************************************************************************/
/*
* Output usage message
*/
static void usage()
{
fprintf(stderr, "usage: webcomp path >output.c\n");
exit(2);
}
/******************************************************************************/
/*
* Compile the web pages
*/
static int compile(char_t *fileList, char_t *prefix)
{
gstat_t sbuf;
FILE *lp;
time_t now;
char_t file[FNAMESIZE];
char_t *cp, *sl;
char buf[512];
unsigned char *p;
int j, i, len, fd, nFile;
//printf("fileList:%s\r\n", fileList);
/*
* Open list of files
*/
if ((lp = fopen(fileList, "r")) == NULL) {
fprintf(stderr, "Can't open file list %s\n", fileList);
return -1;
}
time(&now);
fprintf(stdout, "/*\n * webrom.c -- Compiled Web Pages\n *\n");
fprintf(stdout, " * Compiled by GoAhead WebCompile: %s */\n\n",
gctime(&now));
fprintf(stdout, "#include \"wsIntrn.h\"\n\n");
fprintf(stdout, "#ifndef WEBS_PAGE_ROM\n");
fprintf(stdout, "websRomPageIndexType websRomPageIndex[] = {\n");
fprintf(stdout, " { 0, 0, 0 },\n};\n");
fprintf(stdout, "#else\n");
/*
* Open each input file and compile each web page
*/
nFile = 0;
while (fgets(file, sizeof(file), lp) != NULL) {
if ((p = strchr(file, '\n')) || (p = strchr(file, '\r'))) {
*p = '\0';
}
if (*file == '\0') {
continue;
}
if (gstat(file, &sbuf) == 0 && sbuf.st_mode & S_IFDIR) {
continue;
}
errno = 0;
if ((fd = gopen(file, O_RDONLY | O_BINARY)) < 0) {
fprintf(stderr, "Can't open file %s\n", file);
return -1;
}
fprintf(stdout, "static unsigned char page_%d[] = {\n", nFile);
while ((len = read(fd, buf, sizeof(buf))) > 0) {
p = buf;
for (i = 0; i < len; ) {
fprintf(stdout, " ");
for (j = 0; p < &buf[len] && j < 16; j++, p++) {
fprintf(stdout, "%3d,", *p);
}
i += j;
fprintf(stdout, "\n");
}
}
fprintf(stdout, " 0 };\n\n");
close(fd);
nFile++;
}
fclose(lp);
/*
* Now output the page index
*/
fprintf(stdout, "websRomPageIndexType websRomPageIndex[] = {\n");
if ((lp = fopen(fileList, "r")) == NULL) {
fprintf(stderr, "Can't open file list %s\n", fileList);
return -1;
}
nFile = 0;
while (fgets(file, sizeof(file), lp) != NULL) {
if ((p = strchr(file, '\n')) || (p = strchr(file, '\r'))) {
*p = '\0';
}
if (*file == '\0') {
continue;
}
/*
* Remove the prefix and add a leading "/" when we print the path
*/
if (strncmp(file, prefix, gstrlen(prefix)) == 0) {
cp = &file[gstrlen(prefix)];
} else {
cp = file;
}
while((sl = strchr(file, '\\')) != NULL) {
*sl = '/';
}
if (*cp == '/') {
cp++;
}
if (gstat(file, &sbuf) == 0 && sbuf.st_mode & S_IFDIR) {
fprintf(stdout, " { T(\"/%s\"), 0, 0 },\n", cp);
continue;
}
fprintf(stdout, " { T(\"/%s\"), page_%d, %d },\n", cp, nFile,
sbuf.st_size);
nFile++;
}
fclose(lp);
fprintf(stdout, " { 0, 0, 0 },\n");
fprintf(stdout, "};\n");
fprintf(stdout, "#endif /* WEBS_PAGE_ROM */\n");
fclose(lp);
fflush(stdout);
return 0;
}
/******************************************************************************/
然后编写了bat文件,内容是:
webcomp.exe web1 > src\webrom.c
即将web1目录下page转换成webrom.c了。