00001
00002
00003
00004
00005 #include "common.h"
00006 #include "tabRow.h"
00007
00008 struct tabRow *tabRowNew(int colCount)
00009
00010 {
00011 struct tabRow *row = needMem(sizeof(*row) + colCount*sizeof(char*));
00012 row->colCount = colCount;
00013 return row;
00014 }
00015
00016 int tabRowMaxColCount(struct tabRow *rowList)
00017
00018 {
00019 int maxCount = 0;
00020 struct tabRow *row;
00021 for (row = rowList; row != NULL; row = row->next)
00022 if (row->colCount > maxCount)
00023 maxCount = row->colCount;
00024 return maxCount;
00025 }
00026
00027 struct tabRow *tabRowByWhite(struct slName *lineList, char *fileName,
00028 boolean varCol)
00029
00030
00031 {
00032 struct slName *line;
00033 struct tabRow *rowList = NULL, *row;
00034
00035 if (varCol)
00036 {
00037 for (line = lineList; line != NULL; line = line->next)
00038 {
00039 char *s = line->name;
00040 int rowSize = chopByWhite(s, NULL, 0);
00041 row = tabRowNew(rowSize);
00042 chopByWhite(s, row->columns, rowSize);
00043 slAddHead(&rowList, row);
00044 }
00045 }
00046 else
00047 {
00048 if (lineList)
00049 {
00050 int rowSize = chopByWhite(lineList->name, NULL, 0);
00051 int extraSize = rowSize+1;
00052 int ix = 1;
00053 for (line = lineList; line != NULL; line = line->next, ++ix)
00054 {
00055 int oneSize;
00056 row = tabRowNew(rowSize);
00057 oneSize = chopByWhite(line->name, row->columns, extraSize);
00058 if (oneSize != rowSize)
00059 {
00060 if (oneSize > rowSize)
00061 errAbort("Got more than the expected %d columns line %d of %s",
00062 rowSize, ix, fileName);
00063 else
00064 errAbort("Expecting %d columns got %d, line %d of %s",
00065 rowSize, oneSize, ix, fileName);
00066
00067 }
00068 slAddHead(&rowList, row);
00069 }
00070 }
00071 }
00072 slReverse(&rowList);
00073 return rowList;
00074 }
00075
00076 struct tabRow *tabRowByChar(struct slName *lineList, char c, char *fileName,
00077 boolean varCol)
00078
00079
00080 {
00081 struct slName *line;
00082 struct tabRow *rowList = NULL, *row;
00083
00084 if (varCol)
00085 {
00086 for (line = lineList; line != NULL; line = line->next)
00087 {
00088 char *s = line->name;
00089 int rowSize = countChars(s, c) + 1;
00090 row = tabRowNew(rowSize);
00091 chopByChar(s, c, row->columns, rowSize);
00092 slAddHead(&rowList, row);
00093 }
00094 }
00095 else
00096 {
00097 if (lineList)
00098 {
00099 int rowSize = countChars(lineList->name, c) + 1;
00100 int extraSize = rowSize+1;
00101 int ix = 1;
00102 for (line = lineList; line != NULL; line = line->next, ++ix)
00103 {
00104 int oneSize;
00105 row = tabRowNew(rowSize);
00106 oneSize = chopByChar(line->name, c, row->columns, extraSize);
00107 if (oneSize != rowSize)
00108 {
00109 if (oneSize > rowSize)
00110 errAbort("Got more than the expected %d columns line %d of %s",
00111 rowSize, ix, fileName);
00112 else
00113 errAbort("Expecting %d columns got %d, line %d of %s",
00114 rowSize, oneSize, ix, fileName);
00115
00116 }
00117 slAddHead(&rowList, row);
00118 }
00119 }
00120 }
00121 return rowList;
00122 }
00123
00124 struct slInt *tabRowGuessFixedOffsets(struct slName *lineList, char *fileName)
00125
00126
00127 {
00128 struct slInt *offList = NULL, *off;
00129
00130 if (lineList)
00131 {
00132 char *spaceRec = cloneString(lineList->name), *s;
00133 int lineSize = strlen(spaceRec);
00134 struct slName *line;
00135 int lineIx=1;
00136
00137
00138
00139
00140 for (line = lineList->next; line != NULL; line = line->next, ++lineIx)
00141 {
00142 int i;
00143 s = line->name;
00144 if (strlen(s) != lineSize)
00145 errAbort("Line %d of %s has %lu chars, but first line has just %d",
00146 lineIx, fileName, (unsigned long)strlen(s), lineSize);
00147 for (i=0; i<lineSize; ++i)
00148 {
00149 if (s[i] != ' ')
00150 spaceRec[i] = 'X';
00151 }
00152 }
00153
00154
00155 s = spaceRec;
00156 for (;;)
00157 {
00158 s = skipLeadingSpaces(s);
00159 if (s == NULL || s[0] == 0)
00160 break;
00161 AllocVar(off);
00162 off->val = s - spaceRec;
00163 slAddHead(&offList, off);
00164 s = skipToSpaces(s);
00165 }
00166 slReverse(&offList);
00167 }
00168 return offList;
00169 }
00170
00171 struct tabRow *tabRowByFixedOffsets(struct slName *lineList, struct slInt *offList,
00172 char *fileName)
00173
00174
00175 {
00176 struct slName *line;
00177 struct slInt *off;
00178 struct tabRow *rowList = NULL, *row;
00179 int rowSize = slCount(offList);
00180
00181 if (lineList)
00182 {
00183 int lineSize = strlen(lineList->name);
00184 int lineIx = 1;
00185 for (off = offList; off != NULL; off = off->next)
00186 {
00187 if (off->val >= lineSize)
00188 errAbort("Offset %d is bigger than lineSize of %d", off->val, lineSize);
00189 }
00190 for (line = lineList; line != NULL; line = line->next, ++lineIx)
00191 {
00192 char *linePt = line->name;
00193 int offIx = 0;
00194 if (strlen(linePt) != lineSize)
00195 errAbort("Line %d of %s has %lu chars, but first line has just %d",
00196 lineIx, fileName, (unsigned long)strlen(linePt), lineSize);
00197 row = tabRowNew(rowSize);
00198 for (off = offList; off != NULL; off = off->next, ++offIx)
00199 {
00200 int start = off->val, end;
00201 if (off->next != NULL)
00202 {
00203 end = off->next->val-1;
00204 if (linePt[end] != ' ')
00205 errAbort("Line %d of %s expecting space column %d, got %c",
00206 lineIx, fileName, end, linePt[end]);
00207 }
00208 else
00209 end = lineSize;
00210 linePt[end] = 0;
00211 row->columns[offIx] = trimSpaces(linePt + start);
00212 }
00213 slAddHead(&rowList, row);
00214 }
00215 slReverse(&rowList);
00216 }
00217 return rowList;
00218 }
00219
00220 struct tabRow *tabRowByFixedGuess(struct slName *lineList, char *fileName)
00221
00222 {
00223 struct slInt *offList = tabRowGuessFixedOffsets(lineList, fileName);
00224 struct tabRow *rowList = tabRowByFixedOffsets(lineList, offList, fileName);
00225 slFreeList(&offList);
00226 return rowList;
00227 }
00228