| 1 | // ============================================================= // |
|---|
| 2 | // // |
|---|
| 3 | // File : seqio.cxx // |
|---|
| 4 | // Purpose : // |
|---|
| 5 | // // |
|---|
| 6 | // Institute of Microbiology (Technical University Munich) // |
|---|
| 7 | // http://www.arb-home.de/ // |
|---|
| 8 | // // |
|---|
| 9 | // ============================================================= // |
|---|
| 10 | |
|---|
| 11 | #include "seqio.hxx" |
|---|
| 12 | |
|---|
| 13 | #include <arbdb.h> |
|---|
| 14 | |
|---|
| 15 | #define sio_assert(cond) arb_assert(cond) |
|---|
| 16 | |
|---|
| 17 | namespace SEQIO { |
|---|
| 18 | |
|---|
| 19 | char *fgets_smartLF(char *s, int size, FILE *stream) { |
|---|
| 20 | // same as fgets but also works with file in MacOS format |
|---|
| 21 | int i; |
|---|
| 22 | for (i = 0; i<(size-1); ++i) { |
|---|
| 23 | int byte = fgetc(stream); |
|---|
| 24 | if (byte == EOF) { |
|---|
| 25 | if (i == 0) return NULp; |
|---|
| 26 | break; |
|---|
| 27 | } |
|---|
| 28 | |
|---|
| 29 | s[i] = byte; |
|---|
| 30 | if (byte == '\n' || byte == '\r') break; |
|---|
| 31 | } |
|---|
| 32 | s[i] = 0; |
|---|
| 33 | |
|---|
| 34 | return s; |
|---|
| 35 | } |
|---|
| 36 | |
|---|
| 37 | bool read_string_pair(FILE *in, char *&s1, char *&s2, size_t& lineNr) { |
|---|
| 38 | // helper function to read import/export filters. |
|---|
| 39 | // returns true if successfully read |
|---|
| 40 | // |
|---|
| 41 | // 's1' is set to a heap-copy of the first token on line |
|---|
| 42 | // 's2' is set to a heap-copy of the rest of the line (or NULp if only one token is present) |
|---|
| 43 | // 'lineNr' is incremented with each line read |
|---|
| 44 | |
|---|
| 45 | s1 = NULp; |
|---|
| 46 | s2 = NULp; |
|---|
| 47 | |
|---|
| 48 | const int BUFSIZE = 8000; |
|---|
| 49 | char buffer[BUFSIZE]; |
|---|
| 50 | char *res; |
|---|
| 51 | |
|---|
| 52 | do { |
|---|
| 53 | res = fgets_smartLF(&buffer[0], BUFSIZE-1, in); |
|---|
| 54 | if (res) { |
|---|
| 55 | char *p = buffer; |
|---|
| 56 | |
|---|
| 57 | lineNr++; |
|---|
| 58 | |
|---|
| 59 | while (*p == ' ' || *p == '\t') p++; |
|---|
| 60 | if (*p != '#') { |
|---|
| 61 | int len = strlen(p)-1; |
|---|
| 62 | while (len >= 0 && strchr("\t \n\r", p[len])) { |
|---|
| 63 | p[len--] = 0; |
|---|
| 64 | } |
|---|
| 65 | |
|---|
| 66 | if (*p) { |
|---|
| 67 | char *e = strpbrk(p, " \t"); |
|---|
| 68 | |
|---|
| 69 | if (e) { |
|---|
| 70 | *(e++) = 0; |
|---|
| 71 | s1 = strdup(p); |
|---|
| 72 | |
|---|
| 73 | e += strspn(e, " \t"); // skip whitespace |
|---|
| 74 | |
|---|
| 75 | if (*e == '"') { |
|---|
| 76 | char *k = strrchr(e, '"'); |
|---|
| 77 | if (k!=e) { |
|---|
| 78 | e++; |
|---|
| 79 | *k=0; |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | s2 = strdup(e); |
|---|
| 83 | } |
|---|
| 84 | else { |
|---|
| 85 | s1 = strdup(p); |
|---|
| 86 | } |
|---|
| 87 | } |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | } while (res && !s1); // read until EOF or something found |
|---|
| 91 | |
|---|
| 92 | sio_assert(correlated(res, s1)); |
|---|
| 93 | |
|---|
| 94 | return res; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | }; |
|---|