home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD2.mdf / c / library / dos / grafik / gl_comp / glcomp.c next >
Encoding:
C/C++ Source or Header  |  1994-07-24  |  3.5 KB  |  132 lines

  1. /*
  2.    glcomp.c: a ".gl" file composer/decomposer.
  3.  
  4.    To compose a new ".gl" file, enter each file's name in a listfile.
  5.    Supply the listfile's name as the second argument.
  6.  
  7.    Originally programmed for TurboC.
  8.    Should be portable enough though.
  9.  
  10.    Donated to the public domain by S.R.van den Berg on 1990-11-08
  11.    (That means I will not support the code any further, and you can do
  12.    with the code whatever you want (except copyright it :-)).
  13.    Reachable on INTERNET as:
  14.                   berg%cip-s01.informatik.rwth-aachen.de@unido.bitnet
  15.    Reachable on BITNET as:
  16.                   berg%cip-s01.informatik.rwth-aachen.de@unido
  17. */
  18.  
  19. #pragma  option   -a- -r -O -Z -f-
  20. #include <io.h>
  21. #include <fcntl.h>
  22. #include <stdlib.h>
  23. #include <sys/stat.h>
  24. #define  S_IFILE  (S_IFREG|S_IREAD|S_IWRITE)
  25.  
  26. #define  BUFSIZ   8000U
  27. #define  MAXFILES 1024
  28.  
  29. #define  RECLEN   17
  30.  
  31. unsigned _stklen=1024,_heaplen=4U;
  32. typedef unsigned char uchar;
  33. struct {long offset;char name[13];} file[MAXFILES];
  34. uchar buffer[BUFSIZ];
  35. int in,out;
  36.  
  37. long fgetl(){unsigned i;
  38.  i=fgetw();return i|(long)(unsigned)fgetw()<<16;}
  39.  
  40. main(argc,argv)char*argv[];{int i;char *p;
  41.  if(argc<2||argc>3){
  42.    putse("Usage: glcomp xxx.gl [listfile]\n");return 1;}
  43.  if(0>(in=open(argv[argc-1],O_RDONLY|O_BINARY))){
  44. couldntfind:
  45.    putse("Couldn't find ");putse(argv[argc-1]);return 2;}
  46.  if(argc==2){
  47.    if(fgetw()/RECLEN>MAXFILES){
  48. toomanyfiles:
  49.       putse("Too many files\n");return 3;}
  50.    i=0;
  51.    do{
  52.       file[i].offset=fgetl();read(in,file[i].name,RECLEN-4);}
  53.    while(*file[i++].name);
  54.    i=0;
  55.    do{
  56.       if(0>
  57.          (out=open(p=file[i].name,O_WRONLY|O_BINARY|O_CREAT|O_TRUNC,S_IFILE))){
  58. couldntopen:
  59.          putse("Couldn't open ");putse(p);return 4;}
  60.       puts(p);puts("\n");lseek(in,SEEK_SET,file[i].offset);copyfw(fgetl());
  61.       close(out);}
  62.    while(*file[++i].name);}
  63.  else{long len;
  64.    for(i=0;;){
  65.       if(i>MAXFILES)
  66.          goto toomanyfiles;
  67.       p=file[i++].name;
  68.       do
  69.          if(eof(in))
  70.             goto nomorefiles;
  71.       while((*p=fgetc())<=' ');
  72.       while((*++p=fgetc())>' ');
  73.       *p='\0';}
  74. nomorefiles:
  75.    close(in);*p='\0';
  76.    if(0>(out=open(p=argv[1],O_WRONLY|O_BINARY|O_CREAT|O_TRUNC,S_IFILE)))
  77.       goto couldntopen;
  78.    fputw(i=i*RECLEN);write(out,file,i);
  79.    i=0;
  80.    puts("Reading:\n");
  81.    do{
  82.       if(0>(in=open(p=file[i].name,O_RDONLY|O_BINARY)))
  83.          goto couldntfind;
  84.       puts(p);puts("\n");file[i].offset=lseek(out,0L,SEEK_CUR);
  85.       fputl(len=lseek(in,0L,SEEK_END));lseek(in,0L,SEEK_SET);copyfw(len);
  86.       close(in);}
  87.    while(*file[++i].name);
  88.    lseek(out,2L,SEEK_SET);i=0;
  89.    do{
  90.       fputl(file[i].offset);write(out,file[i].name,RECLEN-4);}
  91.    while(*file[i++].name);}
  92.  return 0;}
  93.  
  94. fgetc(){uchar i;
  95.  read(in,&i,1);return i;}
  96.  
  97. fgetw(){int i;
  98.  i=fgetc();return i|fgetc()<<8;}
  99.  
  100. fputc(i){uchar j;
  101.  j=i;write(out,&j,1);}
  102.  
  103. fputw(i){
  104.  fputc(i);fputc(i>>8);}
  105.  
  106. fputl(i)long i;{
  107.  fputw(i);fputw(i>>16);}
  108.  
  109. copyfw(i)long i;{long j;
  110.  while(i>0L){j=i;
  111.    i-=
  112.    (unsigned)write(out,buffer,read(in,buffer,(unsigned)min(i,(long)BUFSIZ)));
  113.    if(i>=j){
  114.       putse("Choking...  Need space...\n");close(in);close(out);abort();}}}
  115.  
  116. #define STDOUT 1
  117. #define STDERR 2
  118. #include <io.h>
  119.  
  120. puts(a)char *a;{putsfd(a,STDOUT);}
  121.  
  122. putse(a)char *a;{putsfd(a,STDERR);}
  123.  
  124. putsfd(a,fd)register char *a;{register char *b;
  125.  b=a;
  126.  while(*a) a++;
  127.  write(fd,b,a-b);}
  128.  
  129. _setupio(){}   /* Only use this if NO streams are used */
  130.  
  131.  
  132.