c 語言 解析ini文件爲xml

 
/*
 解析ini文件
 */

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

int main(void) {

 FILE *fp_in, *fp_out;
 if ((fp_in = fopen("host.ini", "r")) == NULL) {
  perror("");
  return -1;
 }
 fp_out = fopen("host.xml", "w");

 char buff_in[1024];
 char buff_out[1024];
 char auto_add[50];
 int auto_flag = 0;
 int activXml = 0;

 while (fgets(buff_in, 1024, fp_in) != NULL) {

  if (buff_in[0] == 10) {
   if (auto_flag) {
    fputs(auto_add, fp_out);
    memset(auto_add, '\0', 50);
   }
   auto_flag = 0;
   activXml = 0;
  } else if (buff_in[0] == ';') //;Configuration of http
  {
   if (auto_flag) {
    fputs(auto_add, fp_out);
    memset(auto_add, '\0', 50);
   }
   strcpy(buff_out, "<!-- ");
   strncat(buff_out, buff_in + 1, strlen(buff_in) - 2);
   strcat(buff_out, " -->\n");
   auto_flag = 0;
   activXml = 1;

  } else if (buff_in[0] == '[') //[http] --> <http>
  {
   strcpy(buff_out, "<");
   strncat(buff_out, buff_in + 1, strlen(buff_in) - 3);
   strcat(buff_out, ">\n");
   auto_add[0] = '<';
   auto_add[1] = '/';
   strcpy(auto_add + 2, buff_out + 1); //</http>
   auto_flag = 0;
   activXml = 1;

  } else if (buff_in[0] != ' ') {
   char *p = buff_in;
   char *q = buff_out;
   strncpy(buff_out + 1, buff_in, strlen(buff_in) - 1);
   buff_out[0] = '<';
   while (*p != '=') //domain=www.mysite.com
   {
    p++;
    q++;
   }
   *(q + 1) = '>';
   char end[50];
   p = end + 2;
   q = buff_out + 1;
   end[0] = '<';
   end[1] = '/';
   while (*q != '>') {
    *p++ = *q++;
   }
   *p = '>';
   strcat(p, "\n");
   strcat(buff_out + strlen(buff_out), end);
   memset(end, '\0', 50);
   auto_flag = 1;
   activXml = 1;
  }

  if (activXml) {
   fputs(buff_out, fp_out);
  }
  memset(buff_out, '\0', 1024); //清空緩衝區
 }
 fclose(fp_in);
 fclose(fp_out);

 return 0;
}

 


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章