home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / vol_100 / 199_01 / ged5.c < prev    next >
Text File  |  1987-12-17  |  7KB  |  311 lines

  1.  /*
  2. Header:          CUG199;
  3. Title:           Module 5 of ged editor;
  4. Last Updated:    12/02/87;
  5.  
  6. Description:    "PURPOSE: read and write files";
  7.  
  8. Keywords:        e, editor, qed, ged, DeSmet, MSDOS;
  9. Filename:        ged5.c;
  10. Warnings:       "O file must be present during link for ged";
  11.  
  12. Authors:         G. Nigel Gilbert, James W. Haefner, and Mel Tearle;
  13. Compilers:       DeSmet 3.0;
  14.  
  15. Refrences:
  16. Endref;
  17. */
  18.  
  19. /*
  20. e/qed/ged  screen editor
  21.  
  22. (C) G. Nigel Gilbert, MICROLOGY, 1981 - August-December 1981
  23.  
  24. Modified:  Aug-Dec   1984:  BDS-C 'e'(vers 4.6a) to 'qe' (J.W. Haefner)
  25.            March     1985:  BDS-C 'qe' to DeSmet-C 'qed' (J.W. Haefner)
  26.  
  27.            May       1986:  qed converted to ged         (Mel Tearle)
  28.            August    1987:  ged converted to MSC 4.0     (Mel Tearle)
  29.  
  30. File:      ged5.c
  31.  
  32. Functions: readfile, writefile, exists, checkexists, scans, retag,
  33.            format
  34. */
  35.  
  36.  
  37. #ifndef  TC
  38. #include "ged.h"
  39. #else
  40. #include "ged.t"
  41. #endif
  42.  
  43. #define  GS   0x1d
  44. #define  ESC  0x1b
  45.  
  46. /* read text into current file from 'filename' with ^KR
  47.  * if middle of line then insert text after cursor
  48.  * else if beginning of line insert above cursor
  49.  */
  50. int readfile(name)
  51. char *name;
  52. {
  53. int  c, i, j, k, line, first, res;
  54. char textb[LLIM];
  55.  
  56. if ( ffopen( name, ( struct iobuffer * ) fbuf ) == FAIL )  {
  57.      strcat( name, defext );
  58.      if ( ffopen( name, ( struct iobuffer * ) fbuf ) == FAIL )  {
  59.           error( " Can't find file " );
  60.           name[0] = '\0';
  61.           return( FAIL );
  62.      }
  63. }
  64. first = YES;
  65. i     = charn;
  66. line  = cline;
  67.  
  68. for ( j = charn, k = 0; ( ( textb[k] = text[j] ) != '\0'); j++, k++ );
  69.   do  {
  70.     while ( i < LLIM  &&
  71.       ( ( c = egetc( ( struct iobuffer * ) fbuf ) ) != DFAIL )
  72.               &&  (c != '\n')  &&  ( c != ENDFILE ) )
  73.       text[i++] = ( char )c;
  74.  
  75.     if ( text[i-1] == '\r' )  i--;
  76.     text[i] = '\0';
  77.  
  78.     if ( c == ENDFILE )
  79.          for ( k = 0;
  80.              ( ( text[i] = textb[k] ) != '\0' ) &&  i < LLIM;
  81.                  i++, k++ );
  82.  
  83.     if ( first )  {
  84.          altered = YES;
  85.          puttext();
  86.          first = NO;
  87.          res = YES;
  88.     }
  89.     else  {
  90.       res = writ_txb( line, text );
  91.       line++;
  92.     }
  93.     i = 0;
  94.   } while ( c != DFAIL  &&  res  != FAIL  &&  c != ENDFILE );
  95.  
  96. ffclose( ( struct iobuffer * ) fbuf );
  97. gettext( cline );
  98. return YES;
  99. }
  100.  
  101.  
  102. int writefile(from,to,name,nametoprint,exiting)
  103. int  from, to, exiting;
  104. char *name, *nametoprint;
  105. {
  106. int  l, copying, copysecstart, copybytestart;
  107. char c, *t, *getline();
  108. struct iobuffer *textbuf;
  109.  
  110. puttext();
  111. if ( name[0] <= ' ' )  {
  112.      error("Bad name ");
  113.      return  FAIL;
  114. }
  115.  
  116. if ( fcreat( name, ( struct iobuffer * ) fbuf ) == FAIL )  {
  117.      error( "Can't create file " );
  118.      return(FAIL);
  119. }
  120.  
  121. putmess( "S|aving: " );
  122. putstr( nametoprint );
  123. copying = NO;
  124.  
  125. if ( exiting )  to = lastread;
  126.      else if ( to == lastl )  to = loc( lastl, 0 );
  127.  
  128. for ( l = from; l <= to; )  {
  129.       t = getline( l++ );
  130.       while ( *(t) )  {
  131.         if ( eputc( *t, ( struct iobuffer * ) fbuf ) == FAIL )
  132.              goto diskfull;
  133.         t++;
  134.       }
  135.  
  136.       if ( l <= to )  {
  137.            if ( eputc( '\r', ( struct iobuffer * ) fbuf ) == FAIL )
  138.                 goto diskfull;
  139.            if ( eputc( '\n', ( struct iobuffer * ) fbuf ) == FAIL )
  140.                 goto diskfull;
  141.       }
  142. }
  143.  
  144. if ( exiting && ( ( lastl == UNKNOWN ) || !goteof ) )  {
  145.      copybytestart = SECSIZ-( textbuf->_nleft + SECSIZ )%SECSIZ;
  146.      if ( eputc( '\r', ( struct iobuffer * ) fbuf ) == FAIL )  goto diskfull;
  147.      if ( eputc( '\n', ( struct iobuffer * ) fbuf ) == FAIL )  goto diskfull;
  148.      copying = YES;
  149.      while ( ( c = ( char ) egetc( textbuf ) ) != ENDFILE
  150.                &&    c != ( char ) DFAIL )
  151.      if ( eputc( c, ( struct iobuffer * ) fbuf ) == FAIL )  goto  diskfull;
  152. }
  153.  
  154. if ( eputc( ENDFILE, ( struct iobuffer * ) fbuf ) == FAIL )  goto  diskfull;
  155. if ( dflush( ( struct iobuffer * ) fbuf ) == FAIL )  goto  diskfull;
  156. if ( ffclose( ( struct iobuffer * ) fbuf ) == FAIL )  {
  157.      error( " Can't close file " );
  158.      goto  reposition;
  159. }
  160.  
  161. if ( copying )  ffclose( textbuf );
  162. return  YES;
  163.  
  164. diskfull:
  165.   error(" Disk full ");
  166. reposition:
  167.   if ( copying )  {
  168.        ffclose( ( struct iobuffer * ) fbuf );
  169.        funlink( name );
  170.        textbuf->_nleft = 0;
  171.        while ( copybytestart-- )  egetc( textbuf );
  172.    }
  173. return FAIL;
  174. }
  175.  
  176.  
  177. int exists(name)
  178. char *name;
  179. {
  180. char c;
  181.  
  182. c = 'y';
  183. if ( checkexists( name ) )  {
  184.      putmess(" OK to replace ");
  185.      putstr( name );
  186.      putstr(" ? ");
  187.      scr_co( ( c = getlow() ) );
  188.      putret();
  189.   }
  190. return c == 'y';
  191. }
  192.  
  193.  
  194. /* return YES if file 'name' exists, else NO
  195.  */
  196. int checkexists(name)
  197. char *name;
  198. {
  199. int  fd;
  200.     
  201. /* if (dskcheck(setjmp(dskerr)) != 0 || (fd=open(name,0)) == FAIL) return NO; */
  202. /* no setjmp just yet */
  203. if ( ( fd = open( name, 0 ) ) == FAIL )
  204.        return  NO;
  205. close( fd );
  206. return  YES;
  207. }
  208.  
  209.  
  210. /* used to input filename, etc in message routine
  211.  */
  212. int scans(answer,maxlen)
  213. char *answer;
  214. int maxlen;
  215. {
  216. unsigned char c;
  217. int  n, i;
  218.  
  219. maxlen--;
  220. n = 0;
  221. while ( n < maxlen )  {
  222.   switch( ( c = getscankey() ) )  {
  223.     case BACKSP :
  224.     case DELLEFT_P :
  225.       if ( n )  {
  226.         scr_co( BACKSP );
  227.         scr_co( ' ' );
  228.         scr_co( BACKSP );
  229.         n--;
  230.         answer--;
  231.       }
  232.       break;
  233.     case CR_P :
  234.     case ESCKEY_P :
  235.       n = maxlen;
  236.       break;
  237.     case LEFTKEY_P :    /* pass this key code thru */
  238.       c = tran[LEFTKEY];
  239.       goto passthru;
  240.     case RETRIEVE_P :
  241.       if ( *answer )  {
  242.            while ( *answer )  {
  243.              scr_co( *answer );
  244.              answer++;
  245.              n++;
  246.            }
  247.       break;
  248.       }
  249.       else  {
  250.         c = tran[RETRIEVE];       /* if no answer, display the key code */
  251.         goto passthru;
  252.       }
  253.     case GS :                     /* kludge to permit ESC in find/alter */
  254.       c = ESC;
  255.       default :
  256. passthru:
  257.      if ( c > 127 || c < 31 )  break;
  258.      scr_co(c);
  259.      *answer = c;
  260.      answer++;
  261.      n++;
  262.      break;
  263.    }
  264. }
  265. *answer = '\0';
  266. putret();                   /* set PARBIT of c back to 0 to */
  267. return ( c & ~PARBIT );     /* maintain compatibility with other calls */
  268. }
  269.  
  270.  
  271. void format(name)
  272. char *name;
  273. {
  274. char tempname[FILELEN], *n, *tn;
  275.  
  276. if ( !*name )  return;
  277.  
  278. if ( *(name+1) == ':' )
  279.        strcpy( tempname, name );
  280.   else  {
  281.     tempname[0] = ( char ) curdsk + 'A';
  282.     tempname[1] = ':';
  283.     name[FILELEN] = '\0';
  284.     strcpy( &tempname[2], name );
  285. }
  286.  
  287. for (  n = name, tn = tempname; ( *n = toupper( *tn ) ); n++, tn++ )
  288.        if ( *n == '.' )  break;
  289.  
  290. while( *n = tolower( *tn ) )  {
  291.         n++;
  292.         tn++;
  293. /*      ;  */
  294.   }
  295. }
  296.  
  297.  
  298. /* puts a new suffix on a file name
  299.  */
  300. void retag(name,tag)
  301. char *name, *tag;
  302. {
  303. for ( ; *name && *name != '.'; name++ );
  304. if ( !*name )  *name = '.';
  305. for ( ++name; ( *name = *tag ); name++, tag++ );
  306. }
  307.  
  308.  
  309. /* that's all */
  310.  
  311.