]> git.ipfire.org Git - thirdparty/sarg.git/blame - longline.c
Update the messages when an error is detected while reading a line
[thirdparty/sarg.git] / longline.c
CommitLineData
4ca814cc
FM
1/*
2 * SARG Squid Analysis Report Generator http://sarg.sourceforge.net
61d965f3 3 * 1998, 2012
4ca814cc
FM
4 *
5 * SARG donations:
6 * please look at http://sarg.sourceforge.net/donations.php
7 * Support:
8 * http://sourceforge.net/projects/sarg/forums/forum/363374
9 * ---------------------------------------------------------------------
10 *
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.
15 *
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.
20 *
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.
24 *
25 */
26
27#include "include/conf.h"
28#include "include/defs.h"
29
30#define INITIAL_LINE_BUFFER_SIZE 32768
31
afaa3b67 32struct longlinestruct
4ca814cc 33{
9bd92830
FM
34 //! The buffer to store the data read from the log file.
35 char *buffer;
36 //! The size of the buffer.
37 size_t size;
38 //! The number of bytes stored in the buffer.
39 size_t length;
40 //! The position of the beginning of the current string.
41 size_t start;
42 //! The position of the end of the current string.
43 size_t end;
afaa3b67
FM
44};
45
46longline longline_create(void)
47{
9bd92830 48 longline line;
afaa3b67 49
9bd92830
FM
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) {
55 free(line);
56 return(NULL);
57 }
58 line->start=0;
59 line->end=0;
60 line->length=0;
61 return(line);
4ca814cc
FM
62}
63
afaa3b67
FM
64void longline_reset(longline line)
65{
9bd92830
FM
66 if (line!=NULL) {
67 line->start=0;
68 line->end=0;
69 line->length=0;
70 }
afaa3b67
FM
71}
72
73char *longline_read(FILE *fp_in,longline line)
4ca814cc 74{
9bd92830
FM
75 int i;
76 char *newbuf;
77 size_t nread;
4ca814cc 78
9bd92830 79 if (line==NULL || line->buffer==NULL) return(NULL);
4ca814cc 80
9bd92830
FM
81 while (true) {
82 for (i=line->end ; i<line->length && (line->buffer[i]=='\n' || line->buffer[i]=='\r') ; i++);
83 if (i<line->length) {
84 line->end=i;
85 break;
86 }
87 nread=(feof(fp_in)!=0) ? 0 : fread(line->buffer,1,line->size,fp_in);
88 if (nread==0) return(NULL);
89 line->length=nread;
90 line->end=0;
91 }
4ca814cc 92
9bd92830
FM
93 line->start=line->end;
94 while (true) {
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;
98 }
7a0a624b 99
9bd92830
FM
100 line->end=i;
101 if (line->end<line->length) break;
4ca814cc 102
9bd92830
FM
103 if (line->start>0) {
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;
107 line->start=0;
108 }
109 if (line->length>=line->size) {
110 line->size+=8192;
111 newbuf=realloc(line->buffer,line->size);
112 if (!newbuf) {
110ed1b4 113 debuga(_("Not enough memory to read one more line from the file\n"));
9bd92830
FM
114 exit(EXIT_FAILURE);
115 }
116 line->buffer=newbuf;
117 }
118 nread=(feof(fp_in)!=0) ? 0 : fread(line->buffer+line->length,1,line->size-line->length,fp_in);
119 if (nread==0) {
120 if (line->end<=line->start) return(NULL);
121 if (line->end>=line->size) {
122 line->end=line->size;
123 line->size++;
124 newbuf=realloc(line->buffer,line->size);
125 if (!newbuf) {
110ed1b4 126 debuga(_("Not enough memory to read one more line from the file\n"));
9bd92830
FM
127 exit(EXIT_FAILURE);
128 }
129 line->buffer=newbuf;
130 }
131 line->buffer[line->end]='\0';
132 return(line->buffer+line->start);
133 }
134 line->length+=nread;
135 }
136 line->buffer[line->end++]='\0';
137 return(line->buffer+line->start);
4ca814cc
FM
138}
139
afaa3b67 140void longline_destroy(longline *line_ptr)
4ca814cc 141{
9bd92830 142 longline line;
afaa3b67 143
9bd92830
FM
144 if (line_ptr==NULL || *line_ptr==NULL) return;
145 line=*line_ptr;
146 *line_ptr=NULL;
147 if (line->buffer!=NULL) free(line->buffer);
148 free(line);
4ca814cc 149}