Exemple #1
0
static void
process_cmdline(int argc, char **argv)
{
    int i, j, nextarg;
    char *optArg;
    char *optNext;
    PFILE dummy;		/* starts linked list of filenames */
    PFILE *tail = &dummy;
    size_t length;

    if (argc <= 1)
	usage();

    for (i = 1; i < argc && argv[i][0] == '-'; i = nextarg) {
	if (argv[i][1] == 0)	/* -  alone */
	{
	    if (!pfile_name)
		no_program();
	    break;		/* the for loop */
	}
	/* safe to look at argv[i][2] */

	/*
	 * Check for "long" options and decide how to handle them.
	 */
	if (strlen(argv[i]) > 2 && !strncmp(argv[i], "--", (size_t) 2)) {
	    if (!allow_long_options(argv[i])) {
		nextarg = i + 1;
		continue;
	    }
	}

	if (argv[i][2] == 0) {
	    if (i == argc - 1 && argv[i][1] != '-') {
		if (strchr("WFvf", argv[i][1])) {
		    errmsg(0, "option %s lacks argument", argv[i]);
		    mawk_exit(2);
		}
		bad_option(argv[i]);
	    }

	    optArg = argv[i + 1];
	    nextarg = i + 2;
	} else {		/* argument glued to option */
	    optArg = &argv[i][2];
	    nextarg = i + 1;
	}

	switch (argv[i][1]) {

	case 'W':
	    for (j = 0; j < (int) strlen(optArg); j = (int) (optNext - optArg)) {
		switch (parse_w_opt(optArg + j, &optNext)) {
		case W_VERSION:
		    print_version();
		    break;
#if USE_BINMODE
		case W_BINMODE:
		    if (haveValue(optNext)) {
			set_binmode(atoi(optNext + 1));
			optNext = skipValue(optNext);
		    } else {
			errmsg(0, "missing value for -W binmode");
			mawk_exit(2);
		    }
		    break;
#endif
		case W_DUMP:
		    dump_code_flag = 1;
		    break;

		case W_EXEC:
		    if (pfile_name) {
			errmsg(0, "-W exec is incompatible with -f");
			mawk_exit(2);
		    } else if (nextarg == argc) {
			no_program();
		    }
		    if (haveValue(optNext)) {
			pfile_name = optNext + 1;
			i = nextarg;
		    } else {
			pfile_name = argv[nextarg];
			i = nextarg + 1;
		    }
		    goto no_more_opts;

		case W_INTERACTIVE:
		    interactive_flag = 1;
		    setbuf(stdout, (char *) 0);
		    break;

		case W_POSIX_SPACE:
		    posix_space_flag = 1;
		    break;

		case W_RANDOM:
		    if (haveValue(optNext)) {
			int x = atoi(optNext + 1);
			CELL c[2];

			memset(c, 0, sizeof(c));
			c[1].type = C_DOUBLE;
			c[1].dval = (double) x;
			/* c[1] is input, c[0] is output */
			bi_srand(c + 1);
			optNext = skipValue(optNext);
		    } else {
			errmsg(0, "missing value for -W random");
			mawk_exit(2);
		    }
		    break;

		case W_SPRINTF:
		    if (haveValue(optNext)) {
			int x = atoi(optNext + 1);

			if (x > (int) sizeof(string_buff)) {
			    if (sprintf_buff != string_buff &&
				sprintf_buff != 0) {
				zfree(sprintf_buff,
				      (size_t) (sprintf_limit - sprintf_buff));
			    }
			    sprintf_buff = (char *) zmalloc((size_t) x);
			    sprintf_limit = sprintf_buff + x;
			}
			optNext = skipValue(optNext);
		    } else {
			errmsg(0, "missing value for -W sprintf");
			mawk_exit(2);
		    }
		    break;

		case W_HELP:
		    /* FALLTHRU */
		case W_USAGE:
		    usage();
		    /* NOTREACHED */
		    break;
		case W_UNKNOWN:
		    errmsg(0, "vacuous option: -W %s", optArg + j);
		    break;
		}
		while (*optNext == '=') {
		    errmsg(0, "unexpected option value %s", optArg + j);
		    optNext = skipValue(optNext);
		}
	    }
	    break;

	case 'v':
	    if (!is_cmdline_assign(optArg)) {
		errmsg(0, "improper assignment: -v %s", optArg);
		mawk_exit(2);
	    }
	    break;

	case 'F':

	    rm_escape(optArg, &length);		/* recognize escape sequences */
	    cell_destroy(FS);
	    FS->type = C_STRING;
	    FS->ptr = (PTR) new_STRING1(optArg, length);
	    cast_for_split(cellcpy(&fs_shadow, FS));
	    break;

	case '-':
	    if (argv[i][2] != 0) {
		bad_option(argv[i]);
	    }
	    i++;
	    goto no_more_opts;

	case 'f':
	    /* first file goes in pfile_name ; any more go
	       on a list */
	    if (!pfile_name)
		pfile_name = optArg;
	    else {
		tail = tail->link = ZMALLOC(PFILE);
		tail->fname = optArg;
	    }
	    break;

	default:
	    bad_option(argv[i]);
	}
    }

  no_more_opts:

    tail->link = (PFILE *) 0;
    pfile_list = dummy.link;

    if (pfile_name) {
	set_ARGV(argc, argv, i);
	scan_init((char *) 0);
    } else {			/* program on command line */
	if (i == argc)
	    no_program();
	set_ARGV(argc, argv, i + 1);

#if  defined(MSDOS) && ! HAVE_REARGV	/* reversed quotes */
	{
	    char *p;

	    for (p = argv[i]; *p; p++)
		if (*p == '\'')
		    *p = '\"';
	}
#endif
	scan_init(argv[i]);
/* #endif  */
    }
}
Exemple #2
0
bool StringsMap::parseFile(string relativePath)
{
	// 看是否已经解析过了
	if (s_paresedFile.find(relativePath) != s_paresedFile.end())
	{
		CCLOG("Waring : %s --> file[%s] is pared over before or is in parsing now ,do you include it again???",__FUNCTION__,relativePath.c_str());
		return true;
	}
	s_paresedFile.insert(pair<string, int>(relativePath,0));


	string path = string(ROOT_PATH)+relativePath;
	CCLOG("%s parsing file [%s] ....",__FUNCTION__,path.c_str());


	////
	//parse file
	Document root;
	XReadJson *json = XReadJson::getIntanse();
	json->getJsonRootValue(path,root);

	if (!root.IsObject())
	{
		return false;
	}
	const rapidjson::Value &arrayValue = json->getSubValue_json(root,"strings");
	int size = arrayValue.Size();
	for (int i = 0 ; i < size ; i++)
	{
		const rapidjson::Value &onePair = json->getSubValue_json(arrayValue,i);

		// 校验
		if(!json->isHave(onePair, LABLE_KEY) && json->isHave(onePair, LABLE_VALUE))
		{
			CCLOG("Error : %s there is no %s for %s [%s]",__FUNCTION__,LABLE_KEY,LABLE_VALUE,json->getString_json(onePair, LABLE_VALUE).c_str());
			CCASSERT(false,"key 和 value 没有配对");
			continue;
		}
		else if(json->isHave(onePair, LABLE_KEY) && ! json->isHave(onePair, LABLE_VALUE))
		{
			CCLOG("Error : %s there is no %s for %s [%s]",__FUNCTION__,LABLE_VALUE,LABLE_KEY,json->getString_json(onePair, LABLE_KEY).c_str());
			CCASSERT(false,"key 和 value 没有配对");
			continue;
		}

		//有include
		if (json->isHave(onePair, LABLE_INCLUDE))
		{
			string file = json->getString_json(onePair, LABLE_INCLUDE);
			if (!parseFile(file))
			{
				CCLOG("Error : %s parse file %s faild",__FUNCTION__,file.c_str());
				return false;
			}
		}
		// 解析键值对
		if (json->isHave(onePair, LABLE_KEY) && json->isHave(onePair, LABLE_VALUE))
		{
			string _key = json->getString_json(onePair, LABLE_KEY);
			string _value = json->getString_json(onePair, LABLE_VALUE);

			if (haveValue(_key))
			{
				CCLOG("Waring : %s --> the key [%s] in file[%s] is already used before,we will ignore this key and value[%s]!",__FUNCTION__,_key.c_str(),path.c_str(),_value.c_str());
				continue;
			}
			s_dataMap.insert(pair<string, string>(_key,_value));
		}
	}

	return true;
}