]>
git.ipfire.org Git - thirdparty/sarg.git/blob - fileobject.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 \brief Encapsulate a file object
29 The file can be a standard file of the C library or a gzip file or
33 #include "include/conf.h"
34 #include "include/stringbuffer.h"
35 #include "include/fileobject.h"
37 //! Message describing the last open error.
38 static char LastOpenErrorString
[2048]="";
39 //! Message describing the last close error.
40 static char LastCloseErrorString
[2048]="";
43 * Read a file using the standard C api.
45 * \param Data The file object.
46 * \param Buffer The boffer to store the data read.
47 * \param Size How many bytes to read.
49 * \return The number of bytes read.
51 static int Standard_Read(void *Data
,void *Buffer
,int Size
)
53 return(fread(Buffer
,1,Size
,(FILE *)Data
));
57 * Check if end of file is reached.
59 * \param Data The file object.
61 * \return \c True if end of file is reached.
63 static int Standard_Eof(void *Data
)
65 return(feof((FILE *)Data
));
69 * Return to the beginnig of the file.
71 * \param Data The file object.
73 static void Standard_Rewind(void *Data
)
79 * Close a file using the standard C api.
81 * \param Data File to close.
83 * \return EOF on error.
85 static int Standard_Close(void *Data
)
89 if (fclose((FILE *)Data
)==EOF
)
91 FileObject_SetLastCloseError(strerror(errno
));
98 * Open a file for reading using the standard C api.
100 * \param FileName The file to open.
102 * \return The object to pass to other function in this module.
104 FileObject
*FileObject_Open(const char *FileName
)
108 LastOpenErrorString
[0]='\0';
109 File
=malloc(sizeof(*File
));
112 FileObject_SetLastOpenError(_("Not enough memory"));
115 File
->Data
=MY_FOPEN(FileName
,"r");
119 FileObject_SetLastOpenError(strerror(errno
));
122 File
->Read
=Standard_Read
;
123 File
->Eof
=Standard_Eof
;
124 File
->Rewind
=Standard_Rewind
;
125 File
->Close
=Standard_Close
;
130 * Open a file for reading using the standard C api.
132 * \param FileName The file to open.
134 * \return The object to pass to other function in this module.
136 FileObject
*FileObject_FdOpen(int fd
)
140 LastOpenErrorString
[0]='\0';
141 File
=malloc(sizeof(*File
));
144 FileObject_SetLastOpenError(_("Not enough memory"));
147 File
->Data
=fdopen(fd
,"r");
151 FileObject_SetLastOpenError(strerror(errno
));
154 File
->Read
=Standard_Read
;
155 File
->Eof
=Standard_Eof
;
156 File
->Rewind
=Standard_Rewind
;
157 File
->Close
=Standard_Close
;
162 * Read the content of the file using the function identified
163 * by the file object.
165 * \param File The file object to read.
166 * \param Buffer The buffer to write the data into.
167 * \param Size The maximum number of bytes to read.
169 * \return The number of bytes read or -1.
171 int FileObject_Read(FileObject
*File
,void *Buffer
,int Size
)
173 return(File
->Read(File
->Data
,Buffer
,Size
));
177 * Check if the end of file is reached.
179 * \param File The file object.
181 * \return \c True if end of file is reached.
183 int FileObject_Eof(FileObject
*File
)
185 return(File
->Eof(File
->Data
));
189 * Return to the beginning of the file.
191 * \param File The file object.
193 void FileObject_Rewind(FileObject
*File
)
195 File
->Rewind(File
->Data
);
199 * Close the file opened. The memory is freed. The object
200 * cannot be reused after this function returns.
202 * \param File The file object to close.
204 * \return Zero on success or -1 on failure.
206 int FileObject_Close(FileObject
*File
)
208 LastCloseErrorString
[0]='\0';
209 int RetCode
=File
->Close(File
->Data
);
215 * Set the message returned by the last open error.
217 * \param Message The message explaining what error occurred
218 * when the file was opened.
220 void FileObject_SetLastOpenError(const char *Message
)
224 strncpy(LastOpenErrorString
,Message
,sizeof(LastOpenErrorString
)-1);
225 LastOpenErrorString
[sizeof(LastOpenErrorString
)-1]='\0';
228 LastOpenErrorString
[0]='\0';
232 * Get a message explaining the last open error.
234 * \return The string to display to the user.
236 const char *FileObject_GetLastOpenError(void)
238 return(LastOpenErrorString
);
242 * Set the message returned by the last close error.
244 * \param Message The message explaining what error occurred
245 * when the file was closed.
247 void FileObject_SetLastCloseError(const char *Message
)
251 strncpy(LastCloseErrorString
,Message
,sizeof(LastCloseErrorString
)-1);
252 LastCloseErrorString
[sizeof(LastCloseErrorString
)-1]='\0';
255 LastCloseErrorString
[0]='\0';
259 * Get a message explaining the last close error.
261 * \return The string to display to the user.
263 const char *FileObject_GetLastCloseError(void)
265 return(LastCloseErrorString
);