# getoptdata.cpp

Go to the documentation of this file.

    00001 /* $Id: getoptdata.cpp 23245 2011-11-17 21:18:24Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD 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, version 2.
    00006  * OpenTTD 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.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "../stdafx.h"
    00013 #include "getoptdata.h"
    00014
    00022 int GetOptData::GetOpt()
    00023 {
    00024   const OptionData *odata;
    00025
    00026   char *s = this->cont;
    00027   if (s == NULL) {
    00028     if (this->numleft == 0) return -1; // No arguments left -> finished.
    00029
    00030     s = this->argv[0];
    00031     if (*s != '-') return -1; // No leading '-' -> not an option -> finished.
    00032
    00033     this->argv++;
    00034     this->numleft--;
    00035
    00036     /* Is it a long option? */
    00037     for (odata = this->options; odata->flags != ODF_END; odata++) {
    00038       if (odata->longname != NULL && !strcmp(odata->longname, s)) { // Long options always use the entire argument.
    00039         this->cont = NULL;
    00040         goto set_optval;
    00041       }
    00042     }
    00043
    00044     s++; // Skip leading '-'.
    00045   }
    00046
    00047   /* Is it a short option? */
    00048   for (odata = this->options; odata->flags != ODF_END; odata++) {
    00049     if (odata->shortname != '\0' && *s == odata->shortname) {
    00050       this->cont = (s[1] != '\0') ? s + 1 : NULL;
    00051
    00052 set_optval: // Handle option value of *odata .
    00053       this->opt = NULL;
    00054       switch (odata->flags) {
    00055         case ODF_NO_VALUE:
    00056           return odata->id;
    00057
    00058         case ODF_HAS_VALUE:
    00059         case ODF_OPTIONAL_VALUE:
    00060           if (this->cont != NULL) { // Remainder of the argument is the option value.
    00061             this->opt = this->cont;
    00062             this->cont = NULL;
    00063             return odata->id;
    00064           }
    00065           /* No more arguments, either return an error or a value-less option. */
    00066           if (this->numleft == 0) return (odata->flags == ODF_HAS_VALUE) ? -2 : odata->id;
    00067
    00068           /* Next argument looks like another option, let's not return it as option value. */
    00069           if (odata->flags == ODF_OPTIONAL_VALUE && this->argv[0][0] == '-') return odata->id;
    00070
    00071           this->opt = this->argv[0]; // Next argument is the option value.
    00072           this->argv++;
    00073           this->numleft--;
    00074           return odata->id;
    00075
    00076         default: NOT_REACHED();
    00077       }
    00078     }
    00079   }
    00080
    00081   return -2; // No other ways to interpret the text -> error.
    00082 }
    00083