⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cs_mpfs.cpp

📁 将网页转换成数组
💻 CPP
字号:
/***********************************************************
 * 本程序将输入文件夹中的文件形成数组形式,方便webserver
 * 来传输
 * ---------------------------------------------------------
 * 用法: cs_mpfs  armweb  csimg.c
 *
 * 解释: 将armweb目录下的文件全部转换成数组形式存于
 *       csimg.c文件中,这里将cs_mpfs.exe与目录armweb放在同一子
 *       目录下,如d:\arm下有文件cs_mpfs.exe及目录armweb
 *       我们进入d:\arm,在Dos模式下输入:
 *             cs_mpfs armweb csimg.c
 *       将会在d:\arm目录下得到转换好的csimg.c文件
 *
 *       本软件针对webpage的文件转换,文件我们又定死为8.3格式,但jave
 *       程序必将会产生.class文件,因为最后我们要产生一个文件名对应表,
 *       这个表又需要真实文件名才好,所以最后我们进行了处理
 *       但是对于其他文件扩展名>3的文件,我们将会产生错误的结果,这点需要
 *       在调用本程序之前加以保证
 **********************************************************/
#include <stdio.h>
#include <dos.h>
#include <dir.h>
#include <io.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>

#define     MAX_DIR_LEN         80      /* 目录名 */
#define     MAX_FILE_NAME_LEN   120     /* 文件名最大长度,包括路径 */
#define     MAX_FILE_SUM        80      /* 被转换的目录里最多的文件数 */
#define     MAX_DATA_PER_LINE   8       /* 每行最多放多少个字节 */
#define     JS_FILE_ADD_SUM     100     /* .JS文件需要webserver软件动态形成,这里虽然做成数组状,
                                           但实际上去没用,因为是动态生成,所以可能造成
                                           数组容量会增大, 这里暂定一个值
                                         */
FILE    *fp_wr,*fp_rd;
struct  ffblk	ff_blk;
char    fileNameBuf[MAX_FILE_NAME_LEN];
char    dirBuf[MAX_DIR_LEN];
char    fileNameArray[MAX_FILE_SUM][13];
time_t  current_time,current_time1;
struct tm *datetime,datetime1;
//************************************************************************
void buildNameArray(char dirbuf[],struct ffblk ff_blk,int fileno) {
    FILE    *fp_rd;
    long    file_size;
    int     lineNumCtr=0;
    int     sizeLSB,sizeMSB;
    int     file_handle;
    int     c;
    char    filename[MAX_FILE_NAME_LEN];
    char    fn_1[14],*fn_2;
    int     f_isJSfile=0;
	//int     f_havedata=0;

    strcpy(filename,dirbuf);
    strcat(filename,ff_blk.ff_name);
    if ((file_handle=open(filename,O_RDONLY))==-1)
        return;
    file_size=filelength(file_handle);	// get file size
    close(file_handle);

    fp_rd=fopen(filename,"rb");
    if (fp_rd == NULL)
        return;

    strcpy(fn_1,ff_blk.ff_name);
    fn_2=strchr(fn_1,'.');
    *fn_2=0;
    fn_2++;         // 取得文件名的扩展名
    if (strcmp(fn_2,"JS")==0)
        f_isJSfile=1;

    fprintf(fp_wr,"\n/*******************************************\n");
    fprintf(fp_wr," * Start Of \'");
    fprintf(fp_wr,"%s",ff_blk.ff_name);
    fprintf(fp_wr,"\' file...\n");
    fprintf(fp_wr," ******************************************/\n");
    if (f_isJSfile==1) {
        fprintf(fp_wr,"unsigned char %s[%d]= {\n",fn_1,file_size+JS_FILE_ADD_SUM);
    }
    else { 
        fprintf(fp_wr,"static ROM unsigned char CS_FILE_");
	    fprintf(fp_wr,"%04d[]= {\n",fileno);
    }
    sizeLSB=file_size & 0xff;
    sizeMSB=(file_size>>8) & 0xff;
    fprintf(fp_wr,"    0x%02x,0x%02x,",sizeMSB,sizeLSB);    // 文件长度值

    while ((c=fgetc(fp_rd)) != EOF) {
        //f_havedata=1;
        if (lineNumCtr==0)
            fprintf(fp_wr,"\n    ");
        fprintf(fp_wr,"0x%02x,",c);     
        lineNumCtr++;
        if (lineNumCtr>=MAX_DATA_PER_LINE) {
            lineNumCtr=0;
        }
    }
    fseek(fp_wr,-1,SEEK_CUR);	// get rid of ',' at last word
    fprintf(fp_wr,"\n};\n");

    fprintf(fp_wr,"/*******************************************\n");
    fprintf(fp_wr," * End Of \'");
    fprintf(fp_wr,"%s",ff_blk.ff_name);
    fprintf(fp_wr,"\' file...\n");
    fprintf(fp_wr," ******************************************/\n\n");

    fclose(fp_rd);

    printf("\n%s has been added ...!\n",filename);
}

