From: Frédéric Marchal Date: Wed, 17 Mar 2010 13:29:48 +0000 (+0000) Subject: Move longline module to a separate file X-Git-Tag: v2.3-pre2~45 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=4ca814cc4f85e1d3ce3c0f8bfb99cc8c63e43457;p=thirdparty%2Fsarg.git Move longline module to a separate file --- diff --git a/CMakeLists.txt b/CMakeLists.txt index e7c988d..775ca6f 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -53,7 +53,7 @@ SET(SRC util.c log.c report.c topuser.c email.c sort.c html.c smartfilter.c denied.c authfail.c charset.c squidguard_log.c squidguard_report.c auth.c download.c grepday.c dansguardian_log.c dansguardian_report.c realtime.c btree_cache.c - usertab.c userinfo.c) + usertab.c userinfo.c longline.c) FOREACH(f ${SRC}) ADD_FILE_DEPENDENCIES(${f} ${CMAKE_BINARY_DIR}/config.h ${CMAKE_SOURCE_DIR}/include/conf.h ${CMAKE_SOURCE_DIR}/include/info.h ${CMAKE_SOURCE_DIR}/include/defs.h) diff --git a/Makefile.in b/Makefile.in index d481fb9..29c95f3 100644 --- a/Makefile.in +++ b/Makefile.in @@ -38,7 +38,7 @@ SRCS = util.c log.c report.c topuser.c email.c sort.c html.c \ smartfilter.c denied.c authfail.c charset.c \ squidguard_log.c squidguard_report.c auth.c download.c grepday.c \ dansguardian_log.c dansguardian_report.c realtime.c btree_cache.c \ - usertab.c userinfo.c + usertab.c userinfo.c longline.c OBJS = $(patsubst %.c,%.o,$(SRCS)) diff --git a/documentation/longline.txt b/documentation/longline.txt new file mode 100644 index 0000000..95d8359 --- /dev/null +++ b/documentation/longline.txt @@ -0,0 +1,59 @@ +/*! \file util.c +\brief Various useful functions. +*/ + + +/*! \def INITIAL_LINE_BUFFER_SIZE +The initial buffer size to allocate to read a long line from a text file. +*/ + + + + + + +/*! \fn int longline_prepare(struct longlinestruct *line) +Prepare the buffer to read long text lines from a file. + +The memory allocated by this function must be freed with a call to +longline_free(). + +\param line The buffer to initialize. + +\retval 0 No error. +\retval -1 Not enough memory. +*/ + + + + + +/*! \fn char *longline_read(FILE *fp_in,struct longlinestruct *line) +Read one long line of text from a file. If the buffer is too short, it is +expended until the line can fit in it. + +The function always read as many bytes as can fit in the buffer and split the lines +to return one line at a time to the caller. The returned lines are always terminated +by a null ASCII character. The CR or LF are removed. + +Any empty line is skipped. + +\param fp_in The file to read. +\param line The buffer initialized by longline_preapre(). + +\return A pointer to the beginning of the string in the buffer or NULL if it is +the last string read. + +\note If not enough memory is available to read the line, the program is terminated +with an error message. +*/ + + + + + +/*! \fn void longline_free(struct longlinestruct *line) +Free the memory allocated by longline_prepare(). + +\param line The buffer to free. +*/ diff --git a/documentation/util.txt b/documentation/util.txt index 5a4ed04..1f1c242 100644 --- a/documentation/util.txt +++ b/documentation/util.txt @@ -6,13 +6,6 @@ -/*! \def INITIAL_LINE_BUFFER_SIZE -The initial buffer size to allocate to read a long line from a text file. -*/ - - - - /*! \var static char mtab1[12][4]; The list of the months. */ @@ -889,52 +882,3 @@ Delete a directory and its content. itself in place. If set to \c zero, the directory is removed too. */ - - - - -/*! \fn int longline_prepare(struct longlinestruct *line) -Prepare the buffer to read long text lines from a file. - -The memory allocated by this function must be freed with a call to -longline_free(). - -\param line The buffer to initialize. - -\retval 0 No error. -\retval -1 Not enough memory. -*/ - - - - - -/*! \fn char *longline_read(FILE *fp_in,struct longlinestruct *line) -Read one long line of text from a file. If the buffer is too short, it is -expended until the line can fit in it. - -The function always read as many bytes as can fit in the buffer and split the lines -to return one line at a time to the caller. The returned lines are always terminated -by a null ASCII character. The CR or LF are removed. - -Any empty line is skipped. - -\param fp_in The file to read. -\param line The buffer initialized by longline_preapre(). - -\return A pointer to the beginning of the string in the buffer or NULL if it is -the last string read. - -\note If not enough memory is available to read the line, the program is terminated -with an error message. -*/ - - - - - -/*! \fn void longline_free(struct longlinestruct *line) -Free the memory allocated by longline_prepare(). - -\param line The buffer to free. -*/ diff --git a/include/defs.h b/include/defs.h index ed95ca0..750c7b6 100755 --- a/include/defs.h +++ b/include/defs.h @@ -181,10 +181,10 @@ void usage(const char *prog); void useragent(void); // userinfo.c -struct userinfostruct *userinfo_create(const char *userid); +/*@shared@*/struct userinfostruct *userinfo_create(const char *userid); void userinfo_free(void); -struct userinfostruct *userinfo_find_from_file(const char *filename); -struct userinfostruct *userinfo_find_from_id(const char *id); +/*@shared@*/struct userinfostruct *userinfo_find_from_file(const char *filename); +/*@shared@*/struct userinfostruct *userinfo_find_from_id(const char *id); // usertab.c void init_usertab(const char *UserTabFile); diff --git a/longline.c b/longline.c new file mode 100644 index 0000000..4ca20de --- /dev/null +++ b/longline.c @@ -0,0 +1,113 @@ +/* + * SARG Squid Analysis Report Generator http://sarg.sourceforge.net + * 1998, 2010 + * + * SARG donations: + * please look at http://sarg.sourceforge.net/donations.php + * Support: + * http://sourceforge.net/projects/sarg/forums/forum/363374 + * --------------------------------------------------------------------- + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA. + * + */ + +#include "include/conf.h" +#include "include/defs.h" + +#define INITIAL_LINE_BUFFER_SIZE 32768 + +int longline_prepare(struct longlinestruct *line) +{ + line->size=INITIAL_LINE_BUFFER_SIZE; + line->buffer=malloc(line->size); + if (!line->buffer) + return(-1); + line->start=0; + line->end=0; + line->length=0; + return(0); +} + +char *longline_read(FILE *fp_in,struct longlinestruct *line) +{ + int i; + char *newbuf; + size_t nread; + + if (!line->buffer) return(NULL); + + while (1) { + for (i=line->end ; ilength && (line->buffer[i]=='\n' || line->buffer[i]=='\r') ; i++); + if (ilength) { + line->end=i; + break; + } + nread=(feof(fp_in)) ? 0 : fread(line->buffer,1,line->size,fp_in); + if (nread==0) return(NULL); + line->length=nread; + line->end=0; + } + + line->start=line->end; + while (1) { + for (i=line->end ; ilength && line->buffer[i]!='\n' && line->buffer[i]!='\r' ; i++); + line->end=i; + if (line->endlength) break; + + if (line->start>0) { + for (i=line->start ; ilength ; i++) line->buffer[i-line->start]=line->buffer[i]; + line->length-=line->start; + line->end-=line->start; + line->start=0; + } + if (line->length>=line->size) { + line->size+=8192; + newbuf=realloc(line->buffer,line->size); + if (!newbuf) { + debuga(_("Not enough memory to read one more line from the input log file\n")); + exit(EXIT_FAILURE); + } + line->buffer=newbuf; + } + nread=(feof(fp_in)) ? 0 : fread(line->buffer+line->length,1,line->size-line->length,fp_in); + if (nread==0) { + if (line->end<=line->start) return(NULL); + if (line->end>=line->size) { + line->end=line->size; + line->size++; + newbuf=realloc(line->buffer,line->size); + if (!newbuf) { + debuga(_("Not enough memory to read one more line from the input log file\n")); + exit(EXIT_FAILURE); + } + line->buffer=newbuf; + } + line->buffer[line->end]='\0'; + return(line->buffer+line->start); + } + line->length+=nread; + } + line->buffer[line->end++]='\0'; + return(line->buffer+line->start); +} + +void longline_free(struct longlinestruct *line) +{ + if (line->buffer) { + free(line->buffer); + line->buffer=NULL; + } +} diff --git a/splintrc b/splintrc index 673aa93..151099f 100644 --- a/splintrc +++ b/splintrc @@ -55,3 +55,4 @@ -DRLIM_STRING="%d" -DICONV_CONST -DHAVE_LC_MESSAGES=1 +-D__GNUC__ diff --git a/util.c b/util.c index 96dec5e..35b8f55 100644 --- a/util.c +++ b/util.c @@ -36,8 +36,6 @@ #define USE_GETWORD_BACKTRACE 0 #endif -#define INITIAL_LINE_BUFFER_SIZE 32768 - static char mtab1[12][4]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"}; //! The list of the HTTP codes to exclude from the report. @@ -1664,85 +1662,3 @@ void unlinkdir(const char *dir,int contentonly) } } -int longline_prepare(struct longlinestruct *line) -{ - line->size=INITIAL_LINE_BUFFER_SIZE; - line->buffer=malloc(line->size); - if (!line->buffer) - return(-1); - line->start=0; - line->end=0; - line->length=0; - return(0); -} - -char *longline_read(FILE *fp_in,struct longlinestruct *line) -{ - int i; - char *newbuf; - size_t nread; - - if (!line->buffer) return(NULL); - - while (1) { - for (i=line->end ; ilength && (line->buffer[i]=='\n' || line->buffer[i]=='\r') ; i++); - if (ilength) { - line->end=i; - break; - } - nread=(feof(fp_in)) ? 0 : fread(line->buffer,1,line->size,fp_in); - if (nread==0) return(NULL); - line->length=nread; - line->end=0; - } - - line->start=line->end; - while (1) { - for (i=line->end ; ilength && line->buffer[i]!='\n' && line->buffer[i]!='\r' ; i++); - line->end=i; - if (line->endlength) break; - - if (line->start>0) { - for (i=line->start ; ilength ; i++) line->buffer[i-line->start]=line->buffer[i]; - line->length-=line->start; - line->end-=line->start; - line->start=0; - } - if (line->length>=line->size) { - line->size+=8192; - newbuf=realloc(line->buffer,line->size); - if (!newbuf) { - debuga(_("Not enough memory to read one more line from the input log file\n")); - exit(EXIT_FAILURE); - } - line->buffer=newbuf; - } - nread=(feof(fp_in)) ? 0 : fread(line->buffer+line->length,1,line->size-line->length,fp_in); - if (nread==0) { - if (line->end<=line->start) return(NULL); - if (line->end>=line->size) { - line->end=line->size; - line->size++; - newbuf=realloc(line->buffer,line->size); - if (!newbuf) { - debuga(_("Not enough memory to read one more line from the input log file\n")); - exit(EXIT_FAILURE); - } - line->buffer=newbuf; - } - line->buffer[line->end]='\0'; - return(line->buffer+line->start); - } - line->length+=nread; - } - line->buffer[line->end++]='\0'; - return(line->buffer+line->start); -} - -void longline_free(struct longlinestruct *line) -{ - if (line->buffer) { - free(line->buffer); - line->buffer=NULL; - } -}