home *** CD-ROM | disk | FTP | other *** search
/ Encyclopedia of Graphics File Formats Companion / GFF_CD.ISO / formats / off / code / writeobj.c < prev   
Encoding:
C/C++ Source or Header  |  1994-06-20  |  6.7 KB  |  288 lines

  1.  
  2. /*
  3.  *
  4.  * Description
  5.  *    Write an OFF object data file.
  6.  *
  7.  * Output
  8.  *
  9.  * Input
  10.  *    Obj        Pointer to object to be written
  11.  *    FileName    Name of header file for object
  12.  *    Directory    Name of directory in which to write file(s)
  13.  *    FileType    File type (OFF_ASCII or OFF_BINARY)
  14.  *
  15.  * Diagnostics
  16.  *    Returns 0 if successful, -1 if unsuccessful for any reason.
  17.  *
  18.  * Author
  19.  *    Randi J. Rost
  20.  *    Digital Equipment Corp.
  21.  *    Workstation Systems Engineering
  22.  *    Palo Alto, CA
  23.  *
  24.  * History
  25.  *    17-Nov-86    Created
  26.  *
  27.  */
  28.  
  29. #include <stdio.h>
  30. #include "off.h"
  31.  
  32. OFFWriteObj(Obj, FileName, Directory, FileType)
  33.     OFFObjDesc    *Obj;            /* Object data structure to write */
  34.     char    *FileName;        /* Name of file to be written */
  35.     char    *Directory;        /* Directory in which to write */
  36.     int        FileType;        /* Type of file (bin/ascii) to write */
  37.  
  38.     {
  39.     FILE    *ObjFile;
  40.     char    Name[OFF_BIGSTR];
  41.     OFFProperty    *pProp;
  42.     int        status;
  43.     char    Dir[OFF_BIGSTR];
  44.  
  45.  
  46.  
  47. /*  Open the file for writing  */
  48.     strcpy(Dir, Directory);
  49.     writeglob(Dir);
  50.     strcat(Dir, "/");
  51.     strcpy(Name, Dir);
  52.     strcat(Name, FileName);
  53.     ObjFile = fopen(Name,"w");
  54.  
  55. /*  If that doesn't work, punt  */
  56.     if (ObjFile == NULL)
  57.     { 
  58.     fprintf(stderr, "OFFWriteObj:  cannot open %s\n", Name);
  59.     return(-1);
  60.     }
  61.  
  62. /*  Initialize fields in object structure  */
  63.     pProp = Obj->FirstProp;
  64.  
  65. /*  Write lines to the header file  */
  66.     while(pProp != NULL)
  67.     {
  68.     status = WriteProperty(ObjFile, pProp, Dir, FileType);
  69.     if (status != 0)
  70.         {
  71.         fprintf(stderr, "OFFWriteObj: problem writing property %s\n",
  72.         pProp->PropName);
  73.         return(-1);
  74.         }
  75.     pProp = pProp->NextProp;
  76.     }
  77.  
  78.     fclose(ObjFile);
  79.     return(0);
  80.     }
  81.  
  82.  
  83.  
  84.  
  85. static int WriteProperty(ObjFile, pProp, Directory, FileType)
  86.     FILE    *ObjFile;
  87.     OFFProperty    *pProp;
  88.     char    *Directory;
  89.     int        FileType;
  90.  
  91.     {
  92.     long    *iptr;
  93.     short    *hptr;
  94.     float    *fptr;
  95.     double    *dptr;
  96.     char    *ptr;
  97.     int        i;
  98.     int        ntabs;
  99.     char    filename[OFF_BIGSTR];
  100.  
  101.  
  102.     if (strcmp(pProp->PropName, "comment") == 0)
  103.     {
  104.     fprintf(ObjFile, "%s\n", pProp->PropData);
  105.     return(0);
  106.     }
  107.  
  108.     if (strcmp(pProp->PropName, "nl") == 0)
  109.     {
  110.     fprintf(ObjFile, "\n");
  111.     return(0);
  112.     }
  113.  
  114.     if (strcmp(pProp->PropName, "name") == 0 ||
  115.     strcmp(pProp->PropName, "author") == 0 ||
  116.     strcmp(pProp->PropName, "type") == 0)
  117.     {
  118.     fprintf(ObjFile, "%s\t\t%s\n", pProp->PropName, pProp->PropData);
  119.     return(0);
  120.     }
  121.  
  122.     if (strcmp(pProp->PropName, "description") == 0 ||
  123.     strcmp(pProp->PropName, "copyright") == 0)
  124.     {
  125.     fprintf(ObjFile, "%s\t%s\n", pProp->PropName, pProp->PropData);
  126.     return(0);
  127.     }
  128.  
  129.     else
  130.     {
  131.     fprintf(ObjFile, "%s", pProp->PropName);
  132.     ntabs = (strlen(pProp->PropName) < 8) ? 2 : 1;
  133.     for (i = 0; i < ntabs; i++) fprintf(ObjFile, "\t");
  134.     switch (pProp->PropType)
  135.         {
  136.         case OFF_DEFAULT_DATA:
  137.         fprintf(ObjFile, "default\t\t"); break;
  138.         case OFF_GENERIC_DATA:
  139.         fprintf(ObjFile, "generic\t\t"); break;
  140.         case OFF_INDEXED_DATA:
  141.         fprintf(ObjFile, "indexed\t\t"); break;
  142.         case OFF_INDEXED_POLY_DATA:
  143.         fprintf(ObjFile, "indexed_poly\t"); break;
  144.         default:
  145.         fprintf(ObjFile, "unknown\t\t"); break;
  146.         }
  147.     fprintf(ObjFile, "%s", pProp->DataFormat);
  148.     ntabs = (strlen(pProp->DataFormat) < 8) ? 2 : 1;
  149.     for (i = 0; i < ntabs; i++) fprintf(ObjFile, "\t");
  150.     switch(pProp->PropType)
  151.         {
  152.         case OFF_DEFAULT_DATA:
  153.         ptr = pProp->PropData;
  154.         for (i = 0; i < strlen(pProp->DataFormat); i++)
  155.             {
  156.             switch (pProp->DataFormat[i])
  157.             {
  158.             case 'i':
  159.                  /* Make sure we're aligned on word boundary */
  160.                  ptr += (((int) ptr % 4) == 0) ?
  161.                 0 : 4 - (int) ptr % 4;
  162.                  iptr = (long *) ptr;
  163.                  fprintf(ObjFile, "%d ", *iptr);
  164.                  ptr += sizeof(long);
  165.                  break;
  166.             case 'b':
  167.                  fprintf(ObjFile, "%ud ", (unsigned char) *ptr);
  168.                  ptr++; 
  169.                  break;
  170.             case 'd':
  171.                  /* Make sure we're aligned on word boundary */
  172.                  ptr += (((int) ptr % 4) == 0) ?
  173.                 0 : 4 - (int) ptr % 4;
  174.                  dptr = (double *) ptr;
  175.                  fprintf(ObjFile, "%g ", *dptr);
  176.                  ptr += sizeof(double);
  177.                  break;
  178.             case 'h':
  179.                  /* Make sure we're aligned on halfword boundary */
  180.                  ptr += (((int) ptr % 2) == 0) ? 0 : 1;
  181.                  hptr = (short *) ptr;
  182.                  fprintf(ObjFile, "%d ", *hptr);
  183.                  ptr += sizeof(short);
  184.                  break;
  185.             case 'f':
  186.                  /* Make sure we're aligned on word boundary */
  187.                  ptr += (((int) ptr % 4) == 0) ?
  188.                 0 : 4 - (int) ptr % 4;
  189.                  fptr = (float *) ptr;
  190.                  fprintf(ObjFile, "%g ", *fptr);
  191.                  ptr += sizeof(float);
  192.                  break;
  193.             case 's':
  194.                  fprintf(ObjFile, "%s ", *((char **) ptr));
  195.                  ptr += strlen(ptr) + 1;
  196.                  break;
  197.             default:
  198.                  return(-1);
  199.             } /* switch */
  200.             } /* for */
  201.         fprintf(ObjFile, "\n");
  202.         break;
  203.  
  204.         case OFF_GENERIC_DATA:
  205.         fprintf(ObjFile, "%s\n", pProp->PropFileName);
  206.         strcpy(filename, Directory);
  207.         strcat(filename, pProp->PropFileName);
  208.         if ((i = OFFWriteGeneric(pProp, filename, FileType)) != 0)
  209.             return(-1);
  210.         break;
  211.  
  212.         case OFF_INDEXED_DATA:
  213.         fprintf(ObjFile, "%s\n", pProp->PropFileName);
  214.         strcpy(filename, Directory);
  215.         strcat(filename, pProp->PropFileName);
  216.         if ((i = OFFWriteIndexed(pProp, filename, FileType)) != 0)
  217.             return(-1);
  218.         break;
  219.  
  220.         case OFF_INDEXED_POLY_DATA:
  221.         fprintf(ObjFile, "%s\n", pProp->PropFileName);
  222.         strcpy(filename, Directory);
  223.         strcat(filename, pProp->PropFileName);
  224.         if (i = OFFWriteIndexedPoly(pProp, filename, FileType) != 0)
  225.             return(-1);
  226.         break;
  227.  
  228.         default:
  229.         return(-1);
  230.         break;
  231.         } /* switch */
  232.     }
  233.  
  234.     return(0);
  235.     }
  236.  
  237.  
  238. #include <pwd.h>
  239. #include <ctype.h>
  240.  
  241. static writeglob(string)
  242.     char    *string;
  243.  
  244.     {
  245.     struct    passwd    *p, *getpwname();
  246.     char    str1[160], str2[160];
  247.     char    *pstr;
  248.     char    *index(), *getenv();
  249.  
  250.    
  251. /*  If first character is '~', expand it  */
  252.     if (string[0] == '~')
  253.  
  254.     {
  255.  
  256.     strcpy(str1, string);
  257.  
  258.     /*  ~/... means use home directory  */
  259.     if (string[1] == '/')
  260.         {
  261.         strcpy(string, getenv("HOME"));
  262.         strcat(string, &(str1[1]));
  263.         }
  264.  
  265.     /*  ~whatever/... means use whatever's home directory  */
  266.     else
  267.         {
  268.         pstr = index(str1, '/');
  269.         strncpy(str2, &(string[1]), pstr - str1 - 1);
  270.         str2[pstr - str1 - 1] = 0;
  271.         p = getpwnam(str2);
  272.         strcpy(string, p->pw_dir);
  273.         strcat(string, pstr);
  274.         }
  275.  
  276.     }
  277.  
  278. /*  if directory is '/' (root) return null so concatenation works right  */
  279.     else if (strcmp(string, "/") == 0)
  280.     strcpy(string, "");
  281.  
  282. /*  if directory is null, return "." so concatenation works right */
  283.     else if (strlen(string) == 0)
  284.     strcpy(string, ".");
  285.     }
  286.  
  287.  
  288.