Chinaunix首页 | 论坛 | 博客
  • 博客访问: 567432
  • 博文数量: 127
  • 博客积分: 1169
  • 博客等级: 少尉
  • 技术积分: 1298
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-16 14:29
个人简介

空白

文章分类

全部博文(127)

分类: C/C++

2012-09-28 23:40:35


  1. /*
  2.  * A simple code for test
  3.  * create time: 2012-09-28
  4.  *
  5.  */

  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <stdlib.h>
  9. #include <unistd.h>
  10. #include <fcntl.h> /* for EINTR */
  11. #include <sys/types.h> /* for stat */
  12. #include <sys/stat.h> /* for stat */
  13. #include <errno.h> /* for errno */

  14. /* MACROS define */
  15. #define MAX_FILE_BUF_SIZE (2 * 1024)
  16. #define MAX_DST_FILE_BUF_SIZE (2 * 1024)

  17. #define FILE_PATH "test.txt"

  18. /* global variable define */
  19. char g_cFileBuffer[MAX_FILE_BUF_SIZE] = {0};
  20. char g_cDstFileBuffer[MAX_DST_FILE_BUF_SIZE] = {0};

  21. /* local functions */
  22. static int readn(int ifilefd, void *vptr, int ireadsize)
  23. {
  24.     int ileft = 0;
  25.     int iread = 0;

  26.     char *cptr = NULL;

  27.     cptr = vptr;
  28.     ileft = ireadsize;

  29.     while (ileft > 0)
  30.     {
  31.         if ((iread = read(ifilefd, cptr, ileft)) < 0)
  32.         {
  33.             if (errno == EINTR)
  34.             {
  35.                 iread = 0;
  36.             }
  37.             else
  38.             {
  39.                 return -1;
  40.             }
  41.         }
  42.         else if (iread == 0)
  43.         {
  44.             break;
  45.         }
  46.         
  47.         ileft -= iread;
  48.         cptr += iread;
  49.     }

  50.     return (ireadsize - ileft); /* 返回读取到的字节数 */
  51. }

  52. static int writen(int ifilefd, const void *cvptr, int iwritesize)
  53. {
  54.     int ileft = 0;
  55.     int iwrite = 0;

  56.     const char *ccptr = NULL;

  57.     ccptr = cvptr;
  58.     ileft = iwritesize;

  59.     while (ileft > 0)
  60.     {
  61.         if ((iwrite = write(ifilefd, ccptr, ileft)) <= 0)
  62.         {
  63.             if (iwrite < 0 && errno == EINTR)
  64.             {
  65.                 iwrite = 0;
  66.             }
  67.             else
  68.             {
  69.                 return -1;
  70.             }
  71.         }

  72.         ileft -= iwrite;
  73.         ccptr += iwrite;
  74.     }

  75.     return iwritesize;
  76. }

  77. /*
  78.  * return value:
  79.  * -1: pfilebuf is null
  80.  * -2: pfilepath is null
  81.  * -3: open file fail
  82.  * >0: read file length
  83.  *
  84.  **/
  85. int ReadFile(char *pfilebuf, int ibuffsize, char *pfilepath)
  86. {
  87.     int ifilefd = -1;
  88.     int ifilelen = 0;
  89.     int ireadlen = 0;

  90.     char *ptr = NULL;

  91.     struct stat st;

  92.     if (pfilebuf == NULL)
  93.     {
  94.         printf("pfilebuf is null %d \r\n", __LINE__);
  95.         return -1;
  96.     }

  97.     if (pfilepath == NULL || *pfilepath == '\0')
  98.     {
  99.         printf("pfilepath is null %d \r\n", __LINE__);
  100.         return -2;
  101.     }
  102.     
  103.     ifilefd = open(pfilepath, O_RDONLY);
  104.     
  105.     if (ifilefd < 0)
  106.     {
  107.         printf("open %d fail \r\n", pfilepath);
  108.         return -3;
  109.     }

  110.     if (stat(pfilepath, &st) == 0)
  111.     {
  112.         ifilelen = st.st_size;
  113.     }

  114.     if (ifilelen > ibuffsize)
  115.     {
  116.         ifilelen = ibuffsize;
  117.     }

  118.     ireadlen = readn(ifilefd, pfilebuf, ifilelen);

  119.     close(ifilefd);

  120.     return ireadlen;
  121. }

  122. /**
  123.  *
  124.  * return value:
  125.  * -1: pbuf is null
  126.  * -2: pfilepath is null
  127.  * -3: open file fail
  128.  * >0: write data len
  129.  *
  130.  **/

  131. int WriteFile(char *pbuf, int iwritesize, char *pfilepath)
  132. {
  133.     int ifilefd = -1;
  134.     int iwritelen = 0;

  135.     char *ptr = NULL;

  136.     if (pbuf == NULL)
  137.     {
  138.         printf("pBuf is null %d \r\n", __LINE__);
  139.         return -1;
  140.     }

  141.     if (pfilepath == NULL || *pfilepath == '\0')
  142.     {
  143.         printf("pfilepath is null %d \r\n", __LINE__);
  144.         return -2;
  145.     }

  146.     ifilefd = open(pfilepath, O_RDWR | O_CREAT | O_APPEND, 0777);

  147.     if (ifilefd < 0)
  148.     {
  149.         printf("open %s fail %d \r\n", pfilepath, __LINE__);
  150.         return -3;
  151.     }

  152.     iwritelen = write(ifilefd, pbuf, iwritesize);

  153.     close(ifilefd);

  154.     return iwritelen;
  155. }

  156. /**
  157.  *
  158.  * return value
  159.  * -1: psearch buf is null;
  160.  * -2: psrcbuf is null
  161.  * -3: pdstbuf is null
  162.  * -4: not find the src string
  163.  * 0: find src string success
  164.  *
  165.  **/
  166. int SearchString(char *psearchbuf, int inum, const char *psrcbuf, char *pdstbuf)
  167. {
  168.     int iret = -1;
  169.     int i;
  170.     int ioffset = 0;
  171.     char *ptr = NULL;
  172.     char tempbuf[32] = {0};

  173.     if (psearchbuf == NULL)
  174.     {
  175.         printf("search buf is null %d \r\n", __LINE__);
  176.         iret = -1;
  177.     }

  178.     if (psrcbuf == NULL)
  179.     {
  180.         printf("src buf is null %d \r\n", __LINE__);
  181.         iret = -2;
  182.     }

  183.     if (pdstbuf == NULL)
  184.     {
  185.         printf("dst buf is null %d \r\n", __LINE__);
  186.         iret = -3;
  187.     }
  188.     
  189.     for (i = 0; i < inum; i++)
  190.     {
  191.         memset(tempbuf, 0, sizeof(tempbuf));
  192.         memcpy(tempbuf, psearchbuf + ioffset, 30);
  193.         //ioffset += 30;

  194.         printf("%s \r\n", tempbuf);
  195.         if (strstr(tempbuf, psrcbuf) != NULL)
  196.         {
  197.             memcpy(pdstbuf, tempbuf, 30);
  198.             iret = 0;
  199.             break;
  200.         }
  201.         
  202.         ioffset += 30;
  203.     }

  204. #if 0
  205.     ptr = strstr(psrcbuf, psearchbuf);

  206.     if (ptr == NULL)
  207.     {
  208.         printf("no find string %d \r\n", __LINE__);
  209.         iret = -4;
  210.     }
  211.     else
  212.     {
  213.         printf("%s \r\n", ptr);
  214.         memcpy(pdstbuf, ptr, 30);
  215.         iret = 0;
  216.     }
  217. #endif
  218.     return iret;
  219. }

  220. int main()
  221. {
  222.     int iretvalue = -1;
  223.     int inum = 0;
  224.     char dststring[16] = {0};
  225.     
  226.     snprintf(dststring, sizeof(dststring), "0810812000040");

  227.     memset(g_cFileBuffer, 0, sizeof(g_cFileBuffer));
  228.     memset(g_cDstFileBuffer, 0, sizeof(g_cDstFileBuffer));

  229.     iretvalue = ReadFile(g_cFileBuffer, MAX_FILE_BUF_SIZE, FILE_PATH);
  230.     inum = iretvalue / 30;

  231.     printf("iretvalue %d inum %d %d \r\n", iretvalue, inum, __LINE__);

  232.     if (iretvalue > 0)
  233.     {
  234. #if 1
  235.         printf("g_cFileBuffer: \r\n");
  236.         printf("%s \r\n", g_cFileBuffer);

  237.         printf("dststring: \r\n");
  238.         printf("%s \r\n", dststring);
  239. #endif

  240.         iretvalue = SearchString(g_cFileBuffer, 7, dststring, g_cDstFileBuffer);

  241.         printf("iretvalue %d %d \r\n", iretvalue, __LINE__);

  242.         if (0 == iretvalue)
  243.         {
  244.             printf("find dst string success %d \r\n", __LINE__);

  245.             printf("%s %d \r\n", g_cDstFileBuffer, __LINE__);
  246.         }
  247.         else
  248.         {
  249.             printf("not find dst string %d \r\n", __LINE__);
  250.         }
  251.     }
  252.     else
  253.     {
  254.         printf("read file fail %d \r\n", __LINE__);
  255.     }

  256.     return 0;
  257. }

阅读(1299) | 评论(0) | 转发(0) |
0

上一篇:popen

下一篇:linux下串口gps应用

给主人留下些什么吧!~~