home *** CD-ROM | disk | FTP | other *** search
- /*
- * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
- * All Rights Reserved.
- *
- * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
- * the contents of this file may not be disclosed to third parties, copied or
- * duplicated in any form, in whole or in part, without the prior written
- * permission of Silicon Graphics, Inc.
- *
- * RESTRICTED RIGHTS LEGEND:
- * Use, duplication or disclosure by the Government is subject to restrictions
- * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
- * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
- * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
- * rights reserved under the Copyright Laws of the United States.
- */
- #include "data.h"
- #include <string.h>
- #include <stdio.h>
- #include <malloc.h>
- #include <sys/types.h>
- #include <sys/stat.h>
-
- static char* currentMolecule = NULL;
-
- char *getSelection() {
- if (currentMolecule)
- return strdup(currentMolecule);
- else
- return NULL;
- }
-
- void parseElement(char *s) {
- char name[ELEMNAMESIZE];
- float radius, red, green, blue;
-
- sscanf(s, "%[A-Za-z] = %f ( %f, %f, %f )", name, &radius, &red, &green, &blue);
- elementRoot = createElement(elementRoot, name, radius, red, green, blue);
- }
-
- void parseAtom(char *s, struct molecule *m) {
- char name[ELEMNAMESIZE];
- float x, y, z;
-
- sscanf(s, "%[A-Za-z] : %f %f %f", name, &x, &y, &z);
- addAtom(m, name, x, y, z);
- }
-
-
- void parse(char *s, struct molecule *m) {
- char name[ELEMNAMESIZE];
- char type[ELEMNAMESIZE];
-
- if ((s[0] == '\0') || (s[0] == ';'))
- return;
-
- sscanf(s, "%[A-Za-z] %[:=]", name, type);
-
- if (!type)
- return;
-
- switch(type[0]) {
- case ':':
- parseAtom(s, m);
- break;
- case '=':
- parseElement(s);
- break;
- default:
- break;
- }
- }
-
- void buildMolecule(char *text) {
- char line[80];
- char *lp, *tp;
- struct molecule *m;
- if (!text)
- return;
-
- if(currentMolecule)
- free(currentMolecule);
- currentMolecule = text;
-
- moleculeRoot = freeMolecules(moleculeRoot);
- elementRoot = freeElements(elementRoot);
- m = moleculeRoot = createMolecule(moleculeRoot, "Test");
-
- tp = text;
- while(*tp != '\0') {
- lp = line;
- while((*tp != '\n') && (*tp != '\0'))
- *lp++ = *tp++;
- *lp = '\0';
- tp++;
- parse(line, m);
- }
- }
-
- char *readFile(FILE *fp, char *theFilename)
- {
- struct stat theStats;
- char *fileBuffer;
-
- if (stat(theFilename, &theStats) < 0){
- perror("stat");
- exit(1);
- }
- fileBuffer = calloc(theStats.st_size+1, 1);
- if (!fileBuffer)
- return;
-
- fread(fileBuffer, 1, theStats.st_size, fp);
- if (ferror(fp)) {
- free(fileBuffer);
- fileBuffer = NULL;
- }
- fclose(fp);
- return fileBuffer;
- }
-
- void openFile(char *theFilename)
- {
- FILE *fp;
- char *fileBuffer;
- if (!theFilename)
- return;
-
- fp = fopen(theFilename, "r");
- if (!fp)
- return;
- if (!(fileBuffer = readFile(fp, theFilename)))
- return;
- buildMolecule(fileBuffer);
-
- }
-
-