Linux C 编程一站式学习 第25章 C标准库 综合练习二题解 文件格式转换

来源:互联网 发布:java正则表达式语法 编辑:程序博客网 时间:2024/05/21 10:05

/**************************************************
test.ini file is:
;Configuration of http
[http]
domain=www.mysite.com
port=8080
cgihome=/cgi-bin
   
;Configuration of db
[database]
server = mysql
user = myname
password = toopendatabase

After conveter test.xml file is:
<!-- Configuration of http -->
<http>
  <domain>www.mysite.com</domain>
  <port>8080</port>
  <cgihome>/cgi-bin</cgihome>
</http>

<!-- Configuration of db -->
<database>
  <server>mysql</server>
  <user>myname</user>
  <password>toopendatabase</password>
</database>
   
***************************************************/

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>

int isEmptyLine(char *str);
char* delStartLastSpace(char *str);

void change_Ini2Xml(char *srcfile, char *destfile)
{
    FILE *srcfp = NULL;
    FILE *destfp = NULL;
    char buf[81];
    char *str = NULL;
    char *head = NULL;
    char headbuf[80];
    int  condition = 1;
    int index = 0;
    int ch = 0;
  
    srcfp = fopen(srcfile, "r");
    if (srcfp == NULL)
    {
        printf("Open the source file error!\n");
        exit(1);   
    }
   
    destfp = fopen(destfile, "w");
    if (destfp == NULL)
    {
        printf("Open the destination file error!\n");
        exit(1);   
    }
   
    while (condition)
    {
         ch = fgetc(srcfp);
         if (EOF == ch) /* if read the end of the file set condition to 0 */
         {
            condition = 0;  
         }
         buf[index++] = ch;
         if (('\n' == ch) || ('\0' == ch) || (EOF == ch))  /* have read a line */
         {
            buf[--index] = '\0';
            index = 0;
            if (isEmptyLine(buf)) /* check whither the line is empty? */
            {
                if (head != NULL)
                {
                    fprintf(destfp, "</%s>\n", head);
                    head = NULL;
                }
                fprintf(destfp, "\n");   
            }
            else
            {
                switch (buf[0])
                {
                    case ';':
                        str = strtok(buf, ";");
                        fprintf(destfp, "<!-- %s-->\n", str);/* string has a 回车 at last */
                        break;
                    case '[':
                        str = strtok(buf + 1, "]");
                        strcpy(headbuf, str);
                        head = headbuf;
                        fprintf(destfp, "<%s>\n", str);
                        break;
                       
                    default:
                        str = strtok(buf, "="); /* get the section name */
                        str = delStartLastSpace(str);  /* delete the start and end space of the string */
                        fprintf(destfp, "\t\t<%s>", str);
                       
                        str = strtok(NULL, "=");
                        str = delStartLastSpace(str);
                        while (*str != '\0') /* the last line of file end with '\0'  */
                        {
                            /* the line isn't the last line of the file end with 回车,it's asscii code is 0x0D */
                            if (*str != 0x0D) /* if the char is not 回车,write to destfp */
                            {
                                fputc(*str, destfp);                                                 
                            }
                            str++;
                        }
                                            
                        str = strtok(buf, "=");
                        str = delStartLastSpace(str);
                        fprintf(destfp, "</%s>\n", str);
                        break;  
                }
              }     
            }
                         
    }
    printf("%s--->%s\nThe convert success!\n", srcfile, destfile);
    if (head != NULL)
    {
        fprintf(destfp, "</%s>\n", head);/* write the end of the section name */
    }
    fclose(srcfp);
    fclose(destfp);             
}

/*******************************************************
 return 0: the string is not empty.
 return 1: the string is empty.
 empty string is contain zero or many space characters
 and tab characters.
 note:
 the line isn't the last line of the file end with 回车,
 it's asscii code is 0x0D.
 ********************************************************/
int isEmptyLine(char *str)

    while (*str != '\0')
    {
        //printf("%c %x\n", *str, *str);
        if ((*str != ' ') && (*str != '\t') && (*str != 0x0D) && (*str != '\n'))
        {
            return 0;   
        }
        str++;       
    }
    return 1;  
}

char* delStartLastSpace(char *str)
{
    char *temp = NULL;
    int len = 0, i = 0;
    temp = str;
   
    while (temp[i] != '\0')
    {
        if ((' ' == temp[i]) || ('\t' == temp[i])) /* delelte the start space */
        {
            i++;
        }
        else
        {
            temp[len++] = temp[i++];
        }
    }
   
    --len;
    while ((' ' == str[len]) || ('\t' == str[len]) )/* delelte the last space */
    {
        --len;   
    }
    str[++len] = '\0';
   
    return str;
}

int main(void)
{
    change_Ini2Xml("test.ini", "test.xml");
   
    return 0;
}