Ice Sheet System Model  4.18
Code documentation
Functions
ToolkitOptions.cpp File Reference

file containing the methods for ToolkitOptions.h More...

#include <string.h>
#include "./ToolkitOptions.h"
#include "../shared/Numerics/types.h"
#include "../shared/Exceptions/exceptions.h"
#include "../shared/MemOps/MemOps.h"

Go to the source code of this file.

Functions

char * TokenValue (char *tokenlist, const char *target)
 

Detailed Description

file containing the methods for ToolkitOptions.h

Definition in file ToolkitOptions.cpp.

Function Documentation

◆ TokenValue()

char* TokenValue ( char *  tokenlist,
const char *  target 
)

Definition at line 41 of file ToolkitOptions.cpp.

41  { /*{{{*/
42 
43  /*output:*/
44  char* value=NULL;
45 
46  /*intermediary: */
47  char *token = NULL;
48  char *tokenlistcopy = NULL;
49 
50  /*First, because tokenizing destroys a string, copy what we have: */
51  if(tokenlist==NULL) _error_("tokenlist not set (may be a mex?)");
52  tokenlistcopy= xNew<char>(strlen(tokenlist)+1);
53  sprintf(tokenlistcopy,"%s",tokenlist);
54 
55  /*Now go through list of tokens, and look for target, return value: */
56  token=strtok(tokenlistcopy, " ");
57  while(token != NULL) {
58 
59  /*Is this token starting with "-", if so, compare to our target: */
60  if (strncmp(token,"-",1)==0){
61  if (strcmp(token+1,target)==0){
62  /*Ok, we found our target. Get next token: */
63  token = strtok(NULL, " ");
64  /*This token could actually be another option start with "-", just be sure: */
65  if (strncmp(token,"-",1)==0){
66  /*ok, we hit another option, which means our target value is "":*/
67  value= xNew<char>(strlen("")+1);
68  sprintf(value,"%s","");
69  continue;
70  }
71  else{
72  /*this token is the value we are looking for, copy: */
73  value= xNew<char>(strlen(token)+1);
74  sprintf(value,"%s",token);
75  }
76  }
77  else{
78  /*we found the wrong target. Go to the next option: */
79  token = strtok(NULL, " ");
80  if (strncmp(token,"-",1)==0){
81  /*this is indeed an option, continue: */
82  continue;
83  }
84  else{
85  /*this is the value of the option, discard it: */
86  }
87  }
88  }
89  else _error_("token list should start with an option, not a value");
90 
91  /*Get new token and continue*/
92  token = strtok(NULL, " ");
93  }
94 
95  /*Clean up and return*/
96  xDelete<char>(tokenlistcopy);
97  return value;
98 }
_error_
#define _error_(StreamArgs)
Definition: exceptions.h:49