Chinaunix首页 | 论坛 | 博客
  • 博客访问: 203765
  • 博文数量: 63
  • 博客积分: 2764
  • 博客等级: 少校
  • 技术积分: 620
  • 用 户 组: 普通用户
  • 注册时间: 2009-12-28 21:11
文章分类

全部博文(63)

文章存档

2011年(35)

2010年(28)

我的朋友

分类: LINUX

2010-07-18 19:14:31

阶段小练习:转换。ini文件成XML文件

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 1024

// insert into dest string on left ++ num charconsers;

char *insert_string(char *dest, int num, const char *str)
{
        int dlen, slen, i, count;
        dlen = strlen(dest);
        slen = strlen(str);
        count = dlen - num;
        for(i = 0; i <= count; i++)
        {
                dest[dlen+slen-i] = dest[dlen-i];
        }
        dest[dlen+slen+1] = '\0';
        for(i = 0; i < slen; i++)
        {
                dest[i+num] = str[i];
        }
        return dest;
}
char *findstr(char *dest, char *str, char str1, char str2)
{
        int i,cp,j = 0;
        int len;
        len = strlen(str);
        for(i = 0; i < len; i++)
        {
                if(str[i] == str1) cp = 1, i++;
                if(str[i] == str2)
                {
                        cp = 0;
                        j++;
                        break;
                }
                if(cp == 1)
                {
                        dest[j] = str[i];
                        j++;
                }
        }
        dest[j] = '\0';
        return dest;

}


void delspace(char str[], int len)
{
        int i,j = 0;

        for(i = 0; i < len; i++)
        {
                if(str[i] == ' ') i++;
                str[j] = str[i];
                j++;
        }
        str[j] = '\0';
        return ;
}


int main(int argc, char *argv[])
{
        if(argc < 3)
        {
                printf("Usag: two name.ini name.xml\n");
                exit(1);
        }
        FILE *in;
        FILE *out;
        int i = 0, len;
        char buf[SIZE];
        char buf2[SIZE] = "abcdef";
        char buf3[SIZE] = "1234";
        char *p2, *p3;
        p2 = buf2;
        p3 = buf3;

        in = fopen(argv[1], "r");
        if(in == NULL)
        {
                perror("Open file argv[1]");
                exit(1);
        }

        out = fopen(argv[2], "w+");
        if(in == NULL)
        {
                perror("Open file argv[1]");
                exit(1);
        }

        while(fgets(buf, 1024, in) != NULL)
        {
// printf("buf=%s\n",buf);

                if(strcmp(buf,"\n") == 0)
                {
                        insert_string(buf,0,buf2);
                        insert_string(buf,1,"/");
                        fputs(buf,stdout);
                        fputs(buf,out);
                }
                i = 0;
                while(buf[i] != '\0')
                {

                        switch(buf[i]){
                        case ';':
                                buf[i] = ' ';
                                insert_string(buf,0,"");
                                fputs(buf,stdout);
                                fputs(buf,out);
                                break;
                        case '[':
                                buf[i] = '<';
                                len = strlen(buf);
                                buf[len-2] = '>';
                                strcpy(buf2, buf);
                                fputs(buf,stdout);
                                fputs(buf,out);
                                break;
                        case '=':
                                buf[i] = '>';
                                insert_string(buf,0,"\t<");
                                findstr(buf3,buf,'<','>');
                                len = strlen(buf);
                                insert_string(buf,len-1,buf3);
                                insert_string(buf,len-1,");
                                len = strlen(buf);
                                insert_string(buf,len-1,">");
                                len = strlen(buf);
                                delspace(buf,len);
                                fputs(buf,stdout);
                                fputs(buf,out);
                                break;
                        default :
                                break;
                        }
                i++;
                }
        }
        buf[0]= '\0';
        insert_string(buf,0,buf2);
        insert_string(buf,1,"/");
        fputs(buf,stdout);
        fputs(buf,out);


        fclose(in);
        fclose(out);
        return 0;
}


阅读(1503) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~