mt_aq.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <regex.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <unistd.h>
  7. #include <string.h>
  8. #include "mt_mt.h"
  9. #define LINEMAX 256
  10. #define AQOSDSETTINGVER_LEN 32
  11. int parseAQ(Item_t *pItem)
  12. {
  13. FILE *fp = NULL;
  14. FILE *binfp = NULL;
  15. //int retval;
  16. //long filelen = 0;
  17. char buf[LINEMAX];
  18. unsigned char *pOutBuf;
  19. //int datasize= 0;
  20. int line=0;
  21. int retval = 0;
  22. int value;
  23. int databyte;
  24. char AQOSDSetting_ver[AQOSDSETTINGVER_LEN];
  25. #if WRITEITEMTOFILE
  26. char binFileName[256];
  27. #endif
  28. pOutBuf = (unsigned char *)malloc(MAXBINSIZE);
  29. if (pOutBuf == NULL)
  30. {
  31. fprintf(stderr, "Unable alloc pOutBuf buffer\n");
  32. return -1;
  33. }
  34. pItem->pData = pOutBuf;
  35. pItem->size = 0;
  36. if (pItem->filepath != NULL && pOutBuf!= NULL)
  37. {
  38. printf("%s 'open file %s\n", __FUNCTION__, pItem->filepath);
  39. fp = fopen(pItem->filepath, "rb");;
  40. #if WRITEITEMTOFILE
  41. strcpy(binFileName, pItem->name);
  42. binfp = fopen(strcat(binFileName,".bin"), "wb");
  43. if (fp != NULL && binfp != NULL)
  44. #else
  45. if (fp != NULL)
  46. #endif
  47. {
  48. char *pch;
  49. //int reti;
  50. //regmatch_t match[2];
  51. /* Compile regular expression */
  52. //reti = regcomp(&regex, "^ItemFile[ \t]*=[ \t]*\"[a-zA-Z0-9._/\\]*\"[ \t]*", 0);
  53. //reti = regcomp(&regex, "^[ \t]*#[.]*", 0);
  54. //if( reti ){ fprintf(stderr, "Could not compile regex\n"); exit(1); }
  55. /* Free compiled regular expression if you want to use the regex_t again */
  56. //fgets (buf , LINEMAX , fp);
  57. //line++;
  58. if (fgets (buf , LINEMAX , fp) == NULL)
  59. {
  60. fprintf(stderr, "%s %d fgets error!\n", __FUNCTION__, __LINE__);
  61. #if WRITEITEMTOFILE
  62. fclose(binfp);
  63. #endif
  64. fclose(fp);
  65. free(pOutBuf);
  66. return -1;
  67. }
  68. pch = strtok (buf," @\t\r\n");
  69. pch = strtok (NULL, " @\t\r\n");
  70. pch = strtok (NULL, " @\t\r\n");
  71. if (strlen(pch) <=32)
  72. {
  73. //fprintf(stderr, "strlen(pch) = %d\n", strlen(pch));
  74. memset(AQOSDSetting_ver,0,32);
  75. memcpy(AQOSDSetting_ver, pch, strlen(pch));
  76. //fwrite(PQOSDSetting_ver, sizeof(char)*AQOSDSETTINGVER_LEN, 1, binfp);
  77. writeOutputBuf(AQOSDSetting_ver, sizeof(char)*AQOSDSETTINGVER_LEN, &pOutBuf, binfp);
  78. pItem->size += sizeof(char)*AQOSDSETTINGVER_LEN;;
  79. }
  80. else
  81. {
  82. //fprintf(stderr, "PQOSDSetting_ver len > AQOSDSETTINGVER_LEN, strip it\n");
  83. //fprintf(stderr, "strlen(pch) = %d\n", strlen(pch));
  84. memset(AQOSDSetting_ver,0,AQOSDSETTINGVER_LEN);
  85. memcpy(AQOSDSetting_ver, pch, AQOSDSETTINGVER_LEN);
  86. //fwrite(AQOSDSetting_ver, sizeof(char)*AQOSDSETTINGVER_LEN, 1, binfp);
  87. writeOutputBuf(AQOSDSetting_ver, sizeof(char)*AQOSDSETTINGVER_LEN, &pOutBuf, binfp);
  88. pItem->size += sizeof(char)*AQOSDSETTINGVER_LEN;
  89. }
  90. line++;
  91. //fgets (buf , LINEMAX , fp);
  92. //line++;
  93. while( fgets (buf , LINEMAX , fp) != NULL )
  94. {
  95. short data_word;
  96. char data_byte;
  97. line++;
  98. //reti = regexec(&regex, buf, 1, match, 0);
  99. //if( !reti ){
  100. // puts("Match");
  101. // printf("==>%s", buf);
  102. //}
  103. //else if( reti == REG_NOMATCH ){
  104. // puts("No match");
  105. // printf("==>%s", buf);
  106. //}
  107. //else{
  108. // //regerror(reti, &regex, msgbuf, sizeof(msgbuf));
  109. // fprintf(stderr, "Regex match failed:\n");
  110. //}
  111. pch = strtok (buf,"@");
  112. pch = strtok (NULL, " @\t\r\n");
  113. value = atoi(pch);
  114. //printf ("%s\n",pch);
  115. pch = strtok (NULL, " @\t\r\n");
  116. //printf ("%s\n",pch);
  117. databyte = atoi(pch);
  118. switch (databyte)
  119. {
  120. case 1:
  121. data_byte = (char) value;
  122. //fwrite(&data_byte, sizeof(char), 1, binfp);
  123. writeOutputBuf(&data_byte, sizeof(char), &pOutBuf, binfp);
  124. pItem->size += sizeof(char);
  125. break;
  126. case 2:
  127. data_word = (short) value;
  128. //fwrite(&data_word, sizeof(short), 1, binfp);
  129. writeOutputBuf(&data_word, sizeof(short), &pOutBuf, binfp);
  130. pItem->size += sizeof(short);
  131. break;
  132. case 3:
  133. data_byte = (char) (value & 0xff);
  134. data_word = (short) (value>>8);
  135. //fwrite(&data_byte, sizeof(char), 1, binfp);
  136. //fwrite(&data_word, sizeof(short), 1, binfp);
  137. writeOutputBuf(&data_byte, sizeof(char), &pOutBuf, binfp);
  138. writeOutputBuf(&data_word, sizeof(short), &pOutBuf, binfp);
  139. pItem->size += sizeof(char)+ sizeof(short);
  140. break;
  141. case 4:
  142. //fwrite(&value, sizeof(int), 1, binfp);
  143. writeOutputBuf(&value, sizeof(int), &pOutBuf, binfp);
  144. pItem->size += sizeof(int);
  145. break;
  146. }
  147. }
  148. //regfree(&regex);
  149. }
  150. else
  151. {
  152. perror("Can't open file");
  153. }
  154. }
  155. //for 4 bytes aligment
  156. pItem->dummyByteNums = 0;
  157. if ((pItem->size % 4) != 0)
  158. {
  159. pItem->dummyByteNums = 4 - (pItem->size % 4);
  160. }
  161. if (fp != NULL)
  162. {
  163. fclose(fp);
  164. }
  165. if (binfp != NULL)
  166. {
  167. fflush(binfp);
  168. fclose(binfp);
  169. }
  170. return retval;
  171. }