MCPcopy Create free account
hub / github.com/GenericMappingTools/gmt / gmtapi_split_words

Function gmtapi_split_words

src/gmt_api.c:14116–14160  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

14114}
14115
14116GMT_LOCAL struct GMT_WORD * gmtapi_split_words (const char *line) {
14117 /* Split line into an array of words where words are separated by either
14118 * a space (like between options), the occurrence of "][" sequences, or
14119 * items separated by slashes "/", commas ",", bars "|" or hyphens "-".
14120 * These are the places where we are allowed to break the line. */
14121 struct GMT_WORD *array = NULL;
14122 unsigned int n = 0, c, start = 0, next, end, j, stop, space = 0, n_alloc = GMT_LEN256, hyphen = 0;
14123 array = calloc (n_alloc, sizeof (struct GMT_WORD));
14124 while (line[start]) { /* More line to chop up */
14125 hyphen = 0; /* Initialize */
14126 /* Find the next break location */
14127 stop = start;
14128 while (line[stop] && !(strchr (" ,/|", line[stop]) || (line[stop] == ']' && line[stop+1] == '[') || (hyphen = gmtapi_hyphen (line, stop)))) stop++;
14129 end = next = stop; /* Mark likely end */
14130 array[n].space = space; /* Do we need a leading space (set via previous word)? */
14131 if (line[stop] == ' ') { /* Skip the space to start over at next word */
14132 while (line[stop] == ' ') stop++; /* In case there are more than one space */
14133 next = stop; space = 1;
14134 }
14135 else if (line[stop] && strchr (",/|]", line[stop])) { /* Include this char then break */
14136 next = ++end, space = 0;
14137 }
14138 else if (line[stop] == '-') { /* Include this char then break with no break symbol */
14139 next = ++end, space = hyphen;
14140 }
14141 array[n].word = calloc (end - start + 1, sizeof (char)); /* Allocate space for word */
14142 for (j = start, c = 0; j < end; j++, c++) array[n].word[c] = line[j];
14143 n++; /* Got another word */
14144 if (n == n_alloc) { /* Need more memory, must be a long line */
14145 n_alloc += GMT_LEN256;
14146 array = realloc (array, n_alloc*sizeof (struct GMT_WORD));
14147 }
14148 start = next; /* Advance to start of next word */
14149 }
14150 /* Finalize array length - keep one extra to indicate end of array */
14151 array = realloc (array, (n+1)*sizeof (struct GMT_WORD));
14152#if 0 /* Left for possible debug in case there are unresolved issues of breaking things up into words */
14153 fprintf (stderr, "Found %d words\n", n);
14154 for (j = 0; j < n; j++)
14155 fprintf (stderr, "%2.2d: [%d] %s\n", j, array[j].space, array[j].word);
14156 fprintf (stderr, "\n");
14157#endif
14158
14159 return (array);
14160}
14161
14162GMT_LOCAL unsigned int gmtapi_space (unsigned int space) {
14163 return (space == 2) ? 0 : space;

Callers 1

gmtapi_wrap_the_lineFunction · 0.85

Calls 1

gmtapi_hyphenFunction · 0.85

Tested by

no test coverage detected