home *** CD-ROM | disk | FTP | other *** search
-
- /*
- *
- * Description
- * Write an OFF object data file.
- *
- * Output
- *
- * Input
- * Obj Pointer to object to be written
- * FileName Name of header file for object
- * Directory Name of directory in which to write file(s)
- * FileType File type (OFF_ASCII or OFF_BINARY)
- *
- * Diagnostics
- * Returns 0 if successful, -1 if unsuccessful for any reason.
- *
- * Author
- * Randi J. Rost
- * Digital Equipment Corp.
- * Workstation Systems Engineering
- * Palo Alto, CA
- *
- * History
- * 17-Nov-86 Created
- *
- */
-
- #include <stdio.h>
- #include "off.h"
-
- OFFWriteObj(Obj, FileName, Directory, FileType)
- OFFObjDesc *Obj; /* Object data structure to write */
- char *FileName; /* Name of file to be written */
- char *Directory; /* Directory in which to write */
- int FileType; /* Type of file (bin/ascii) to write */
-
- {
- FILE *ObjFile;
- char Name[OFF_BIGSTR];
- OFFProperty *pProp;
- int status;
- char Dir[OFF_BIGSTR];
-
-
-
- /* Open the file for writing */
- strcpy(Dir, Directory);
- writeglob(Dir);
- strcat(Dir, "/");
- strcpy(Name, Dir);
- strcat(Name, FileName);
- ObjFile = fopen(Name,"w");
-
- /* If that doesn't work, punt */
- if (ObjFile == NULL)
- {
- fprintf(stderr, "OFFWriteObj: cannot open %s\n", Name);
- return(-1);
- }
-
- /* Initialize fields in object structure */
- pProp = Obj->FirstProp;
-
- /* Write lines to the header file */
- while(pProp != NULL)
- {
- status = WriteProperty(ObjFile, pProp, Dir, FileType);
- if (status != 0)
- {
- fprintf(stderr, "OFFWriteObj: problem writing property %s\n",
- pProp->PropName);
- return(-1);
- }
- pProp = pProp->NextProp;
- }
-
- fclose(ObjFile);
- return(0);
- }
-
-
-
-
- static int WriteProperty(ObjFile, pProp, Directory, FileType)
- FILE *ObjFile;
- OFFProperty *pProp;
- char *Directory;
- int FileType;
-
- {
- long *iptr;
- short *hptr;
- float *fptr;
- double *dptr;
- char *ptr;
- int i;
- int ntabs;
- char filename[OFF_BIGSTR];
-
-
- if (strcmp(pProp->PropName, "comment") == 0)
- {
- fprintf(ObjFile, "%s\n", pProp->PropData);
- return(0);
- }
-
- if (strcmp(pProp->PropName, "nl") == 0)
- {
- fprintf(ObjFile, "\n");
- return(0);
- }
-
- if (strcmp(pProp->PropName, "name") == 0 ||
- strcmp(pProp->PropName, "author") == 0 ||
- strcmp(pProp->PropName, "type") == 0)
- {
- fprintf(ObjFile, "%s\t\t%s\n", pProp->PropName, pProp->PropData);
- return(0);
- }
-
- if (strcmp(pProp->PropName, "description") == 0 ||
- strcmp(pProp->PropName, "copyright") == 0)
- {
- fprintf(ObjFile, "%s\t%s\n", pProp->PropName, pProp->PropData);
- return(0);
- }
-
- else
- {
- fprintf(ObjFile, "%s", pProp->PropName);
- ntabs = (strlen(pProp->PropName) < 8) ? 2 : 1;
- for (i = 0; i < ntabs; i++) fprintf(ObjFile, "\t");
- switch (pProp->PropType)
- {
- case OFF_DEFAULT_DATA:
- fprintf(ObjFile, "default\t\t"); break;
- case OFF_GENERIC_DATA:
- fprintf(ObjFile, "generic\t\t"); break;
- case OFF_INDEXED_DATA:
- fprintf(ObjFile, "indexed\t\t"); break;
- case OFF_INDEXED_POLY_DATA:
- fprintf(ObjFile, "indexed_poly\t"); break;
- default:
- fprintf(ObjFile, "unknown\t\t"); break;
- }
- fprintf(ObjFile, "%s", pProp->DataFormat);
- ntabs = (strlen(pProp->DataFormat) < 8) ? 2 : 1;
- for (i = 0; i < ntabs; i++) fprintf(ObjFile, "\t");
- switch(pProp->PropType)
- {
- case OFF_DEFAULT_DATA:
- ptr = pProp->PropData;
- for (i = 0; i < strlen(pProp->DataFormat); i++)
- {
- switch (pProp->DataFormat[i])
- {
- case 'i':
- /* Make sure we're aligned on word boundary */
- ptr += (((int) ptr % 4) == 0) ?
- 0 : 4 - (int) ptr % 4;
- iptr = (long *) ptr;
- fprintf(ObjFile, "%d ", *iptr);
- ptr += sizeof(long);
- break;
- case 'b':
- fprintf(ObjFile, "%ud ", (unsigned char) *ptr);
- ptr++;
- break;
- case 'd':
- /* Make sure we're aligned on word boundary */
- ptr += (((int) ptr % 4) == 0) ?
- 0 : 4 - (int) ptr % 4;
- dptr = (double *) ptr;
- fprintf(ObjFile, "%g ", *dptr);
- ptr += sizeof(double);
- break;
- case 'h':
- /* Make sure we're aligned on halfword boundary */
- ptr += (((int) ptr % 2) == 0) ? 0 : 1;
- hptr = (short *) ptr;
- fprintf(ObjFile, "%d ", *hptr);
- ptr += sizeof(short);
- break;
- case 'f':
- /* Make sure we're aligned on word boundary */
- ptr += (((int) ptr % 4) == 0) ?
- 0 : 4 - (int) ptr % 4;
- fptr = (float *) ptr;
- fprintf(ObjFile, "%g ", *fptr);
- ptr += sizeof(float);
- break;
- case 's':
- fprintf(ObjFile, "%s ", *((char **) ptr));
- ptr += strlen(ptr) + 1;
- break;
- default:
- return(-1);
- } /* switch */
- } /* for */
- fprintf(ObjFile, "\n");
- break;
-
- case OFF_GENERIC_DATA:
- fprintf(ObjFile, "%s\n", pProp->PropFileName);
- strcpy(filename, Directory);
- strcat(filename, pProp->PropFileName);
- if ((i = OFFWriteGeneric(pProp, filename, FileType)) != 0)
- return(-1);
- break;
-
- case OFF_INDEXED_DATA:
- fprintf(ObjFile, "%s\n", pProp->PropFileName);
- strcpy(filename, Directory);
- strcat(filename, pProp->PropFileName);
- if ((i = OFFWriteIndexed(pProp, filename, FileType)) != 0)
- return(-1);
- break;
-
- case OFF_INDEXED_POLY_DATA:
- fprintf(ObjFile, "%s\n", pProp->PropFileName);
- strcpy(filename, Directory);
- strcat(filename, pProp->PropFileName);
- if (i = OFFWriteIndexedPoly(pProp, filename, FileType) != 0)
- return(-1);
- break;
-
- default:
- return(-1);
- break;
- } /* switch */
- }
-
- return(0);
- }
-
-
- #include <pwd.h>
- #include <ctype.h>
-
- static writeglob(string)
- char *string;
-
- {
- struct passwd *p, *getpwname();
- char str1[160], str2[160];
- char *pstr;
- char *index(), *getenv();
-
-
- /* If first character is '~', expand it */
- if (string[0] == '~')
-
- {
-
- strcpy(str1, string);
-
- /* ~/... means use home directory */
- if (string[1] == '/')
- {
- strcpy(string, getenv("HOME"));
- strcat(string, &(str1[1]));
- }
-
- /* ~whatever/... means use whatever's home directory */
- else
- {
- pstr = index(str1, '/');
- strncpy(str2, &(string[1]), pstr - str1 - 1);
- str2[pstr - str1 - 1] = 0;
- p = getpwnam(str2);
- strcpy(string, p->pw_dir);
- strcat(string, pstr);
- }
-
- }
-
- /* if directory is '/' (root) return null so concatenation works right */
- else if (strcmp(string, "/") == 0)
- strcpy(string, "");
-
- /* if directory is null, return "." so concatenation works right */
- else if (strlen(string) == 0)
- strcpy(string, ".");
- }
-
-
-