home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / cprog / file_cl.zip / FILTST.CPP < prev    next >
C/C++ Source or Header  |  1992-02-01  |  6KB  |  257 lines

  1. /*
  2. Example of File_cl class usage.
  3.  
  4. Include file is in directory :
  5. \borlandc\nat\classes\include
  6.  
  7. Please link with debugging library, for instance LDBORNAT.LIB
  8. */
  9.  
  10. #include "\borlandc\nat\classes\include\file_cl.hpp"
  11.  
  12. #ifdef __BCPLUSPLUS__
  13. // pour Borland C++
  14. #include <alloc.h>
  15. #endif
  16. #include <conio.h>
  17. #ifdef __ZTC__
  18. // Pour Zortech C++
  19. #include <dos.h>    // pour _farptr_norm()
  20. #endif
  21. #include <stdlib.h>
  22. #include <time.h>
  23. #include <stdarg.h>
  24. #include <string.h>
  25.  
  26. #undef NDEBUG    // in order to use assert()
  27. #include <assert.h>
  28.  
  29. const int MAX_FIL_TST=20;    // number of files
  30.  
  31.  
  32. FILE *ficdbg;    // debugging file
  33. int dbg_depth=0;    // debugging file indentation
  34.  
  35. // standard abort function
  36. void cdecl die(int err, const char *msg ...)
  37. {
  38.     va_list arg;
  39.  
  40.     if (err)
  41.         {
  42.         fprintf(stderr, "\n\nERROR :\n");
  43.         va_start(arg, msg);
  44.         vfprintf(stderr, msg, arg);
  45.         va_end(arg);
  46.         while(kbhit())    // flushes keyboard buffer
  47.             getch();
  48.         puts("\nHit a key to continue ...");
  49.         getch();    // thus the user cannot but read the error message
  50.         }
  51.     exit(err);
  52. }
  53.  
  54. void pascal stop(const char *where)
  55. {
  56.     if (kbhit())
  57.         {
  58.         while (kbhit())
  59.             getch();
  60.         die(3, "BREAK : %s", where);
  61.         }
  62. }
  63.  
  64. void recurs(char count)
  65. {
  66.     stop("recurse");
  67.     printf("RECURS %d begin\n", count);
  68.     if (count%2)
  69.         recurs(count-1);
  70.     printf("RECURS %d ...\n", count);
  71.     File_cl fil_recurse;
  72.     assert(fil_recurse.assign("REC", NULL)==OK);
  73.     assert(fil_recurse.fwrite("REC", 3, 1)==1);
  74.     if ((!(count%2)) && count)
  75.         recurs(count-1);
  76.     printf("RECURS %d end\n", count);
  77. }
  78.  
  79. void go2(void)
  80. {
  81.     File_cl tmpgo;
  82.  
  83.     puts("GO2");
  84.     assert(tmpgo.assign("GO2", NULL)==OK);    // mode NULL : fichier temporaire
  85.     assert(tmpgo.fwrite("Essai", 5, 1)==1);
  86.     puts("GO2 end");
  87.     recurs(9);
  88. /* "tpgo" go out of scope :
  89. associated (temporary) file will be closed and deleted by destructor
  90. */
  91. }
  92.  
  93. File_cl fic[MAX_FIL_TST], fic2[MAX_FIL_TST];
  94. void go(void)
  95. {
  96.     OFFSET_T off;
  97.     int rep, rep2, rep3, lu;
  98.     long tmpl;
  99.     File_cl fic_essai, *tmp_new;
  100.     size_t reste;
  101.     unsigned long crc32;
  102.  
  103. // Init
  104.     puts("FICSTST");
  105.     system("del *.tmp");    // dangerous !
  106.     srand((unsigned int) ((clock()*clock())));
  107.  
  108.     unsigned long tmpt[16];
  109.  
  110. // CRC test :
  111.  
  112.     for (char i=0; i<15; i++)
  113.         tmpt[i]=rand();
  114.     crc32=~crc_32((unsigned char *) tmpt, sizeof(tmpt)-sizeof(unsigned long));
  115.     tmpt[15]=crc32;
  116.     assert(~crc_32((unsigned char *) tmpt, sizeof(tmpt))==0);
  117.  
  118.     tmp_new=new File_cl;
  119.     assert(tmp_new!=NULL);    // allocation OK ?
  120.  
  121.     assert(fic_essai.assign("/+.</xyzcaca/<.+", "w")==ERR);    // nom illégal : doit foirer
  122.  
  123.     for (rep=0; rep<MAX_FIL_TST; rep++)
  124.         {
  125.         assert(fic[rep].assign(NULL, NULL)==OK);    // temporary file
  126.         printf("%s open\n", fic[rep].getName());
  127.         assert(fic2[rep].assign(NULL, NULL)==OK);    // temporary file
  128.         printf("%s open\n", fic2[rep].getName());
  129.         stop("opening");
  130.         }
  131.     rep=0;
  132.  
  133. #ifdef __ZTC__
  134.     rep3=rand()%20;
  135. #else
  136.     rep3=random(20);
  137. #endif
  138.     for (rep=0; rep<MAX_FIL_TST; rep++)
  139.         {
  140.         tmpl=labs(((long) rand())/20)+21;
  141.         tmpl+=fic[rep].length();
  142.         printf("%d garbage(%ld)\n", rep, tmpl);
  143.         assert(fic[rep].fseek(0, SEEK_END)==0);    // à la fin du fichier
  144.         assert(fic[rep].writeGarbage(tmpl)==OK);    // du garbage
  145. // lecture d'un int à l'offset 0. Doit retourner 1 (nombre d'objets lus)
  146.         assert(fic[rep].fread(&lu, sizeof(int), 1, 0)==1);
  147. // écriture d'un octet (le n° de fichier) à l'offset "rep3"
  148.         assert(fic[rep].fwrite(&rep, sizeof(int), 1, (long) rep3)==1);
  149.         stop("one");
  150.         }
  151.     for (rep=0; rep<MAX_FIL_TST; rep++)
  152.         assert(fic[rep].fseek(-((long) sizeof(rep)), SEEK_CUR)==0);
  153.     assert(fic[1].whatFile_cl(fic[1].getName()) == _farptr_norm(&fic[1]));
  154.     for (rep=0; rep<MAX_FIL_TST; rep++)
  155.         {
  156.         fic[0].forceClose(1);
  157.         printf("P2 : byte %d, offset %d  %s\n", rep, rep3, fic[rep].getName());
  158. // reads the byte (offset "rep3")
  159.         assert(fic[rep].fread(&lu, sizeof(int), 1, (long) rep3)==1);
  160. // must be the file number
  161.         assert(lu==rep);
  162. // 1/3 of file length
  163.         tmpl=fic[rep].length()/3;
  164. // maximum 9999
  165.         if (tmpl>9999)
  166.             tmpl=9999;
  167. // cut !
  168.         assert(fic[rep].cut(tmpl)==OK);
  169.         stop("two");
  170.         }
  171.     for (rep2=0; rep2<MAX_FIL_TST; rep2++)
  172.         {
  173.         fic[rep2].length(&reste, 3);
  174.         printf("P3 : %d offset 0 %s\n", rep2, fic[rep2].getName());
  175. #ifdef __ZTC__
  176.         rep=rand()%32000;
  177. #else
  178.         rep=random(32000);
  179. #endif
  180. // "rep" will be written at the file beginning
  181.         assert(fic[rep2].fwrite(&rep, sizeof(int), 1, 0L)==1);
  182. // read on file beginning
  183.         assert(fic[rep2].fread(&lu, sizeof(int), 1, 0L)==1);
  184. // value checking
  185.         assert(rep==lu);
  186.         }
  187. #ifdef __ZTC__
  188.     rep=rand()%MAX_FIL_TST;
  189. #else
  190.     rep=random(MAX_FIL_TST);
  191. #endif
  192.     for (rep2=0; rep2<MAX_FIL_TST; rep2++)
  193.         {
  194.         printf("P4 : %s\n", fic[rep2].getName());
  195. // "rep" writing
  196.         assert(fic[rep2].fwrite(&rep, sizeof(int), 1)==1);
  197.         assert(fic[rep2].fseek(-((long) sizeof(rep)), SEEK_CUR)==0);
  198.         assert(fic[rep2].fread(&lu, sizeof(int), 1)==1);    // lecture
  199.         assert(rep==lu);    // et vérification
  200.         if (fic[rep2].length()>1)    // si longueur>1
  201.             {
  202.             // CRC
  203.             assert(fic[rep2].getCRC32(&crc32)==OK);
  204.             printf("%s :\n CRC32 -> %lu (0x%lx)\n", fic[rep2].getName(), crc32, crc32);
  205.             }
  206.         }
  207.  
  208.     go2();
  209.  
  210.     File_cl fictmp1;
  211.     unlink("TMPCOP0");
  212.     unlink("TMPCOP00");
  213. // garbage
  214.     assert(fic[0].writeGarbage(900)==0);
  215. // copy in TMPCOP0
  216.     assert(fic[0].copy("TMPCOP0")==OK);
  217.     assert(fictmp1.assign("TMPCOP0", "r")==OK);
  218. // src/dst of last copy comparison
  219.     assert(fic[0].compare(&fictmp1, &off)==OK);
  220.     assert(fictmp1.rename("TMPCOP00")==0);    // attention : rename() renvoie 0 si OK !
  221. // comparison after rename()
  222.     assert(fic[0].compare(&fictmp1, &off)==OK);
  223. // partial comparison
  224.     assert(fic[0].compare(&fictmp1, &off, 260, 260, 513)==OK);
  225.     assert(fictmp1.unlink()==0);
  226.  
  227. #if NAT_FILE_DEBUG
  228.     fic[0].status(2);
  229.     fic[0].optimiseBuf(10000);
  230. #endif
  231.  
  232.     puts("END");
  233.     delete tmp_new;
  234.  
  235. #if NAT_FILE_DEBUG
  236.     for (rep=0; rep<MAX_FIL_TST; rep++)
  237.         {
  238.         fic[rep].status(2);
  239.         fic2[rep].status(2);
  240.         }
  241. #endif
  242.     fclose(ficdbg);
  243.     ficdbg=NULL;
  244. }
  245.  
  246. void cdecl main(void)
  247. {
  248.     printf("File_cl test : sizeof(File_cl) = %d\n", sizeof(File_cl));
  249.     getch();
  250.     ficdbg=fopen("DEBUG.DBG", "wt");
  251.     if (ficdbg==NULL)
  252.         die(4, "Cannot open the debugging file");
  253.     go();
  254.     puts("\n !!! return !!!");
  255.     die(0, NULL);
  256. }
  257.