00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #include "common.h"
00048 #include "linefile.h"
00049 #include "hash.h"
00050 #include "emblParse.h"
00051
00052 static char const rcsid[] = "$Id: emblParse.c,v 1.3 2003/05/06 07:33:42 kate Exp $";
00053
00054 boolean emblLineGroup(struct lineFile *lf, char type[16], struct dyString *val)
00055
00056
00057 {
00058 char *line, *word;
00059 int typeLen = 0;
00060
00061 dyStringClear(val);
00062 while (lineFileNext(lf, &line, NULL))
00063 {
00064 line = skipLeadingSpaces(line);
00065
00066
00067 if (isspace(line[0]))
00068 errAbort("embl line that doesn't start with type line %d of %s",
00069 lf->lineIx, lf->fileName);
00070 if (typeLen == 0)
00071 {
00072 word = nextWord(&line);
00073 typeLen = strlen(word);
00074 if (typeLen >= 16)
00075 errAbort("Type word at start of line too long for embl file line %d of %s",
00076 lf->lineIx, lf->fileName);
00077 strcpy(type, word);
00078 }
00079 else if (!startsWith(type, line) || !isspace(line[typeLen]))
00080 {
00081 lineFileReuse(lf);
00082 break;
00083 }
00084 else
00085 {
00086 dyStringAppendC(val, '\n');
00087 word = nextWord(&line);
00088 }
00089
00090 if (line != NULL)
00091 {
00092
00093 if (isspace(line[0]))
00094 ++line;
00095 if (isspace(line[0]))
00096 ++line;
00097
00098
00099 dyStringAppend(val, line);
00100 }
00101 }
00102 return typeLen > 0;
00103 }
00104
00105 struct hash *emblRecord(struct lineFile *lf)
00106
00107
00108
00109 {
00110 struct hash *hash = NULL;
00111 char type[16];
00112 struct dyString *val = newDyString(256);
00113 boolean gotEnd = FALSE;
00114
00115 while (emblLineGroup(lf, type, val))
00116 {
00117 if (hash == NULL)
00118 hash = newHash(7);
00119 if (sameString(type, "//"))
00120 {
00121 gotEnd = TRUE;
00122 break;
00123 }
00124 hashAdd(hash, type, cloneString(val->string));
00125 }
00126 if (hash != NULL && !gotEnd)
00127 warn("Incomplete last record of embl file %s\n", lf->fileName);
00128 return hash;
00129 }
00130
00131 static void notEmbl(char *fileName)
00132
00133 {
00134 errAbort("%s is not an emblFile", fileName);
00135 }
00136
00137 struct lineFile *emblOpen(char *fileName, char type[256])
00138
00139
00140 {
00141 struct lineFile *lf = lineFileOpen(fileName, TRUE);
00142 struct hash *hash = emblRecord(lf);
00143 char *vv;
00144
00145 if (hash == NULL)
00146 notEmbl(fileName);
00147 if ((vv = hashFindVal(hash, "VV")) == NULL)
00148 notEmbl(fileName);
00149 if (type != NULL)
00150 {
00151 if (strlen(vv) >= 256)
00152 notEmbl(fileName);
00153 strcpy(type, vv);
00154 }
00155 freeHashAndVals(&hash);
00156 return lf;
00157 }