SQLite cominbed source file splitter




This program splits the combined version of SQLite source code. See http://www.sqlite.org/ or http://www.sqlite.org/download.html for more of SQLite.


#include <stdio.h>

typedef enum
{
    Error = -1,
    None = 0,
    Common,
    BeginInc,
    EndInc,
    BeginFile,
    EndFile,
} LineType;

typedef int Bool;

typedef struct
{
    FILE    *file;
    char    *fname;
} Writer;

void writeToCur (Writer *cur, char *line)
{
    fprintf(cur->file, "%s", line);
}

void writeIncToCur (Writer *cur, char *chbuf)
{
    fprintf(cur->file, "#include /"%s/"", chbuf);
}

Bool nextEq (char **s, const char *t)
{
    Bool res = strncmp((*s), (t), strlen(t))==0;
    if (res)
    {
        *s += strlen(t);
    }
    return res;
}

void getFileName (char *fname, const char *p)
{
    while (*p != '*' && *p != ' ' && *p != 0)
    {
        *fname++ = *p++;
    }
    *fname = 0;
}

#define LineBufSize 1024

LineType readLine (FILE *file, char *line, char *fname)
{
    int lenLine;
    LineType type;
    if (!fgets(line, LineBufSize - 1, file))
    {
        return None;
    }
    if (strlen(line) == LineBufSize - 1)
    {   // possibly error
        return Error;
    }
    lenLine = strlen(line);
    if (lenLine > 20 && line[0] == '/' && line[1] == '*')
    {
        char *pEnd = line + lenLine;
        char *p = line + 2;
        for ( ; (*p == '*' || *p == ' ') && *p != 0; p++);
        if (p - line < 10)
        {
            return Common;
        }
        if (nextEq(&p, "Begin file "))
        {
            getFileName(fname, p);
            return BeginFile;
        }
        else if (nextEq(&p, "End of "))
        {
            getFileName(fname, p);
            return EndFile;
        }
        else if (nextEq(&p, "Include pager.h in the middle of "))
        {
            getFileName(fname, p);
            return BeginInc;
        }
        else if (nextEq(&p, "Continuing where we left off in "))
        {
            getFileName(fname, p);
            return EndInc;
        }
    }
    return Common;
}

void parse (char *sfname)
{
#define StackSize   32
    char    chbuf[128];
    char    fnbuf[256];
    char    line[LineBufSize];
    Writer  stack[StackSize];
    Writer  *cur;
    int     depth = 0;
    FILE    *srcFile = fopen(sfname, "r");

    cur = stack + depth;
    sprintf(fnbuf, "out/%s", sfname);
    cur->file = fopen(fnbuf, "w");

    while (1)
    {
        LineType type = readLine(srcFile, line, chbuf);
        if (type == Error)
        {
            printf("An error has occurred during parsing./n");
            break;
        }
        else if (type == None)
        {
            break;
        }
        switch (type)
        {
        case Common:
            writeToCur(cur, line);
            break;
        case BeginInc:
            writeIncToCur(cur, chbuf);
            break;
        case EndInc:
            break;
        case BeginFile:
            printf("Found file embedded %s/n", chbuf);
            depth++;
            cur = stack + depth;
            sprintf(fnbuf, "out/%s", chbuf);
            cur->file = fopen(fnbuf, "w");
            writeToCur(cur, line);
            break;
        case EndFile:
            writeToCur(cur, line);
            fclose(cur->file);
            depth--;
            cur = stack + depth;
            break;
        }
        if (depth < 0)
        {
            break;
        }
    }
    for ( ; depth >= 0; depth--)
    {
        cur = stack + depth;
        fclose(cur->file);
    }
}

int main (void)
{
    parse("sqlite3.c");
    return 0;
}
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值