home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume10 / ifp / part03 / interp / F_string.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-07-05  |  3.8 KB  |  142 lines

  1.  
  2. /****** F_string.c ****************************************************/
  3. /**                                                                  **/
  4. /**                    University of Illinois                        **/
  5. /**                                                                  **/
  6. /**                Department of Computer Science                    **/
  7. /**                                                                  **/
  8. /**   Tool: IFP                         Version: 0.5                 **/
  9. /**                                                                  **/
  10. /**   Author:  Arch D. Robison          Date:   May 1, 1985          **/
  11. /**                                                                  **/
  12. /**   Revised by: Arch D. Robison       Date:  July 5, 1985          **/
  13. /**                                                                  **/
  14. /**   Principal Investigators: Prof. R. H. Campbell                  **/
  15. /**                            Prof. W. J. Kubitz                    **/
  16. /**                                                                  **/
  17. /**                                                                  **/
  18. /**------------------------------------------------------------------**/
  19. /**   (C) Copyright 1987  University of Illinois Board of Trustees   **/
  20. /**                       All Rights Reserved.                       **/
  21. /**********************************************************************/
  22.  
  23. #include <stdio.h>
  24. #include "struct.h"
  25. #include "string.h"
  26. #include "node.h"
  27.  
  28. /*
  29.  * F_Patom
  30.  *
  31.  * Convert an atom to it's string representation.
  32.  */
  33. private F_Patom (InOut)
  34.    register ObjectPtr InOut;
  35.    {
  36.       CharPtr U;
  37.       char Buf[255];
  38.       StrPtr S;
  39.       register char *T;
  40.       extern char *sprintf();
  41.  
  42.       T = Buf;
  43.       switch (InOut->Tag) {
  44.      case INT:
  45.         (void) sprintf (T,"%d",InOut->Int);
  46.         break;
  47.      case FLOAT:
  48.         (void) sprintf (T,"%g",InOut->Float);
  49.         break;
  50.      case BOOLEAN:
  51.         (void) sprintf (T,InOut->Bool ? "t":"f");
  52.         break;
  53.      case STRING:
  54.         return;
  55.      default:
  56.         FunError ("not atomic",InOut);
  57.         return;
  58.       }
  59.       S = NULL;
  60.       CPInit (&U,&S);
  61.       do CPAppend (&U,*T); while (*T++);
  62.       RepTag (InOut,STRING);
  63.       InOut->String = S;
  64.    }
  65.  
  66.  
  67. /*
  68.  * F_Explode
  69.  *
  70.  * Convert a string to a list of characters
  71.  */
  72. private F_Explode (InOut)
  73.    ObjectPtr InOut;
  74.    {
  75.       ListPtr Result = NULL;
  76.       MetaPtr A = &Result;
  77.       CharPtr U;
  78.       char C[2];
  79.  
  80.       if (InOut->Tag != STRING)
  81.      FunError ("not a string",InOut);
  82.       else {
  83.      CPInit (&U,&InOut->String);
  84.      while (CPRead (&U,C,2)) {
  85.         NewList (A,1L);
  86.         if (SysError) {DelLPtr (Result); return;}
  87.         (*A)->Val.Tag = STRING;
  88.         (*A)->Val.String = CopySPtr (CharString [C[0] & 0x7F]);
  89.         A = &(*A)->Next;
  90.      }
  91.      RepTag (InOut,LIST);
  92.      InOut->List = Result;
  93.       }
  94.    }
  95.  
  96.  
  97. /*
  98.  * F_Implode
  99.  *
  100.  * Catenate a list of strings into a single string.
  101.  */
  102. private F_Implode (InOut)
  103.    ObjectPtr InOut;
  104.    {
  105.       CharPtr U,V;
  106.       char C[2];
  107.       ListPtr P;
  108.       StrPtr S;
  109.  
  110.       if (InOut->Tag != LIST)
  111.      FunError ("not a sequence",InOut);
  112.       else {
  113.      S = NULL;
  114.      CPInit (&U,&S);
  115.      for (P = InOut->List; P != NULL; P=P->Next) {
  116.         if (P->Val.Tag != STRING) {
  117.            FunError ("non-string in sequence",InOut);
  118.            CPAppend (&U,'\0');
  119.            DelSPtr (S);
  120.            return;
  121.         } else {
  122.            CPInit (&V,&P->Val.String);
  123.            while (CPRead (&V,C,2)) CPAppend (&U,C[0]);
  124.         }
  125.      }
  126.      CPAppend (&U,'\0');
  127.      RepTag (InOut,STRING);
  128.      InOut->String = S;
  129.       }
  130.    }
  131.  
  132.  
  133. void D_string ()
  134.    {                             
  135.       (void) PrimDef (F_Explode,"explode",SysNode);
  136.       (void) PrimDef (F_Implode,"implode",SysNode);
  137.       (void) PrimDef (F_Patom,"patom",SysNode);
  138.    }
  139.  
  140. /************************** end of F_string **************************/
  141.  
  142.