home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / lib / gprim / comment / commentstream.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-16  |  2.5 KB  |  86 lines

  1. /* Copyright (c) 1992 The Geometry Center; University of Minnesota
  2.    1300 South Second Street;  Minneapolis, MN  55454, USA;
  3.    
  4. This file is part of geomview/OOGL. geomview/OOGL is free software;
  5. you can redistribute it and/or modify it only under the terms given in
  6. the file COPYING, which you should have received along with this file.
  7. This and other related software may be obtained via anonymous ftp from
  8. geom.umn.edu; email: software@geom.umn.edu. */
  9.  
  10. /*
  11.  * Authors: Charlie Gunn, Stuart Levy, Tamara Munzner, Mark Phillips,
  12.  * Nathaniel Thurston
  13.  */
  14.  
  15. #include <stdio.h>
  16. #include "commentP.h"
  17. #include "streampool.h"
  18. #include "handleP.h"
  19.  
  20. char *
  21. fbalanced(FILE *file)
  22. {
  23.     int depth = 1;
  24.     int bufsize = 10240;
  25.     char fmt[20];
  26.     char *buf = OOGLNewNE(char, bufsize, "Comment data");
  27.     char *bufp = buf;
  28.     if (fexpectstr(file, "{")) return NULL;
  29.     do {
  30.     if (bufp - buf >= bufsize - 2)
  31.         buf = OOGLRenewNE(char, buf, bufsize += 10240, "Comment data");
  32.     sprintf(fmt, "%%%d[^{}]", bufsize - (bufp - buf) - 2);
  33.     if (fscanf(file, fmt, bufp) > 0)
  34.         bufp += strlen(bufp);
  35.     switch(*bufp++ = fgetc(file)) {
  36.         case '}': depth--; break;
  37.         case '{': depth++; break;
  38.     }
  39.     } while (depth > 0);
  40.     *--bufp = '\0';
  41.     return OOGLRenewNE(char, buf, strlen(buf)+1, "Comment data");
  42. }
  43.  
  44. Geom *
  45. CommentImport( Pool *p )
  46. {
  47.     char *str;
  48.     register Comment *comment =
  49.     (Comment *)GeomCCreate(NULL, CommentMethods(), NULL);
  50.     FILE *file;
  51.     char *expect;
  52.  
  53.     if(p == NULL || (file = p->inf) == NULL)
  54.     return NULL;
  55.  
  56.     if(fexpectstr(file, "COMMENT"))
  57.     return NULL;
  58.  
  59.     if ((str = ftoken(file, 0)) == NULL) return NULL;
  60.     comment->name = OOGLNewNE(char, strlen(str)+1, "Comment name");
  61.     strcpy(comment->name, str);
  62.     if ((str = ftoken(file, 0)) == NULL) return NULL;
  63.     comment->type = OOGLNewNE(char, strlen(str)+1, "Comment type");
  64.     strcpy(comment->type, str);
  65.     if (fnextc(file, 0) == '{' ) {
  66.       comment->data = fbalanced(file); /* read until '}' */
  67.     } else {
  68.     if (fgetni(file, 1, &comment->length, 0) != 1) return NULL;
  69.     if (comment->length == 0) return NULL;
  70.     if (fexpectstr(file, " ")) return NULL;
  71.     comment->data = OOGLNewNE(char, comment->length, "Comment data");
  72.     if (fread(comment->data, comment->length, 1, file) != 1) return NULL;
  73.     }
  74.     return (Geom *)comment;
  75. }
  76.  
  77. int
  78. CommentExport( Comment *comment, Pool *pool )
  79. {
  80.     if(comment == NULL || pool == NULL || pool->outf == NULL)
  81.     return 0;
  82.  
  83.     (void) CommentFSave(comment, pool->outf, "");
  84.     return 1;
  85. }
  86.