// -*- c++ -*- /* * Jakelib2 - General purpose C++ library * Copyright (C) 2001 Florian Wolff (florian@donuz.de) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * $Id: Commandline.jlc,v 1.5 2006-01-12 21:13:22 florian Exp $ */ #include "jakelib2.h" #include "jakelib2/util/Commandline.h" #include "jakelib2/util/ArrayList.h" #include "jakelib2/util/ArrayListIterator.h" #include "jakelib2/util/Hashtable.h" #include "jakelib2/lang/Integer.h" #include "jakelib2/io/File.h" using namespace jakelib::lang; using namespace jakelib::util; using namespace jakelib::io; JAKELIB_IMPLEMENT_CLASS("jakelib.util.Commandline", Commandline, Object) int Commandline::UNDEFINED = Integer::MAX_VALUE; /*****************************************************************************\ * Commandline | *****************************************************************************/ Commandline::Commandline(int argc, char* argv[]) { params = new ArrayList(20); options = new Hashtable(20); filename = new String(argv[0]); File file(argv[0]); pathname = file.getDir(); // pathname = filename; for (int idx = 1; idx < argc; idx++) { params->add(new String(argv[idx])); } } Commandline::~Commandline() { delete params; delete options; } /*****************************************************************************\ * get | *****************************************************************************/ String* Commandline::get(int idx) { if (idx >= params->size()) { return null; } else { return (String*) params->get(idx); } } /*****************************************************************************\ * getExeFile | *****************************************************************************/ String* Commandline::getExeFile() { return filename; } /*****************************************************************************\ * getExePath | *****************************************************************************/ String* Commandline::getExePath() { return pathname; } /*****************************************************************************\ * size | *****************************************************************************/ int Commandline::size() { return params->size(); } /*****************************************************************************\ * iterator | *****************************************************************************/ ArrayListIterator* Commandline::iterator() { return new ArrayListIterator(params); } /*****************************************************************************\ * allowOption | *****************************************************************************/ void Commandline::allowOption(String* key, String* shortkey, jboolean expectValue) { String *key1, *key2; String *shortkey1, *shortkey2; if (key != null) { key1 = `"--"` .. key; key2 = `"--"` .. key .. '='; } if (shortkey != null) { shortkey1 = `"-"` .. shortkey; } for (int idx = 0; idx < params->size(); idx++) { String* str = (String*) params->get(idx); if (expectValue) { if (key != null && str->startsWith(key2)) { options->put(key, str->substring(key2->length())); params->remove(idx); return; } else if (shortkey != null && str->equals(shortkey1)) { if (idx < params->size() -1) { options->put(key, get(idx + 1)); params->remove(idx); params->remove(idx); return; } else { options->put(key, `""`); params->remove(idx); return; } } else if (shortkey != null && str->startsWith(shortkey1)) { options->put(key, str->substring(shortkey1->length())); params->remove(idx); return; } } else { if ( (key != null && str->equals(key1)) || (shortkey != null && str->equals(shortkey1))) { options->put(key, `""`); params->remove(idx); return; } } } } /*****************************************************************************\ * optionSet | *****************************************************************************/ jboolean Commandline::optionSet(String* key) { return (options->get(key) != null); } /*****************************************************************************\ * getAsString | *****************************************************************************/ String* Commandline::getAsString(String* key, String* def) { String* val = (String*) options->get(key); if (val == null) return def; else return val; } /*****************************************************************************\ * getAsInt | *****************************************************************************/ int Commandline::getAsInt(String* key, int def) { String* val = (String*) options->get(key); if (val == null) return def; else return Integer::parseInt(val); }