//************************************************************************
//void BuildJSFile(int filelen) {
//	filelen+=30;
//	fprintf(fp_wr,"\n/*******************************************\n");
//	fprintf(fp_wr," * Start Of \'");
//	fprintf(fp_wr,"SETBOOT.JS");
//	fprintf(fp_wr,"\' file...\n");
//	fprintf(fp_wr," ******************************************/\n");
//	fprintf(fp_wr,"static ROM unsigned char SETBOOT[%d]={\n}");
//	fprintf(fp_wr,"%04d[]= {\n",fileno);

//	sizeLSB=file_size & 0xff;
//	sizeMSB=(file_size>>8) & 0xff;
//	fprintf(fp_wr,"    0x%02x,0x%02x,",sizeMSB,sizeLSB);    // 文件长度值

//}

//************************************************************************
void buildFileNameIndex(int fileno) {
    //int i;
    int j=0;
    char fn_1[14],*fn_2;

    fprintf(fp_wr,"/*******************************************\n");
    fprintf(fp_wr," * Start Of CS_FILE FAT.\n");
    fprintf(fp_wr," ******************************************/\n");
    fprintf(fp_wr,"typedef struct\n{\n");
    fprintf(fp_wr,"    unsigned char Flags\;\n");
    fprintf(fp_wr,"    ROM unsigned char* Address\;\n");
    fprintf(fp_wr,"    unsigned char Name[12]\;\n");
    fprintf(fp_wr,"} FAT_TABLE_ENTRY\;\n\n");
    
    fprintf(fp_wr,"ROM FAT_TABLE_ENTRY CS_FILE_Start[] =\n{");

    
    while (j<fileno) {
        strcpy(fn_1,fileNameArray[j]);
        fn_2=strchr(fn_1,'.');
        *fn_2=0;
        fn_2++;         // 取得文件名的扩展名
	    
        if (strcmp(fn_2,"JS")==0)
            fprintf(fp_wr,"\n    { 0x00, %s, ",fn_1);
        else if (strcmp(fn_2,"CLA")==0) {   // 假如有文件名movie.class,将会被转换成MOVIE~1.CLA
                                            // 实际上我们需要得到MOVIE.CLASS
            fn_2=strchr(fn_1,'~');
            if (fn_2 != 0) {                // 默认为.CLASS文件
                *fn_2=0;
                strcat(fn_1,".CLASS");
            }
            strcpy(fileNameArray[j],fn_1);  // 反过来送回
            fprintf(fp_wr,"\n    { 0x00, CS_FILE_%04d, ",j);
        }
        else
            fprintf(fp_wr,"\n    { 0x00, CS_FILE_%04d, ",j);

        fprintf(fp_wr,"\"%s\"",fileNameArray[j]);
        fprintf(fp_wr,"},");

        j++;
    }
    fprintf(fp_wr,"\n    { 0x04, (unsigned char *)0xffffffff, \"END OF FAT\" }\n};\n");

    fprintf(fp_wr,"/*******************************************\n");
    fprintf(fp_wr," * End Of CS_FILE FAT.\n");
    fprintf(fp_wr," ******************************************/\n");

    fprintf(fp_wr,"\n");

}

