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
00030 #include <stdio.h>
00031 #include <stdlib.h>
00032 #include <stdbool.h>
00033 #include <string.h>
00034 #include <unistd.h>
00035 #include <libgen.h>
00036 #include <sys/stat.h>
00037 #include <sys/file.h>
00038 #include "qDecoder.h"
00039 #include "qInternal.h"
00040
00065 bool qFileLock(int fd) {
00066 #ifdef _WIN32
00067 return false;
00068 #else
00069 int ret = flock(fd, LOCK_EX);
00070 if(ret == 0) return true;
00071 return false;
00072 #endif
00073 }
00074
00082 bool qFileUnlock(int fd) {
00083 #ifdef _WIN32
00084 return false;
00085 #else
00086 int ret = flock(fd, LOCK_EX);
00087 if(ret == 0) return true;
00088 return false;
00089 #endif
00090 }
00091
00099 bool qFileExist(const char *filepath) {
00100 struct stat finfo;
00101 if (stat(filepath, &finfo) < 0) return false;
00102 return true;
00103 }
00104
00112 char *qFileGetName(const char *filepath) {
00113 char *path = strdup(filepath);
00114 char *bname = basename(path);
00115 char *filename = strdup(bname);
00116 free(path);
00117 return filename;
00118 }
00119
00127 char *qFileGetDir(const char *filepath) {
00128 char *path = strdup(filepath);
00129 char *dname = dirname(path);
00130 char *dir = strdup(dname);
00131 free(path);
00132 return dir;
00133 }
00134
00142 char *qFileGetExt(const char *filepath) {
00143 #define MAX_EXTENSION_LENGTH (5)
00144 char *filename = qFileGetName(filepath);
00145 char *p = strrchr(filename, '.');
00146 char *ext = NULL;
00147 if(p != NULL && strlen(p+1) <= MAX_EXTENSION_LENGTH && qStrIsAlnum(p+1) == true) {
00148 ext = strdup(p+1);
00149 qStrLower(ext);
00150 } else {
00151 ext = strdup("");
00152 }
00153
00154 free(filename);
00155 return ext;
00156 }
00157
00165 off_t qFileGetSize(const char *filepath) {
00166 struct stat finfo;
00167 if (stat(filepath, &finfo) < 0) return -1;
00168 return finfo.st_size;
00169 }
00170
00180 off_t qFileSend(int outfd, int infd, off_t nbytes) {
00181 #define MAX_FILESEND_CHUNK_SIZE (32 * 1024)
00182 if(nbytes == 0) return 0;
00183
00184 char buf[MAX_FILESEND_CHUNK_SIZE];
00185
00186 off_t sent = 0;
00187 while(sent < nbytes) {
00188 size_t sendsize;
00189 if(nbytes - sent <= sizeof(buf)) sendsize = nbytes - sent;
00190 else sendsize = sizeof(buf);
00191
00192
00193 ssize_t retr = read(infd, buf, sendsize);
00194 DEBUG("read %zd", retr);
00195 if (retr <= 0) {
00196 if(sent == 0) return -1;
00197 break;
00198 }
00199
00200
00201 ssize_t retw = _q_write(outfd, buf, retr);
00202 DEBUG("write %zd", retw);
00203 if(retw <= 0) {
00204 if(sent == 0) return -1;
00205 break;
00206 }
00207
00208 sent += retw;
00209 if(retr != retw) {
00210 DEBUG("size mismatch %zd, %zd", retr, retw);
00211 break;
00212 }
00213 }
00214
00215 return sent;
00216 }
00217
00247 void *qFileLoad(const char *filepath, size_t *nbytes) {
00248 int fd;
00249 if((fd = open(filepath, O_RDONLY, 0)) < 0) return NULL;
00250
00251 struct stat fs;
00252 if (fstat(fd, &fs) < 0) {
00253 close(fd);
00254 return NULL;
00255 }
00256
00257 size_t size = fs.st_size;
00258 if(nbytes != NULL && *nbytes > 0 && *nbytes < fs.st_size) size = *nbytes;
00259
00260 void *buf = malloc(size + 1);
00261 if(buf == NULL) {
00262 close(fd);
00263 return NULL;
00264 }
00265
00266 ssize_t count = read(fd, buf, size);
00267 close(fd);
00268
00269 if (count != size) {
00270 free(buf);
00271 return NULL;
00272 }
00273
00274 ((char*)buf)[count] = '\0';
00275
00276 if(nbytes != NULL) *nbytes = count;
00277 return buf;
00278 }
00279
00292 void *qFileRead(FILE *fp, size_t *nbytes) {
00293 size_t memsize;
00294 size_t c_count;
00295 size_t size = 0;
00296 char *data = NULL;
00297
00298 if(nbytes != NULL && *nbytes > 0) size = *nbytes;
00299
00300 int c;
00301 for (memsize = 1024, c_count = 0; (c = fgetc(fp)) != EOF;) {
00302 if(size > 0 && c_count == size) break;
00303
00304 if (c_count == 0) {
00305 data = (char*)malloc(sizeof(char) * memsize);
00306 if (data == NULL) {
00307 DEBUG("Memory allocation failed.");
00308 return NULL;
00309 }
00310 } else if (c_count == memsize - 1) {
00311 memsize *= 2;
00312
00313
00314 char *datatmp = (char*)malloc(sizeof(char) * (memsize + 1));
00315 if (datatmp == NULL) {
00316 DEBUG("Memory allocation failed.");
00317 free(data);
00318 return NULL;
00319 }
00320 memcpy(datatmp, data, c_count);
00321 free(data);
00322 data = datatmp;
00323 }
00324 data[c_count++] = (char)c;
00325 }
00326
00327 if (c_count == 0 && c == EOF) return NULL;
00328 data[c_count] = '\0';
00329
00330 if(nbytes != NULL) *nbytes = c_count;
00331
00332 return (void*)data;
00333 }
00334
00342 char *qFileReadLine(FILE *fp) {
00343 int memsize;
00344 int c, c_count;
00345 char *string = NULL;
00346
00347 for (memsize = 1024, c_count = 0; (c = fgetc(fp)) != EOF;) {
00348 if (c_count == 0) {
00349 string = (char *)malloc(sizeof(char) * memsize);
00350 if (string == NULL) {
00351 DEBUG("Memory allocation failed.");
00352 return NULL;
00353 }
00354 } else if (c_count == memsize - 1) {
00355 char *stringtmp;
00356
00357 memsize *= 2;
00358
00359
00360 stringtmp = (char *)malloc(sizeof(char) * (memsize + 1));
00361 if (stringtmp == NULL) {
00362 DEBUG("Memory allocation failed.");
00363 free(string);
00364 return NULL;
00365 }
00366 memcpy(stringtmp, string, c_count);
00367 free(string);
00368 string = stringtmp;
00369 }
00370 string[c_count++] = (char)c;
00371 if ((char)c == '\n') break;
00372 }
00373
00374 if (c_count == 0 && c == EOF) return NULL;
00375 string[c_count] = '\0';
00376
00377 return string;
00378 }
00379
00400 ssize_t qFileSave(const char *filepath, const void *buf, size_t size, bool append) {
00401 int fd;
00402
00403 if(append == false) fd = open(filepath, O_CREAT|O_WRONLY|O_TRUNC, DEF_FILE_MODE);
00404 else fd = open(filepath, O_CREAT|O_WRONLY|O_APPEND, DEF_FILE_MODE);
00405 if(fd < 0) return -1;
00406
00407 ssize_t count = write(fd, buf, size);
00408 close(fd);
00409
00410 return count;
00411 }