4 * Gets an index of a string, where the index must be separated by
5 * a character specified in tokens.
6 * Returns -1, when an error occurs.
9 * getIndex("name_1_2", "_", 0) will return 1.
10 * getIndex("name_1_2", "_", 1) will return 2.
12 * @param string string to parse
13 * @param tokens delimiter of indices
14 * @param indexNumber position of index (starting at 0)
16 int getIndex(const char* string, char* tokens, int indexNumber) {
21 string_copy = (char*) malloc(sizeof(char) * (strlen(string) + 1));
23 fprintf(stderr, "getIndex(): could not allocate memory.\n");
27 strcpy(string_copy, string);
29 token_pointer = strtok(string_copy, tokens);
31 token_pointer = strtok(NULL, tokens);
33 } while (index <= indexNumber && token_pointer != 0);
36 index = atoi(token_pointer);