C傻瓜式模板文件生成

一般编写的C模块都要开发出接口,提供给其他模块使用。

自己写了一个mcm的程序,轻松一下。


mcm.c内容如下:

// import interface.
#include "mcm.h"


static char g_cbuf_InterfaceFile[] = "#ifndef %s\r\n"
"#define %s\r\n\r\n"


"// import interface.\r\n\r\n\r\n"


"// export interface.\r\n"


"#ifdef __cplusplus\r\n"
"extern \"C\"\r\n{\r\n"
"#endif\r\n\r\n"
"\r\n"
"#ifdef __cplusplus\r\n"
"}\r\n"
"#endif\r\n\r\n"


"#endif\r\n";


static char g_cbuf_SourceFile[] = "// import interface.\r\n"
"#include \"%s\"";




#define buf_size 512
int main(int i_argc, char* pcbuf_argv[])
{
    if (i_argc != 2)
    {
        printf("\"make c module\" or \"mcm\" create a simple c module frame source file,\r\n"
                "and it's very simple! author: 2524322562@qq.com.\r\n\r\n");
        printf("usage: %s module name\r\n", pcbuf_argv[0]);
        return 0;
    }


    {
       
        FILE* InterfaceFile = NULL; // c header file.
        FILE* SourceFile = NULL; // c source file.
        char cbuf_check1[buf_size], cbuf_check2[buf_size];


        sprintf(cbuf_check1, "%s.h", pcbuf_argv[1]);
        sprintf(cbuf_check2, "%s.c", pcbuf_argv[1]);


        if (NULL == (InterfaceFile = fopen(cbuf_check1, "r")) && NULL == (SourceFile = fopen(cbuf_check2, "r")))
        {
            // file don't exists, create new file.
            char cbuf_tmp[buf_size];


            InterfaceFile = fopen(cbuf_check1, "wb");
            SourceFile =  fopen(cbuf_check2, "wb");


            {
                char cbuf_module[buf_size];
                sprintf(cbuf_module, "%s_h", pcbuf_argv[1]);
                sprintf(cbuf_tmp, g_cbuf_InterfaceFile, cbuf_module, cbuf_module);
                fwrite(cbuf_tmp, strlen(cbuf_tmp), 1, InterfaceFile);


                sprintf(cbuf_module, "%s.h", pcbuf_argv[1]);
                sprintf(cbuf_tmp, g_cbuf_SourceFile, cbuf_module);
                fwrite(cbuf_tmp, strlen(cbuf_tmp), 1, SourceFile);


                fclose(InterfaceFile);
                fclose(SourceFile);
            }
        }
        else
        {
            printf("c module \"%s\" file already exists.\r\n", pcbuf_argv[1]);
            if (InterfaceFile != NULL)
            {
                fclose(InterfaceFile);
            }
            if (SourceFile != NULL)
            {
                fclose(SourceFile);
            }
            return -1;
        }
    }
    return 0;
}

mcm.h文件内容如下:

#ifndef mcm_h
#define mcm_h


// import interface.
#include <stdio.h>
#include <assert.h>


// export interface.
#ifdef __cplusplus
extern "C"
{
#endif




#ifdef __cplusplus
}
#endif


#endif

编译脚本bd.cmd如下:

@echo off
echo + batch script for windows sdk(c version).
echo + usage      : type "bd" or "bd NDEBUG" or "bd clean".
echo + "bd"       : build debug version.
echo + "bd NDEBUG": build release version.
echo + "bd clean" : delete result files.

if "%1" equ "clean" goto clean

if "%1" neq "" (if "%1" equ "NDEBUG" (echo + & echo + build release version. & echo + & goto release) else (echo + & echo + invalidate parameter. & echo + & goto end)) else (echo + & echo + build debug version. & echo + & goto debug)

:clean
echo + & echo + delete result files. & echo + & del *.obj & del *.exe & del *.res & del *.exp & del *.lib & del *.dll & del *.manifest
goto end

:release
cl /MD /D%1 mcm.c
goto end

:debug
cl /MDd mcm.c
goto end


:end
if exist mcm.exe.manifest mt -manifest mcm.exe.manifest -outputresource:mcm.exe
del *.manifest



编译执行如下:

D:\bd\inst\bs\mcm>dir
 驱动器 D 中的卷是 program
 卷的序列号是 0433-59C8


 D:\bd\inst\bs\mcm 的目录


2011-11-24  16:39    <DIR>          .
2011-11-24  16:39    <DIR>          ..
2011-11-10  09:24               859 bd.cmd
2011-11-24  16:35             2,365 mcm.c
2011-11-24  16:09               207 mcm.h
               3 个文件          3,431 字节
               2 个目录 60,737,548,288 可用字节


D:\bd\inst\bs\mcm>bd NDEBUG
+ batch script for windows sdk(c version).
+ usage      : type "bd" or "bd NDEBUG" or "bd clean".
+ "bd"       : build debug version.
+ "bd NDEBUG": build release version.
+ "bd clean" : delete result files.
+
+ build release version.
+
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 14.00.50727.762 for 80x86


Copyright (C) Microsoft Corporation.  All rights reserved.


mcm.c
Microsoft (R) Incremental Linker Version 8.00.50727.762
Copyright (C) Microsoft Corporation.  All rights reserved.


/out:mcm.exe
mcm.obj
Microsoft (R) Manifest Tool version 5.2.3790.2075
Copyright (c) Microsoft Corporation 2005.
All rights reserved.


D:\bd\inst\bs\mcm>mcm
"make c module" or "mcm" create a simple c module frame source file,
and it's very simple! author: 2524322562@qq.com.


usage: mcm module name


D:\bd\inst\bs\mcm>mcm test


D:\bd\inst\bs\mcm>more test.h
#ifndef test_h
#define test_h


// import interface.




// export interface.
#ifdef __cplusplus
extern "C"
{
#endif




#ifdef __cplusplus
}
#endif


#endif


D:\bd\inst\bs\mcm>more test.c
// import interface.
#include "test.h"


D:\bd\inst\bs\mcm>
源码打包在CSDN资源频道: http://download.csdn.net/detail/qs_coding/3837607

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值