CmdOption.cpp 4.0 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
/***************************************************************************
 * 
 * Copyright (c) 2008 Baidu.com, Inc. All Rights Reserved
 * $Id: CmdOption.cpp,v 1.6 2009/11/10 04:50:30 scmpf Exp $ 
 * 
 **************************************************************************/



/**
 * @file CmdOption.cpp
 * @author yingxiang(com@baidu.com)
 * @date 2008/12/29 21:50:42
 * @version $Revision: 1.6 $ 
 * @brief 
 *  
 **/

#include "CmdOption.h"
#include "ConfigUnit.h"
#include "utils/cc_utils.h"
#include <string.h>

namespace comcfg{
	CmdOption :: CmdOption(){
	}

	int CmdOption :: setOptString(const char * p){
		int ret = 0;
		const char *prev = NULL;
		memset(ch, 0, sizeof(ch));
		while(*p != '\0'){
			if(*p == ':'){
				if(prev == NULL || ch[(unsigned char)(*prev)] == CMD_OPT2){
					ret = -1;
					break;
				}
				else{
					++ ch[(unsigned char)(*prev)];
				}
			}
			else{
				if(ch[(unsigned char)(*p)]){
					ret = -1;
					break;
				}
				ch[(unsigned char)(*p)] = CMD_OPT0;
			}
			prev = p;
			++p;
		}
		return ret;
	}

	int CmdOption :: init(int argc, char * const * argv, const char * optstring){
		if(argv == NULL || optstring == NULL){
			LOG(WARNING) << "Null pointer in CmdOption::init.....";
			return -1;
		}
		if(setOptString(optstring) != 0){
			LOG(WARNING) << "optstring error.";
			return -1;
		}
		int ret = 0;
		//
		try{
			for(int i = argc - 1; i >= 0; --i){
				if(argv[i][0] == '-'){
					str_t key, value;
					//option
					if(argv[i][1] == '-'){
						//long option
						key = argv[i] + 2;
						size_t sz;// = key.find('=', 0);
						for(sz = 0; sz < key.size(); ++sz){
							if(key[sz] == '='){
								break;
							}
						}
						if(sz != key.size()){
							value = key.substr(sz+1);
							key = key.substr(0, sz);
							LOG(INFO) << "[CmdOption init - long opt] key=" << key.c_str() << " value=" << value.c_str();
						}
						if( push(key, new ConfigUnit(key, value, NULL, this)) ){
							ret = -1;
						}
					}

					else if(ch[(unsigned char)(argv[i][1])] == CMD_OPT0){
						//opt 0
						key = "";
						key.push_back(argv[i][1]);
						if( push(key, new ConfigUnit(key, value, NULL, this)) ){
							ret = -1;
						}
					}

					else if(ch[(unsigned char)(argv[i][1])] == CMD_OPT1){
						//opt 1
						key = "";
						key.push_back(argv[i][1]);
						value = argv[i] + 2;
						if(value.size() == 0){
							if(arg.size() == 0){
								LOG(WARNING) << "Option [-" << argv[i][1] << ":] without any argument.";
								ret = -1;
							}
							else{
								value = arg[arg.size() - 1];
								arg.pop_back();
							}
						}
						if( push(key, new ConfigUnit(key, value, NULL, this)) ){
							ret = -1;
						}
					}

					else if(ch[(unsigned char)(argv[i][1])] == CMD_OPT2){
						//opt 2
						key = ""; 
						key.push_back(argv[i][1]);
						value = argv[i] + 2;
						if(value.size() == 0){
							LOG(WARNING) << "Option [-" << argv[i][1] << "::] without any argument.";
							ret = -1;
						}
						if( push(key, new ConfigUnit(key, value, NULL, this)) ){
							ret = -1;
						}
					}
					else{
						LOG(WARNING) << "Option [-" << argv[i][1] << "] unknown.";
						ret = -1;
					}
				}
				else{
					arg.push_back(str_t(argv[i]));
				}
			}
		}
		catch(...){
			LOG(WARNING) << "Option error..";
			ret = -1;
		}
		return ret;
	}

	bool CmdOption :: hasOption(const char * s) const {
		return (*this)[s].selfType() != CONFIG_ERROR_TYPE;
	}
	size_t CmdOption :: argSize() const {
		return arg.size();
	}
	const char * CmdOption :: getArg(int n) const {
		if(n >= (int)arg.size()){
			return NULL;
		}
		//
		return arg[(int)arg.size() - n - 1].c_str();
	}
	int CmdOption :: registHook(const char * option, hook_t hook){
		pair_t p;
		p.first = str_t(option);
		p.second = hook;
		hkmap.insert(p);
		return 0;
	}
	int CmdOption :: runHook(){
		itr_t itr;
		for(itr = fieldMap.begin(); itr != fieldMap.end(); ++itr){
			hk_pair_t p = hkmap.equal_range(itr->first);
			for(hkitr_t hkitr = p.first; hkitr != p.second; ++hkitr){
				(hkitr->second)(this);
			}
		}
		return 0;
	}
}

















/* vim: set ts=4 sw=4 sts=4 tw=100 */