1 #include "rh_string.h"
  2 #include "buffer_readline.h"
  3 
  4 int buffer_readline (struct buffer_readline *readline)
  5 {
  6         const char *temp;
  7 
  8         if (NULL == readline->data || 0 == readline->ndata)
  9                 return -1;
 10         
 11         temp = memchr (readline->data, '\n', readline->ndata);
 12                 
 13         readline->line  = readline->data;
 14 
 15         if (NULL == temp) {
 16                 readline->nline = readline->ndata;
 17                 
 18                 readline->data = NULL;
 19                 readline->ndata = 0;
 20         } else {
 21                 ++readline->lineno;
 22                 
 23                 readline->nline = temp + 1 - readline->line;
 24                 
 25                 readline->ndata -= readline->nline;
 26                 readline->data  += readline->nline;
 27 
 28                 for (   ;
 29                         readline->nline &&
 30                                 ( ('\n' == readline->line[readline->nline-1]) ||
 31                                   ('\r' == readline->line[readline->nline-1]) );
 32                         --readline->nline);
 33         }
 34 
 35         return 0;
 36 }
 37 
 38 void buffer_readline_strip_comment (struct buffer_readline *readline)
 39 {
 40         const char *temp;
 41         
 42         if (NULL == readline->line || 0 == readline->nline)
 43                 return;
 44 
 45         temp = memchr (readline->line, '#', readline->nline);
 46         if (NULL == temp)
 47                 return;
 48 
 49         readline->nline = temp - readline->line;
 50 }
 51 
 52 /*
 53  *
 54  *
 55  * split
 56  *
 57  *
 58  *
 59  */
 60 
 61 void buffer_split_init (struct buffer_readline *readline,
 62                 const char *data, size_t ndata, const char *seperator)
 63 {
 64         TYPE_ZERO(readline);
 65         
 66         readline->data = data;
 67         readline->ndata = ndata;
 68 
 69         for (;*seperator;++seperator) {
 70                 readline->seperator[ (unsigned char)(seperator[0]) ] = 1;
 71         }
 72 }
 73 
 74 int buffer_split (struct buffer_readline *readline)
 75 {
 76         size_t  offset;
 77         
 78         if (NULL == readline->data || 0 == readline->ndata)
 79                 return -1;
 80 
 81         /* left trim */
 82         for (offset = 0; offset < readline->ndata; ++offset) {
 83                 if (0 == readline->seperator[(const unsigned char)(readline->data[offset])])
 84                 {
 85                         break;
 86                 }
 87         }
 88         
 89         if (offset) {
 90                 readline->data  += offset;
 91                 readline->ndata -= offset;
 92         }
 93                 
 94         readline->line = readline->data;
 95         readline->nline = 0;
 96         
 97         /* find end */
 98         for (offset = 0; offset < readline->ndata; ++offset) {
 99                 if (0 != readline->seperator[(const unsigned char)(readline->data[offset])])
100                 {
101                         break;
102                 }
103 
104                 ++readline->nline;
105         }
106         
107         readline->data  += readline->nline;
108         readline->ndata -= readline->nline;
109 
110         return 0;
111 }


syntax highlighted by Code2HTML, v. 0.9.1