Main Page | Class List | File List | Class Members | File Members | Related Pages

ut_loading.c

00001 /***************************************************************************
00002  *            ut_load.c
00003  *
00004  *  Thu Dec 23 15:53:42 2004
00005  *  Copyright  2004  Alliance MCA
00006  *  Written by : Antoine Calando (antoine@alliancemca.net)
00007  ****************************************************************************/
00008 
00009 /*
00010  *  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version.
00014  *
00015  *  This program is distributed in the hope that it will be useful,
00016  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  *  GNU Library General Public License for more details.
00019  *
00020  *  You should have received a copy of the GNU General Public License
00021  *  along with this program; if not, write to the Free Software
00022  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00023  */
00024  
00025 
00032 #include <sys/stat.h>
00033 #include <unistd.h>
00034 #include <fcntl.h>
00035 #include <stdio.h>
00036 #include <stdlib.h>
00037 
00038 #include "utrac.h"
00039 #include "ut_text.h"
00040 
00041 //#undef UT_DEBUG
00042 //#define UT_DEBUG 2
00043 #include "debug.h"
00044 
00045 
00046 /***************************************************************************/
00057 UtCode ut_load_file_pass (UtText *text, const char * filename) {
00058         DBG3 ("Loading file %s...", filename)
00059         
00060         int fd = open (filename, O_RDONLY);
00061         if (fd==-1) return UT_OPEN_FILE_ERROR;
00062 
00063         struct stat f_stat;
00064         if (fstat (fd, &f_stat)) return UT_FSTAT_FILE_ERROR;
00065         text->size = f_stat.st_size;
00066         
00067         if (!text->size) return UT_EMPTY_DATA_ERROR;
00068 
00069         free (text->data);
00070         //some space is needed to add an EOL (1 or 2 bytes) if it is missing and an EOF
00071         text->data = (char*) malloc (text->size + 3);
00072         if (!text->data) return UT_MALLOC_ERROR;
00073         
00074         ulong cumul=0;
00075         bool cont = true;
00076 
00077         //if progress feature is used, read() is called with UT_LOAD_STEP several times and
00078         //can overflow the buffer allocated if read() fills it more than the size of the file
00079         while (cumul<text->size && cont) {
00080                 long code=read (fd, text->data+cumul, (ut_session->progress_function)?UT_LOAD_STEP:(text->size-cumul));
00081                 if (code<=0) return UT_READ_FILE_ERROR;
00082                 cumul += code;
00083                 if (ut_session->progress_function) cont = ut_update_progress (text, cumul, false);
00084         }
00085 
00086         
00087         if (!cont) {
00088                 DBG2 ("File loading canceled by user!")
00089                 free (text->data);
00090                 text->data = NULL;
00091                 return UT_INTERRUPTED_BY_USER;
00092         }
00093 
00094         ASSERT (cumul==text->size)
00095         DBG2 ("File %s (%lu b / %lu b) loaded!", filename, cumul, text->size)
00096         
00097         *(text->data+text->size) = UT_EOF_CHAR;
00098         *(text->data+text->size+1) = UT_EOF_CHAR;
00099         *(text->data+text->size+2) = UT_EOF_CHAR;
00100 
00101         if (close(fd)) return UT_CLOSE_FILE_ERROR;
00102                 
00103         return UT_OK;
00104 }
00105 
00106 /***************************************************************************/
00124 UtCode ut_load_stdin_pass (UtText *text) {
00125 
00126         //some space is needed to add an EOL (1 or 2 bytes) if it is missing and an EOF
00127         free(text->data);
00128         text->data = (char*) malloc (UT_STDIN_BUFFER_SIZE);
00129         if (!text->data) return UT_MALLOC_ERROR;
00130                 
00131         struct stat f_stat;
00132         if (fstat (fileno(stdin), &f_stat)) return UT_FSTAT_FILE_ERROR;
00133         text->size = f_stat.st_size;
00134         
00135         ulong bytes_read = 0, bytes_to_read = 0;
00136         ulong buffer_size = UT_STDIN_BUFFER_SIZE;
00137         bool cont = true;
00138         
00139         //if progress feature is used, read() is called with UT_LOAD_STEP several times and
00140         //can overflow the buffer allocated if read() fills it more than the size of the file
00141         long code;
00142         do {
00143                 bytes_to_read = buffer_size-bytes_read;
00144                 if (ut_session->progress_function && UT_LOAD_STEP < bytes_to_read ) bytes_to_read = UT_LOAD_STEP;
00145                 code = read (STDIN_FILENO, text->data + bytes_read, bytes_to_read);
00146                 if (code<0) {
00147                         free (text->data);
00148                         text->data = NULL;
00149                         return UT_READ_FILE_ERROR;
00150                 }
00151                 bytes_read += code;
00152 
00153                 if (fstat (fileno(stdin), &f_stat)) return UT_FSTAT_FILE_ERROR;
00154                 text->size = f_stat.st_size;
00155                 if (ut_session->progress_function) cont = ut_update_progress (text, bytes_read, false);
00156         
00157                 //if buffer full, allocate new bigger buffer
00158                 if (bytes_read == buffer_size) {
00159                         buffer_size *= 2;
00160                         text->data =  realloc (text->data, buffer_size + 3);
00161                         if (!text->data) return UT_MALLOC_ERROR;
00162                 }
00163                 
00164                 //if (ut_session->progress_function) cont = ut_update_progress (text, bytes_read, false);
00165         } while (code && cont);
00166 
00167         if (!cont) {
00168                 DBG2 ("** stdin loading canceled by user! **")
00169                 free (text->data);
00170                 text->data = NULL;
00171                 return UT_INTERRUPTED_BY_USER;
00172         }
00173 
00174         text->size = bytes_read;
00175         
00176         DBG2 ("stdin stream (%lu b) loaded!", text->size)
00177         
00178         *(text->data+text->size) = UT_EOF_CHAR;
00179         *(text->data+text->size+1) = UT_EOF_CHAR;
00180         *(text->data+text->size+2) = UT_EOF_CHAR;
00181 
00182         return UT_OK;
00183 }

Generated on Fri Feb 25 18:30:15 2005 for Utrac by  doxygen 1.3.9