home *** CD-ROM | disk | FTP | other *** search
/ Simtel MSDOS - Coast to Coast / simteldosarchivecoasttocoast2.iso / awk / awk320sr.zip / AWKCVT.C < prev    next >
C/C++ Source or Header  |  1991-04-25  |  5KB  |  228 lines

  1. /*
  2.  * Awk ITEM conversion routines
  3.  *
  4.  * Copyright (C) 1988, 1989, 1990, 1991 by Rob Duff
  5.  * All rights reserved
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <math.h>
  11.  
  12. #include "awk.h"
  13. #include "awkfstr.h"
  14.  
  15. extern long fatol(FSTR);
  16.  
  17. static  char    onestr[66];
  18. static  char    twostr[66];
  19. static  char    numstr[66];
  20.  
  21. static void updatenf(ITEM*);
  22.  
  23. /*
  24.  * convert any item to a double
  25.  */
  26. double todouble(ITEM *ip)
  27. {
  28.     switch(ip->stype) {
  29.     case S_SHORT:
  30.         return (ip->svalue.ival);
  31.     case S_LONG:
  32.         return (ip->svalue.lval);
  33.     case S_DOUBLE:
  34.     case S_NUMBER:
  35.         return (ip->svalue.dval);
  36.     case S_STRING:
  37.         fstrncpy(numstr, ip->sstr, 64);
  38.         return(atof(numstr+1));
  39.     default:
  40.         return 0;
  41.     }
  42. }
  43.  
  44. /*
  45.  * convert any item to a long
  46.  */
  47. long tolong(ITEM *ip)
  48. {
  49.     switch(ip->stype) {
  50.     case S_SHORT:
  51.         return (long)(ip->svalue.ival);
  52.     case S_LONG:
  53.         return (long)(ip->svalue.lval);
  54.     case S_DOUBLE:
  55.     case S_NUMBER:
  56.         return (long)(ip->svalue.dval);
  57.     case S_STRING:
  58.         return(fatol(ip->sstr+1));
  59.     default:
  60.         return 0L;
  61.     }
  62. }
  63.  
  64. /*
  65.  * convert any item to an integer
  66.  */
  67. int tointeger(ITEM *ip)
  68. {
  69.     switch(ip->stype) {
  70.     case S_SHORT:
  71.     case S_LONG:
  72.         return (ip->svalue.ival);
  73.     case S_DOUBLE:
  74.     case S_NUMBER:
  75.         return (ip->svalue.dval);
  76.     case S_STRING:
  77.         return((int)fatol(ip->sstr+1));
  78.     default:
  79.         return 0;
  80.     }
  81. }
  82.  
  83. /*
  84.  * a value has been assigned to a field so update NF
  85.  */
  86. static void updatenf(ITEM *ip)
  87. {
  88.     int     i;
  89.  
  90.     parse();
  91.     i = (int)(ip - fieldtab);
  92.     if (i > tointeger(nf)) {
  93.         free_item(nf);
  94.         nf->stype = S_SHORT;
  95.         nf->svalue.ival = i;
  96.     }
  97. }
  98.  
  99. /*
  100.  * return a pointer to a variable
  101.  * if there is an error return a pointer to null
  102.  */
  103. ITEM *tovariable(ITEM *ip, int mode)
  104. {
  105.     ITEM    *vp;
  106.  
  107.     switch(ip->stype) {
  108.     case S_FIELD:
  109.         vp = ip->svalue.sptr;
  110.         if (mode == C_LOAD) {
  111.             if (vp == fieldtab)
  112.                 unparse();
  113.             else
  114.                 parse();
  115.         }
  116.         else {
  117.             if (vp == fieldtab) {
  118.                 modrecord = 1;
  119.                 modfield = 0;
  120.             }
  121.             else {
  122.                 updatenf(vp);
  123.                 modrecord = 0;
  124.                 modfield = 1;
  125.             }
  126.         }
  127.         return ip->svalue.sptr;
  128.     case S_BUILT:
  129.         vp = ip->svalue.sptr;
  130.         if (vp <= ofmt) {
  131.             parse();
  132.             unparse();
  133.             if (vp == nf && mode == C_STORE)
  134.                 modfield = 1;
  135.         }
  136.     case S_SIMPLE:
  137.     case S_STACK:
  138.     case S_ARRAY:
  139.         return ip->svalue.sptr;
  140.     default:
  141.         return vartab;
  142.     }
  143. }
  144.  
  145. /*
  146.  * convert an item to a FYLE pointer
  147.  * the parser makes sure that the item
  148.  * is of type FILES
  149.  */
  150. FYLE *tofyle(ITEM *ip)
  151. {
  152.     FYLE    *fp;
  153.  
  154.     fp = ip->svalue.fptr;
  155.     fstrncpy(numstr, fp->fname, 64);
  156.     if (fp->ffyle == NULL)
  157.         fp->ffyle = fopen(numstr+1, fp->fmode);
  158.     if (fp->ffyle == NULL && fp->fmode[0] != 'r')
  159.         error("can't open file \"%s\" for %s", numstr+1, fp->fmode);
  160.     return fp;
  161. }
  162.  
  163. /*
  164.  * convert any item to a string
  165.  * if it is not already a string
  166.  * sprintf it into a buffer
  167.  */
  168. FSTR onestring(ITEM *ip)
  169. {
  170.     *onestr = ZSTR;
  171.     switch(ip->stype) {
  172.     case S_SHORT:
  173.         sprintf(onestr+1, "%d", ip->svalue.ival);
  174.         return onestr;
  175.     case S_LONG:
  176.         sprintf(onestr+1, "%ld", ip->svalue.lval);
  177.         return onestr;
  178.     case S_DOUBLE:
  179.         sprintf(onestr+1, ofmtstr+1, ip->svalue.dval);
  180.         return onestr;
  181.     case S_NUMBER:
  182.     case S_STRING:
  183.         return(ip->sstr);
  184.     default:
  185.         return nullstr;
  186.     }
  187. }
  188.  
  189. /*
  190.  * convert any item to a string
  191.  * if it is not already a string
  192.  * sprintf it into a buffer
  193.  */
  194. FSTR tostring(ITEM *ip)
  195. {
  196.     *twostr = ZSTR;
  197.     switch(ip->stype) {
  198.     case S_SHORT:
  199.         sprintf(twostr+1, "%d", ip->svalue.ival);
  200.         return twostr;
  201.     case S_LONG:
  202.         sprintf(twostr+1, "%ld", ip->svalue.lval);
  203.         return twostr;
  204.     case S_DOUBLE:
  205.         sprintf(twostr+1, ofmtstr+1, ip->svalue.dval);
  206.         return twostr;
  207.     case S_NUMBER:
  208.     case S_STRING:
  209.         return(ip->sstr);
  210.     default:
  211.         return nullstr;
  212.     }
  213. }
  214.  
  215. /*
  216.  * coerce to regular expression
  217.  * (regexp uses buffer)
  218.  */
  219. char *toregexp(ITEM *ip)
  220. {
  221.     if (ip->stype == S_REGEXP)
  222.         return ip->svalue.cptr;
  223.     lineptr = tostring(ip) + 1;
  224.     yyinit();
  225.     return regexp(0);
  226. }
  227.  
  228.