]>
git.ipfire.org Git - thirdparty/sarg.git/blob - longline.c
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
6 * please look at http://sarg.sourceforge.net/donations.php
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
27 #include "include/conf.h"
28 #include "include/defs.h"
30 #define INITIAL_LINE_BUFFER_SIZE 32768
34 //! The buffer to store the data read from the log file.
36 //! The size of the buffer.
38 //! The number of bytes stored in the buffer.
40 //! The position of the beginning of the current string.
42 //! The position of the end of the current string.
46 longline
longline_create(void)
50 line
=malloc(sizeof(*line
));
51 if (line
==NULL
) return(NULL
);
52 line
->size
=INITIAL_LINE_BUFFER_SIZE
;
53 line
->buffer
=malloc(line
->size
);
54 if (line
->buffer
==NULL
) {
64 void longline_reset(longline line
)
73 char *longline_read(FILE *fp_in
,longline line
)
79 if (line
==NULL
|| line
->buffer
==NULL
) return(NULL
);
82 for (i
=line
->end
; i
<line
->length
&& (line
->buffer
[i
]=='\n' || line
->buffer
[i
]=='\r') ; i
++);
87 nread
=(feof(fp_in
)!=0) ? 0 : fread(line
->buffer
,1,line
->size
,fp_in
);
88 if (nread
==0) return(NULL
);
93 line
->start
=line
->end
;
95 for (i
=line
->end
; i
<line
->length
; i
++) {
96 if ((unsigned char)line
->buffer
[i
]>=' ') continue;
97 if (line
->buffer
[i
]=='\n' || line
->buffer
[i
]=='\r') break;
101 if (line
->end
<line
->length
) break;
104 for (i
=line
->start
; i
<line
->length
; i
++) line
->buffer
[i
-line
->start
]=line
->buffer
[i
];
105 line
->length
-=line
->start
;
106 line
->end
-=line
->start
;
109 if (line
->length
>=line
->size
) {
111 newbuf
=realloc(line
->buffer
,line
->size
);
113 debuga(_("Not enough memory to read one more line from the input log file\n"));
118 nread
=(feof(fp_in
)!=0) ? 0 : fread(line
->buffer
+line
->length
,1,line
->size
-line
->length
,fp_in
);
120 if (line
->end
<=line
->start
) return(NULL
);
121 if (line
->end
>=line
->size
) {
122 line
->end
=line
->size
;
124 newbuf
=realloc(line
->buffer
,line
->size
);
126 debuga(_("Not enough memory to read one more line from the input log file\n"));
131 line
->buffer
[line
->end
]='\0';
132 return(line
->buffer
+line
->start
);
136 line
->buffer
[line
->end
++]='\0';
137 return(line
->buffer
+line
->start
);
140 void longline_destroy(longline
*line_ptr
)
144 if (line_ptr
==NULL
|| *line_ptr
==NULL
) return;
147 if (line
->buffer
!=NULL
) free(line
->buffer
);