Esempio n. 1
0
int
main(int argc, char ** argv)
{
	bool					ok;
	Configuration *			cfg;
	Configuration *			schemaCfg;
	Config2Cpp				util("config2cpp");
	StringVector			namesList;
	StringVector			recipeUserTypes;
	StringVector			wildcardedNamesAndTypes;
	StringVector			recipeIgnoreRules;
	StringVector			unmatchedPatterns;
	StringVector			schema;
	SchemaValidator			sv;
	const char *			scope;
	int						i;
	int						len;
	const char *			overrideSchema[] = {
					"@typedef keyword = enum[\"@optional\", \"@required\"]",
					"user_types = list[string]",
					"wildcarded_names_and_types = table[keyword,keyword, "
										"string,wildcarded-name, string,type]",
					"ignore_rules = list[string]",
					0 // null-terminated array
							};

	ok = util.parseCmdLineArgs(argc, argv);

	cfg       = Configuration::create();
	schemaCfg = Configuration::create();
	if (ok && util.wantSchema()) {
		try {
			cfg->parse(util.cfgFileName());
			cfg->listFullyScopedNames("", "", Configuration::CFG_SCOPE_AND_VARS,
									  true, namesList);
			if (util.schemaOverrideCfg() != 0) {
				schemaCfg->parse(util.schemaOverrideCfg());
				scope = util.schemaOverrideScope();
				sv.parseSchema(overrideSchema);
				sv.validate(schemaCfg, scope, "");
				schemaCfg->lookupList(scope, "user_types", recipeUserTypes);
				schemaCfg->lookupList(scope, "wildcarded_names_and_types",
									  wildcardedNamesAndTypes);
				schemaCfg->lookupList(scope, "ignore_rules", recipeIgnoreRules);
			}
			calculateSchema(cfg, namesList, recipeUserTypes,
							wildcardedNamesAndTypes, recipeIgnoreRules, schema);
			checkForUnmatchedPatterns(cfg, namesList, wildcardedNamesAndTypes,
									  unmatchedPatterns);
		} catch(const ConfigurationException & ex) {
			fprintf(stderr, "%s\n", ex.c_str());
			ok = false;
		}
		len = unmatchedPatterns.length();
		if (len != 0) {
			fprintf(stderr, "%s %s\n",
				"Error: the following patterns in the schema",
				"recipe did not match anything");
			for (i = 0; i < len; i++) {
				fprintf(stderr, "\t'%s'\n", unmatchedPatterns[i]);
			}
			ok = false;
		}
	}

	if (ok) {
		ok = util.generateFiles(schema.c_array(), schema.length());
	}

	cfg->destroy();
	if (ok) {
		return 0;
	} else {
		return 1;
	}
}