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

Function gmt_getmodopt

src/gmt_support.c:14741–14804  ·  view source on GitHub ↗

! . */

Source from the content-addressed store, hash-verified

14739
14740/*! . */
14741unsigned int gmt_getmodopt (struct GMT_CTRL *GMT, const char option, const char *string, const char *sep, unsigned int *pos, char *token, unsigned int *err) {
14742 /* Breaks string into tokens separated by one of more modifier separator
14743 * characters (in sep) following a "+". E.g., if you are looking for a
14744 * set of modifiers +a, +g<arg>, and +z, set sep = "agz" and any other
14745 * +<char> will fail. Set *pos to 0 before first call.
14746 * If any of your <arg> may contain a + (e.g., +t"My +ve experience") then you can
14747 * 1) Escape the plus with backslash (e.g., +t"My \+ve experience")
14748 * 2) Put everything in quotes (e.g., +t'"My +ve experience"') as we
14749 * will ignore plus-signs inside quotes. You may also switch around
14750 * and do +t"'My +ve experience'" as long as quotes match properly.
14751 * Returns 1 if it finds a token and 0 if no more tokens left.
14752 * pos is updated and token is returned. char *token must point
14753 * to memory of length >= strlen (string).
14754 * string is not changed by gmt_getmodopt.
14755 */
14756
14757 unsigned int i, j, string_len;
14758 bool done = false, in_quote = false;
14759 gmt_M_unused(GMT);
14760
14761 if (string == NULL) return 0; /* Nothing to do */
14762
14763 string_len = (unsigned int)strlen (string);
14764 token[0] = 0; /* Initialize token to NULL in case we are at end */
14765
14766 while (!done) {
14767 /* Wind up *pos to first + sign NOT inside quotes OR escaped */
14768 while (string[*pos] && (in_quote || string[*pos] != '+' || ((*pos) > 0 && string[(*pos)-1] == '\\'))) {
14769 (*pos)++;
14770 if (string[*pos] == '\"' || string[*pos] == '\'') in_quote = !in_quote; /* Watch if we enter quoted text */
14771 }
14772
14773 if (*pos >= string_len || string_len == 0) return 0; /* Got NULL string or no more string left to search */
14774
14775 (*pos)++; /* Go and examine if next char is one of requested modifier options */
14776 done = (strchr (sep, (int)string[*pos]) != NULL); /* true if this is our guy */
14777 if (!done && err) { /* Unrecognized modifier is an error, unless err is NULL */
14778 if (option)
14779 GMT_Report (GMT->parent, GMT_MSG_ERROR, "Option -%c: Unrecognized modifier +%c\n", option, string[*pos]);
14780 else
14781 GMT_Report (GMT->parent, GMT_MSG_ERROR, "Unrecognized modifier +%c\n", string[*pos]);
14782 (*err)++;
14783 }
14784 }
14785
14786 /* Search for next +sep occurrence */
14787 /* Search for next +char occurrence */
14788 i = *pos; j = 0;
14789 //while (string[i] && (in_quote || string[i] != '+' || (i && string[i-1] == '\\') || !strchr (sep, (int)string[i+1]))) {
14790 while (string[i] && (in_quote || string[i] != '+' || (i && string[i-1] == '\\'))) {
14791 if (string[i+1] != '+' || string[i] != '\\') token[j++] = string[i]; /* Do not return the escape char */
14792 i++; /* Go to next character */
14793 if (string[i] == '\"' || string[i] == '\'') in_quote = !in_quote; /* Check when we get past the quoted section */
14794 }
14795 token[j] = 0; /* Add terminating \0 */
14796 if (j > 2 && token[1] == '\"' && token[j-1] == '\"') { /* The whole thing was given in quotes */
14797 memmove (&token[1], &token[2], strlen(token)-3);
14798 token[j-2] = 0;

Callers 15

parseFunction · 0.85
parseFunction · 0.85
parseFunction · 0.85
grdconvert_parse_Z_optFunction · 0.85
parseFunction · 0.85
parseFunction · 0.85
parseFunction · 0.85
parseFunction · 0.85
parseFunction · 0.85
gmt_getinsetFunction · 0.85
gmt_getpanelFunction · 0.85

Calls 1

GMT_ReportFunction · 0.85

Tested by

no test coverage detected