home *** CD-ROM | disk | FTP | other *** search
/ Oakland CPM Archive / oakcpm.iso / cpm / editc80 / ed9.c < prev    next >
Encoding:
C/C++ Source or Header  |  1984-06-15  |  3.5 KB  |  239 lines

  1. /* Screen editor:  general utilities
  2.  *           C/80 version
  3.  *
  4.  * Source:  ed9.c
  5.  * Version: May 15, 1981.
  6.  */
  7.  
  8. /* define global constants and variables */
  9.  
  10. #include ed1.h
  11.  
  12. /* return: is first token in args a number ? */
  13. /* return value of number in *val            */
  14.  
  15. number(args,val) char *args; int *val;
  16. {
  17. char c;
  18.     c= *args++;
  19.     if ((c<'0')||(c>'9')) {
  20.         return(NO);
  21.     }
  22.     *val=c-'0';
  23.     while (c= *args++) {
  24.         if ((c<'0')||(c>'9')) {
  25.             break;
  26.         }
  27.         *val=(*val*10)+c-'0';
  28.     }
  29.     return(YES);
  30. }
  31.  
  32. /* convert character buffer to numeric */
  33.  
  34. ctoi(buf,index) char *buf; int index;
  35. {
  36. int k;
  37.     while ( (buf[index]==' ')||
  38.         (buf[index]==TAB) ) {
  39.         index++;
  40.     }
  41.     k=0;
  42.     while ((buf[index]>='0')ff(buf[index]<='9')) {
  43.         k=(k*10)+buf[index]-'0';
  44.         index++;
  45.     }
  46.     return(k);
  47. }
  48.  
  49.  
  50. /* put decimal integer n in field width >= w.
  51.  * left justify the number in the field.
  52.  */
  53.  
  54. putdec(n,w) int n,w;
  55. {
  56. char chars[10];
  57. int i,nd;
  58.     nd=itoc(n,chars,10);
  59.     i=0;
  60.     while (i<nd) {
  61.         syscout(chars[i++]);
  62.     }
  63.     i=nd;
  64.     while (i++<w) {
  65.         syscout(' ');
  66.     }
  67. }
  68.  
  69. /* convert integer n to character string in str */
  70.  
  71. itoc(n,str,size) int n; char *str; int size;
  72. {
  73. int absval;
  74. int len;
  75. int i,j,k;
  76.     absval=abs(n);
  77.     /* generate digits */
  78.     str[0]=0;
  79.     i=1;
  80.     while (i<size) {
  81.         str[i++]=(absval%10)+'0';
  82.         absval=absval/10;
  83.         if (absval==0) {
  84.             break;
  85.         }
  86.     }
  87.     /* generate sign */
  88.     if ((i<size)ff(n<0)) {
  89.         str[i++]='-';
  90.     }
  91.     len=i-1;
  92.     /* reverse sign, digits */
  93.     i--;
  94.     j=0;
  95.     while (j<i) {
  96.         k=str[i];
  97.         str[i]=str[j];
  98.         str[j]=k;
  99.         i--;
  100.         j++;
  101.     }
  102.     return(len);
  103. }
  104.  
  105. /* system error routine */
  106.  
  107. syserr(s) char *s;
  108. {
  109.     pmtmess("system error: ",s);
  110. }
  111.  
  112. /* user error routine */
  113.  
  114. error(s) char *s;
  115. {
  116.     pmtmess("error: ",s);
  117. }
  118.  
  119. /* disk error routine */
  120.  
  121. diskerr(s) char *s;
  122. {
  123.     pmtmess("disk error: ",s);
  124. }
  125.  
  126. /* read the next line of the file into
  127.  * the buffer of size n that p points to.
  128.  * Successive calls to readline() read the file
  129.  * from front to back.
  130.  */
  131.  
  132. readline(file,p,n) int file; char *p; int n;
  133. {
  134. int c;
  135. int k;
  136.     k=0;
  137.     while (1) {
  138.         c=sysrdch(file);
  139.         if (c==ERR) {
  140.             return(ERR);
  141.         }
  142.         if (c==EOF) {
  143.             /* ignore line without CR */
  144.             return (EOF);
  145.         }
  146.         if (c==CR) {
  147.             return(k);
  148.         }
  149.         if (k<n) {
  150.             /* move char to buffer */
  151.             *p++=c;
  152.         }
  153.         /* always bump count */
  154.         k++;
  155.     }
  156. }
  157.  
  158. /* push (same as write) line to end of file.
  159.  * line is in the buffer of size n that p points to.
  160.  * lines written by this routine may be read by
  161.  * either readline() or popline().
  162.  */
  163.  
  164. pushline(file,p,n) int file; char *p; int n;
  165. {
  166.     /* write all but trailing CR */
  167.     while ((n--)>0) {
  168.         if (syspshch(*p++,file)==ERR) {
  169.             return(ERR);
  170.         }
  171.     }
  172.     /* write trailing CR */
  173.     return(syspshch(CR,file));
  174. }
  175.  
  176. /* pop a line from the back of the file.
  177.  * the line should have been pushed using pushline().
  178.  */
  179.  
  180. popline(file,p,n) int file; char *p; int n;
  181. {
  182. int c;
  183. int k, kmax, t;
  184.     /* first char must be CR */
  185.     c=syspopch(file);
  186.     if (c==EOF) {
  187.         /* at START of file */
  188.         return(EOF);
  189.     }
  190.     if (c==CR) {
  191.         /* put into buffer */
  192.         *p++=CR;
  193.         k=1;
  194.     }
  195.     else {
  196.         syserr("popline: missing CR");
  197.         return(ERR);
  198.     }
  199.     /* pop line into buffer in reverse order */
  200.     while (1) {
  201.         c=syspopch(file);
  202.         if (c==ERR) {
  203.             return(ERR);
  204.         }
  205.         if (c==EOF) {
  206.             break;
  207.         }
  208.         if (c==CR) {
  209.             /* this ends ANOTHER line */
  210.             /* push it back           */
  211.             if (syspshch(CR,file)==ERR) {
  212.                 return(ERR);
  213.             }
  214.             break;
  215.         }
  216.         /* non-special case */
  217.         if (k<n) {
  218.             /* put into buffer */
  219.             *p++=c;
  220.         }
  221.         /* always bump count */
  222.         k++;
  223.     }
  224.     /* remember if we truncated the line */
  225.     kmax=k;
  226.     /* reverse the buffer */
  227.     k=min(k,n-1);
  228.     t=0;
  229.     while (k>t) {
  230.         /* swap p[t], p[k] */
  231.         c=p[k];
  232.         p[k]=p[t];
  233.         p[t]=c;
  234.         k--;
  235.         t++;
  236.     }
  237.     return(kmax);
  238. }
  239.