//************************************************************************
int main(int argc,char *argv[]) {
    char buff[50];
    struct time curr_time;
    struct date curr_date;
    int i;

    if (argc<3) {
        printf("\n");
        printf("1. Usage: cs_mpfs DirectoryName OutPut-FileName\n");
        printf("   such as : cs_mpfs webpages csimg.c\n");

        printf("\n");
        printf("2. Note: All file name length be accord to \"8.3\" format\n");
        printf("\n");
        printf("3. Because this convert is for webpages, so we will maybe meet \".class\" file,\n");
        printf("   if so, we should ensure the filename length no more than 6\n");
        printf("   such as :\tmovie.class, movie1.class is ok !\n");
        printf("\t\tbut movie10.class is too long, error !\n");

        printf("\n");
        printf("4. This program only agree \".class\" file\n");
        printf("   when else's filename extension length > 3, you will get error result !\n");

        return 1;
    }

    daylight = 0;
    if ((fp_wr=fopen(argv[2],"wt")) == NULL) {
        printf("\nOpen Output File - ");
        printf(argv[2]);
        printf(" Error\n");
        return 1;
    }
    getdate(&curr_date);
    gettime(&curr_time);
    time(&current_time);

    fprintf(fp_wr,"/**************************************************\n");
    fprintf(fp_wr," * This is a CS_FILE image for \'C\' compiler,\n");
    fprintf(fp_wr," * generated by CS_MPFS.EXE utility.\n");
    fprintf(fp_wr," * DO NOT MODIFY BY HAND.\n");
    fprintf(fp_wr," * Always use CS_MPFS.EXE utlity to create this file.\n");
    fprintf(fp_wr," * Programmer : Chenshi\n");
    fprintf(fp_wr," * Contact    : stonechan@21cn.com\n");
    fprintf(fp_wr," * Copyright@ 2004 eVision Limited Company \n");
    fprintf(fp_wr," * Generated on ");
    fprintf(fp_wr,"%d-%d-%d ",curr_date.da_mon,curr_date.da_day,curr_date.da_year);
    fprintf(fp_wr,"%02d:%02d:%02d\n",curr_time.ti_hour,curr_time.ti_min,curr_time.ti_sec);
	
    datetime = localtime(&current_time);
    strftime(buff,sizeof(buff),"%a, %d %b %Y %H:%M:%S Machine Time",datetime);
    fprintf(fp_wr," * %s\n",buff);

    datetime = gmtime(&current_time);
    strftime(buff,sizeof(buff),"%a, %d %b %Y %H:%M:%S GMT Time",datetime);
    fprintf(fp_wr," * %s\n",buff);
	
    tzset();
    fprintf(fp_wr," * Current time zone is %s\n",tzname[0]);

	/*
	datetime1.tm_year  = curr_date.da_year-1900;
	datetime1.tm_mon   = curr_date.da_mon - 1;
	datetime1.tm_mday  = curr_date.da_day ;
	datetime1.tm_hour  = curr_time.ti_hour;
	datetime1.tm_min   = curr_time.ti_min;
	datetime1.tm_sec   = curr_time.ti_sec;
	*/
	
	// 1970.1.1  00:00:00
	datetime1.tm_year  = 70;
	datetime1.tm_mon   = 0;
	datetime1.tm_mday  = 1;
	datetime1.tm_hour  = 0;
	datetime1.tm_min   = 0;
	datetime1.tm_sec   = 0;
    datetime1.tm_isdst = 0;
    
	current_time1 = mktime(&datetime1);
	
    fprintf(fp_wr," *************************************************/\n");

    fprintf(fp_wr,"\n#include <time.h>\n\n");
	
    fprintf(fp_wr,"\n#define ROM  const\n");

    getcwd(dirBuf,MAX_DIR_LEN);
	
    strcpy(fileNameBuf,dirBuf);
    strcat(fileNameBuf,"\\");
    strcat(fileNameBuf,argv[1]);
	
    //strcpy(fileNameBuf,argv[1]);
    strcat(fileNameBuf,"\\*.*");
    //strcpy(fileNameBuf,"*.*");

    strcpy(dirBuf,argv[1]);
    strcat(dirBuf,"\\");

    int fileno=0;
    int done=0;
    while (!done) {
        if (fileno==0) {
            done=findfirst(fileNameBuf,&ff_blk,0);
            //done=findfirst("*.*",&ff_blk,0);
        }
        else {
            done=findnext(&ff_blk);
        }
        if (done)
            break;
        //if (strcmp(ff_blk.ff_name,"setboot.js")==0)
        //	continue;
        i=0;
        while (ff_blk.ff_name[i]!=0) {
            toupper(ff_blk.ff_name[i]);
            i++;
        }
        strcpy(fileNameArray[fileno],ff_blk.ff_name);
        buildNameArray(dirBuf,ff_blk,fileno);
        fileno++;
        if (fileno>MAX_FILE_SUM) {
            printf("Note : total file number maybe too many (no more than 80)!\n");
            break;
        }		    
    }
    buildFileNameIndex(fileno);
    
    fprintf(fp_wr,"\ntime_t  webCrtTime = %ld;\n",(long)(current_time-current_time1));
    //fprintf(fp_wr,"\ntime_t  webSysTime = %ld;\n",(long)current_time1);
    
    fprintf(fp_wr,"\n");    
    
    fprintf(fp_wr,"/*******************************************\n");
    fprintf(fp_wr," * End Of CS_FILE.\n");
    fprintf(fp_wr," ******************************************/\n");

    fclose(fp_wr);
    exit (1);
    return 1;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -