home *** CD-ROM | disk | FTP | other *** search
/ Enter 2007 April / ENTER_CD_04_07.iso / Internet / WinHTTrack 3.23 / httrack-3.23.exe / {app} / src_win / WinHTTrack / HTTrackInterface.c < prev    next >
Encoding:
C/C++ Source or Header  |  2002-11-26  |  5.5 KB  |  262 lines

  1.  
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <time.h>
  6.  
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9.  
  10. #include "htsglobal.h"
  11. #include "htsbase.h"
  12.  
  13. #include "HTTrackInterface.h"
  14.  
  15. // Lecture d'une ligne (peut Ωtre unicode α priori)
  16. int linput(FILE* fp,char* s,int max) {
  17.   int c;
  18.   int j=0;
  19.   do {
  20.     c=fgetc(fp);
  21.     if (c!=EOF) {
  22.       switch(c) {
  23.         case 13: break;  // sauter CR
  24.         case 10: c=-1; break;
  25.         case 9: case 12: break;  // sauter ces caractΦres
  26.         default: s[j++]=(char) c; break;
  27.       }
  28.     }
  29.   }  while((c!=-1) && (c!=EOF) && (j<(max-1)));
  30.   s[j]='\0';
  31.   return j;
  32. }
  33. int linput_trim(FILE* fp,char* s,int max) {
  34.   int rlen=0;
  35.   char* ls=(char*) malloct(max+2);
  36.   s[0]='\0';
  37.   if (ls) {
  38.     char* a;
  39.     // lire ligne
  40.     rlen=linput(fp,ls,max);
  41.     if (rlen) {
  42.       // sauter espaces et tabs en fin
  43.       while( (rlen>0) && ((ls[max(rlen-1,0)]==' ') || (ls[max(rlen-1,0)]=='\t')) )
  44.         ls[--rlen]='\0';
  45.       // sauter espaces en dΘbut
  46.       a=ls;
  47.       while((rlen>0) && ((*a==' ') || (*a=='\t'))) {
  48.         a++;
  49.         rlen--;
  50.       }
  51.       if (rlen>0) {
  52.         memcpy(s,a,rlen);      // can copy \0 chars
  53.         s[rlen]='\0';
  54.       }
  55.     }
  56.     //
  57.     freet(ls);
  58.   }
  59.   return rlen;
  60. }
  61. int linput_cpp(FILE* fp,char* s,int max) {
  62.   int rlen=0;
  63.   s[0]='\0';
  64.   do {
  65.     int ret;
  66.     if (rlen>0)
  67.     if (s[rlen-1]=='\\')
  68.       s[--rlen]='\0';      // couper \ final
  69.     // lire ligne
  70.     ret=linput_trim(fp,s+rlen,max-rlen);
  71.     if (ret>0)
  72.       rlen+=ret;
  73.   } while((s[max(rlen-1,0)]=='\\') && (rlen<max));
  74.   return rlen;
  75. }
  76.  
  77. // idem avec les car spΘciaux
  78. void rawlinput(FILE* fp,char* s,int max) {
  79.   int c;
  80.   int j=0;
  81.   do {
  82.     c=fgetc(fp);
  83.     if (c!=EOF) {
  84.       switch(c) {
  85.         case 13: break;  // sauter CR
  86.         case 10: c=-1; break;
  87.         default: s[j++]=(char) c; break;
  88.       }
  89.     }
  90.   }  while((c!=-1) && (c!=EOF) && (j<(max-1)));
  91.   s[j++]='\0';
  92. }
  93.  
  94. // Like linput, but in memory (optimized)
  95. int binput(char* buff,char* s,int max) {
  96.   char* end;
  97.   int count;
  98.  
  99.   // clear buffer
  100.   s[0]='\0';
  101.   // end of buffer?
  102.   if ( *buff == '\0')
  103.     return 1;
  104.   // find ending \n
  105.   end=strchr(buff,'\n');
  106.   // ..or end of buffer
  107.   if (!end)
  108.     end=buff+strlen(buff);
  109.   // then count number of bytes, maximum=max
  110.   count=min(max,end-buff);
  111.   // and strip annoying ending cr
  112.   while( (count>0) && (buff[count] == '\r'))
  113.     count--;
  114.   // copy
  115.   if (count > 0) {
  116.     strncatbuff(s, buff, count);
  117.   }
  118.   // and terminate with a null char
  119.   s[count]='\0';
  120.   // then return the supplemental jump offset
  121.   return (end-buff)+1;
  122.  
  123. int fexist(char* s) {
  124.   struct stat st;
  125.   memset(&st, 0, sizeof(st));
  126.   if (stat(s, &st) == 0) {
  127.     if (S_ISREG(st.st_mode)) {
  128.       return 1;
  129.     }
  130.   }
  131.   return 0;
  132.  
  133. INTsys fsize(char* s) {
  134.   FILE* fp;
  135.   fp=fopen(s,"rb");
  136.   if (fp!=NULL) {
  137.     INTsys i;
  138.     fseek(fp,0,SEEK_END);
  139.     i=ftell(fp);
  140.     fclose(fp);
  141.     return i;
  142.   } else return -1;
  143. }
  144.  
  145. TStamp time_local(void) {
  146.   return ((TStamp) time(NULL));
  147. }
  148.  
  149.  
  150.  
  151.  
  152.  
  153. #define NOSTATIC_RESERVE(name, type, nelt) NOSTATIC_XRESERVE(name, type, nelt)
  154. #define NOSTATIC_XRESERVE(name, type, nelt) do { \
  155.   __declspec( thread ) static type thValue[nelt]; \
  156.   __declspec( thread ) int  static initValue = 0; \
  157.   name = thValue; \
  158.   if (!initValue) { \
  159.     initValue = 1; \
  160.     memset(&thValue, 0, sizeof(thValue)); \
  161.   } \
  162. } while(0)
  163.  
  164.  
  165.  
  166. typedef struct {
  167.   char buff[16][HTS_URLMAXSIZE*2*2];
  168.   int rol;
  169. } concat_strc;
  170. char* concat(const char* a,const char* b) {
  171.   concat_strc* strc;
  172.   NOSTATIC_RESERVE(strc, concat_strc, 1);
  173.   strc->rol=((strc->rol+1)%16);    // roving pointer
  174.   strcpybuff(strc->buff[strc->rol],a);
  175.   if (b) strcatbuff(strc->buff[strc->rol],b);
  176.   return strc->buff[strc->rol];
  177. }
  178. // conversion fichier / -> antislash
  179. #if HTS_DOSNAME
  180. char* __fconv(char* a) {
  181.   int i;
  182.   for(i=0;i<(int) strlen(a);i++)
  183.     if (a[i]=='/')  // convertir
  184.       a[i]='\\';
  185.   return a;
  186. }
  187. char* fconcat(char* a,char* b) {
  188.   return __fconv(concat(a,b));
  189. }
  190. char* fconv(char* a) {
  191.   return __fconv(concat(a,""));
  192. }
  193. #endif
  194.  
  195. /* / et \\ en / */
  196. char* __fslash(char* a) {
  197.   int i;
  198.   for(i=0;i<(int) strlen(a);i++)
  199.     if (a[i]=='\\')  // convertir
  200.       a[i]='/';
  201.   return a;
  202. }
  203. char* fslash(char* a) {
  204.   return __fslash(concat(a,""));
  205. }
  206.  
  207. // conversion minuscules, avec buffer
  208. char* convtolower(char* a) {
  209.   concat_strc* strc;
  210.   NOSTATIC_RESERVE(strc, concat_strc, 1);
  211.   strc->rol=((strc->rol+1)%16);    // roving pointer
  212.   strcpybuff(strc->buff[strc->rol],a);
  213.   hts_lowcase(strc->buff[strc->rol]);  // lower case
  214.   return strc->buff[strc->rol];
  215. }
  216.  
  217. // conversion en minuscules
  218. void hts_lowcase(char* s) {
  219.   int i;
  220.   for(i=0;i<(int) strlen(s);i++)
  221.     if ((s[i]>='A') && (s[i]<='Z'))
  222.       s[i]+=('a'-'A');
  223. }
  224.  
  225. char* next_token(char* p,int flag) {
  226.   int detect=0;
  227.   int quote=0;
  228.   p--;
  229.   do {
  230.     p++;
  231.     if (flag && (*p=='\\')) {   // sauter \x ou \"
  232.       if (quote) {
  233.         char c='\0';
  234.         if (*(p+1)=='\\')
  235.           c='\\';
  236.         else if (*(p+1)=='"')
  237.           c='"';
  238.         if (c) {
  239.           char tempo[8192];
  240.           tempo[0]=c; tempo[1]='\0';
  241.           strcatbuff(tempo,p+2);
  242.           strcpybuff(p,tempo);
  243.         }
  244.       }
  245.     }
  246.     else if (*p==34) {  // guillemets (de fin)
  247.       quote=!quote;
  248.     }
  249.     else if (*p==32) {
  250.       if (!quote)
  251.         detect=1;
  252.     }
  253.     else if (*p=='\0') {
  254.       p=NULL;
  255.       detect=1;
  256.     }
  257.   } while(!detect);
  258.   return p;
  259. }
  260.