/* parse_argument.cpp */ /* remap Copyright (C) 2006 Fabrice Ducos, fabrice.ducos@icare.univ-lille1.fr This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include #include #include "common.h" // DATA_TYPE #include "tokenize.h" #include "parse_argument.h" // APPNAME, STRING_MAXLEN, entry_type int parse_argument(int iarg, char *argv[], grid_type *grid) { using namespace std; // this pattern matches strings like : dataset[n]@file (square brackets are optional, n is a number) const char *pattern = "^([^[@/]+)(\\[([0-9]+)\\])?(/[^@]+)?@(.*)$"; size_t ntokens; char **tokens = NULL; etok_type err; const char *argument = argv[iarg]; err = tokenize(pattern, argument, &ntokens, &tokens); if (err != ETOK_SUCCESS) { switch (err) { case ETOK_REGCOMP : { std::cerr << APPNAME << ": tokenize: regcomp failed" << std::endl; break; } case ETOK_MEMORY : { std::cerr << APPNAME << ": tokenize: calloc failed" << std::endl; break; } case ETOK_NOMATCH : { std::cerr << APPNAME << ": " << argument << " (see usage for a valid syntax)" << std::endl; break; } case ETOK_ESPACE : { std::cerr << APPNAME << ": tokenize: not enough memory for regexec" << std::endl; break; } default : { std::cerr << APPNAME << ": internal error: unexpected return code from tokenize" << std::endl; abort(); break; } } // switch return -1; } assert(ntokens == 6); /* tokens[0]:"dataset[ichannel]@file" * tokens[1]:"dataset" * tokens[2]:"[ichannel]" * tokens[3]:"ichannel" * tokens[4]:"/output_dataset" * tokens[5]:"file" */ assert(strncmp(argument, tokens[0], STRING_MAXLEN) == 0); assert(tokens[1] && *tokens[1]); strncpy(grid->input_dataset, tokens[1], STRING_MAXLEN); if (tokens[4] && *tokens[4]) { strncpy(grid->output_dataset, tokens[4] + sizeof(char) /* jumps over first '/' */, STRING_MAXLEN); } else { strncpy(grid->output_dataset, grid->input_dataset, STRING_MAXLEN); if (tokens[2] && *tokens[2]) { strcat(grid->output_dataset, tokens[2]); } } if (tokens[5] && *tokens[5]) { strncpy(grid->file, tokens[5], STRING_MAXLEN); } else { std::cerr << APPNAME << ": " << argument << " (see usage for a valid syntax): name of the input file is missing" << std::endl; exit (EXIT_FAILURE); } if (tokens[3] && *tokens[3]) { grid->ichannel = atoi(tokens[3]); } else { grid->ichannel = 0; } free_tokens(ntokens, tokens); return 0; }