Main Page   Modules   Data Structures   File List   Data Fields   Globals  

C_libs/gen/files.c

Go to the documentation of this file.
00001 /* === Doxygen File Comment ============================== */
00010 /* ======================================================= */
00011 
00012 #include <stdio.h>
00013 #include <string.h>
00014 #include <malloc.h>
00015 #include <unistd.h>
00016 #include <sys/stat.h>
00017 
00018 /* --- Doxygen Function Comment -------------------------- */
00022                 /* @{ ***** Starting group2 ***** */
00028 /* ------------------------------------------------------- */
00029 
00030 int 
00031 exist(char *file)
00032 {
00033         struct stat statbuf;
00034         char filename[512], *pt_sep;
00035         int exist_extension(char *);
00036 
00037         strcpy(filename,file);
00038         pt_sep = strchr(filename,'[');
00039         if (pt_sep != NULL) {   /* Fits file extension */
00040                 return (exist_extension(file));
00041         }
00042         else {
00043                 return (stat(filename, &statbuf) < 0 ? 0 : 1);
00044         }
00045 }
00046 
00047 /* --- Doxygen Function Comment -------------------------- */
00053 /* ------------------------------------------------------- */
00054 
00055 char *get_path(char *file) {
00056 
00057         char *pt_slash, *path;
00058         char buffer[512];
00059 
00060         strcpy(buffer,file);
00061         pt_slash = strrchr(buffer,'/');
00062         if (pt_slash == NULL)   /* no path */
00063                 strcpy(buffer,"./");
00064         else 
00065                 *pt_slash = 0;
00066         path = (char *)malloc((strlen(buffer)+1)*sizeof(char));
00067         strcpy(path,buffer);
00068         return(path);
00069 }
00070 
00071 /* --- Doxygen Function Comment -------------------------- */
00077 /* ------------------------------------------------------- */
00078 
00079 int remove_path(char *file) {
00080 
00081         char *pt_slash;
00082 
00083         pt_slash = strrchr(file,'/');
00084         if (pt_slash == NULL)   /* no path */
00085                 return 0;
00086         strcpy(file,pt_slash+1);
00087         return 0;
00088 }
00089 
00090 /* --- Doxygen Function Comment -------------------------- */
00096 /* ------------------------------------------------------- */
00097 
00098 #define BSIZ 4096
00099 
00100 int f_lines(char *file) 
00101 {
00102     int l_cnt=0,cnt;
00103 
00104     FILE *fd;
00105     unsigned long tcnt = 0;
00106     unsigned long was_sp = 1;
00107     unsigned char *pp, *pe;
00108 
00109     unsigned long ws[256];
00110     char buff[BSIZ];
00111 
00112  /* Fill tables */
00113     for (cnt = 0; cnt < 256; cnt++) ws[cnt] = 0;
00114 
00115     /* also: ws['\r']=ws['\v']=ws['\f']= */
00116     ws[' ']=ws['\t']=ws['\n']=1;
00117     ws['\n']=65536 + 1;
00118 
00119     /* Main loop */
00120     fd = fopen(file,"r");
00121     if (fd == NULL) return(-1);
00122     while((cnt=fread(buff,1,BSIZ,fd))) {
00123         if (cnt < 0) break;
00124 
00125         tcnt = 0;
00126 
00127         pp = buff;
00128         pe = buff + cnt;
00129 
00130         while(pp < pe) {
00131             tcnt += ws[*pp] ^ was_sp;
00132             was_sp = ws[*pp] & 0xFFFF;
00133             pp ++;
00134         }
00135         l_cnt += tcnt >> 16;
00136     }
00137     fclose(fd);
00138     return l_cnt;
00139 }
00140 
00141 /* --- Doxygen Function Comment -------------------------- */
00149 /* ------------------------------------------------------- */
00150 int read_DOS(int imno, char *data, int size)
00151 {
00152         int fd_no = imno;
00153         long lsize = size;
00154         char ctmp;
00155         long i, nb_w;
00156 
00157         read(fd_no,data,lsize);
00158         if (size < 2)
00159                 return(-1);
00160         for (i=0;i<size;i+=2) {
00161                 ctmp = data[i];
00162                 data[i] = data[i+1];
00163                 data[i+1] = ctmp;
00164         }
00165         nb_w = size/2;
00166         for (i=0;i<nb_w/2;i++) {
00167                 ctmp = data[i*2];
00168                 data[i*2] = data[(nb_w-1-i)*2];
00169                 data[(nb_w-1-i)*2] = ctmp;
00170                 ctmp = data[i*2+1];
00171                 data[i*2+1] = data[(nb_w-1-i)*2+1];
00172                 data[(nb_w-1-i)*2+1] = ctmp;
00173         }
00174         return(0);
00175 }
00176 
00177 /* --- Doxygen Function Comment -------------------------- */
00185 /* ------------------------------------------------------- */
00186 int write_DOS(int imno, char *truedata, int size)
00187 {
00188         int fd_no = imno;
00189         long lsize = size;
00190         char ctmp, data[8];
00191         long i, nb_w;
00192 
00193         memcpy(data,(char *)truedata,size);
00194 
00195         if (size < 2)
00196                 return(-1);
00197         for (i=0;i<size;i+=2) {
00198                 ctmp = data[i];
00199                 data[i] = data[i+1];
00200                 data[i+1] = ctmp;
00201         }
00202         nb_w = size/2;
00203         for (i=0;i<nb_w/2;i++) {
00204                 ctmp = data[i*2];
00205                 data[i*2] = data[(nb_w-1-i)*2];
00206                 data[(nb_w-1-i)*2] = ctmp;
00207                 ctmp = data[i*2+1];
00208                 data[i*2+1] = data[(nb_w-1-i)*2+1];
00209                 data[(nb_w-1-i)*2+1] = ctmp;
00210         }
00211         write(fd_no,data,lsize);
00212         return(0);
00213 }
00214 

Generated on Tue Jul 1 17:35:44 2003 for IFU_C_iolibs by doxygen1.3