00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00042
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
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
00078
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
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
00140
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
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
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 }