<commit_before>/*
 * dbgen.c
 *
 *  Created on: Sep 17, 2012
 *      Author: sander
 */

#include "hyve.h"
#include "db_generator.h"
#include "db_loader.h"
#include "db_arg.h"

static char* name = NULL;
static char* language = NULL;
static char* prefix = NULL;
static char* lang = NULL;
static db_ll generators = NULL;
static db_ll scopes = NULL;
static db_ll includes = NULL;
static db_ll attributes = NULL;

int db_arg_attributes(char* arg, int argc, char* argv[]) {
	if (argc) {
	    if (!attributes) {
	        attributes = db_llNew();
	    }
	    db_llInsert(attributes, argv[1]);
	} else {
		db_error("missing argument for '%s'.", arg);
	}
	return 1;
}

int db_arg_name(char* arg, int argc, char* argv[]) {
	if (argc) {
		name = argv[1];
	} else {
		db_error("missing argument for '%s'.", arg);
	}
	return 1;
}

int db_arg_prefix(char* arg, int argc, char* argv[]) {
	if (argc) {
		prefix = argv[1];
	} else {
		db_error("missing argument for '%s'.", arg);
	}
	return 1;
}

int db_arg_generator(char* arg, int argc, char* argv[]) {
	if (argc) {
		if (!generators) {
			generators = db_llNew();
		}
		db_llAppend(generators, argv[1]);
	} else {
		db_error("missing argument for '%s'.", arg);
	}
	return 1;
}

int db_arg_scope(char* arg, int argc, char* argv[]) {
	if (argc) {
		if (!scopes) {
			scopes = db_llNew();
		}
		db_llInsert(scopes, argv[1]);
	} else {
		db_error("missing argument for '%s'.", arg);
	}
	return 1;
}

int db_arg_include(char* arg, int argc, char* argv[]) {
	if (argc) {
		if (!includes) {
			includes = db_llNew();
		}
		db_llAppend(includes, argv[0]);
	} else {
		db_error("missing argument for '%s'.", arg);
	}
	return 1;
}

int db_arg_language(char* arg, int argc, char* argv[]) {
    if (argc == 2) {
        if (!generators) {
            generators = db_llNew();
        }
        if (!strcmp(argv[1], "c")) {
            if (!attributes) {
                attributes = db_llNew();
            }
            db_llAppend(generators, "c_project");
            db_llAppend(generators, "c_type");
            db_llAppend(generators, "c_interface");
            db_llAppend(generators, "c_load");
            db_llAppend(generators, "c_api");
            db_llAppend(attributes, "c=src");
            db_llAppend(attributes, "h=include");

            lang = "c";
        } else if (!strcmp(argv[1], "cpp")) {
            db_llAppend(generators, "cpp_type");
            db_llAppend(generators, "cpp_class");
            db_llAppend(generators, "cpp_load");
        } else {
        	db_error("unknown language '%s'.", argv[1]);
        	goto error;
        }
    } else {
        db_error("invalid number of argument for '%s'.", arg);
        goto error;
    }
    return 1;
error:
	return 0;
}


/* Parse arguments */
static int db_parseArguments(int argc, char* argv[]) {
	/* Specifiy arguments and callbacks */
	db_argSet("attr", db_arg_attributes, 0, -1);
	db_argSet("name", db_arg_name, 0, 1);
	db_argSet("prefix", db_arg_prefix, 0, 1);
	db_argSet("g", db_arg_generator, 0, -1);
	db_argSet("scope", db_arg_scope, 1, -1);
	db_argSet("lang", db_arg_language, 0, 1);
	db_argSet(NULL, db_arg_include, 0, -1);

	/* Parse commandline */
	if (db_argParse(argc, argv)) {
		return -1;
	}
	return 0;
}

void printUsage(void) {
	printf("Usage:\n");
	printf("   dbgen [file 0..n] --scope [scope] --prefix [prefix] --lang [language] -g [generator 1..n] -attr [key=value]\n");
	printf("     file:     Any file that is loadable by Hyve (optional).\n");
	printf("     --scope:  Provide the scope for which code will be generated.\n");
	printf("     --prefix: Provide a prefix for the generated code (optional).\n");
	printf("     --lang:   Provide the language. Possible choices are c and cpp (optional).\n");
	printf("               Note that --lang is a shortcut to common generators and attributes for the chosen language.\n");
	printf("     -g:       Any code generator located in generator/bin (optional).\n");
	printf("     --attr:   Specify attributes that will be passed on to the generator (optional).\n");
	printf("               Attributes are specified using a key=value syntax.\n");
	printf("\n");
	printf("Examples:\n");
	printf("   dbgen Foo.hyve --scope Foo --lang c\n");
	printf("   dbgen Foo.hyve --scope Foo -g c_type -g c_interface -g c_load -g c_api -attr c=src -attr h=include\n");
	printf("   dbgen Foo.hyve --scope Foo --lang cpp\n");
	printf("   dbgen Foo.hyve --scope Foo --prefix Bar --lang cpp (replaces 'Foo' by 'Bar' in generated code)\n");<commit_msg>#84 Typo in usage of dbgen<commit_after>/*
 * dbgen.c
 *
 *  Created on: Sep 17, 2012
 *      Author: sander
 */

#include "hyve.h"
#include "db_generator.h"
#include "db_loader.h"
#include "db_arg.h"

static char* name = NULL;
static char* language = NULL;
static char* prefix = NULL;
static char* lang = NULL;
static db_ll generators = NULL;
static db_ll scopes = NULL;
static db_ll includes = NULL;
static db_ll attributes = NULL;

int db_arg_attributes(char* arg, int argc, char* argv[]) {
	if (argc) {
	    if (!attributes) {
	        attributes = db_llNew();
	    }
	    db_llInsert(attributes, argv[1]);
	} else {
		db_error("missing argument for '%s'.", arg);
	}
	return 1;
}

int db_arg_name(char* arg, int argc, char* argv[]) {
	if (argc) {
		name = argv[1];
	} else {
		db_error("missing argument for '%s'.", arg);
	}
	return 1;
}

int db_arg_prefix(char* arg, int argc, char* argv[]) {
	if (argc) {
		prefix = argv[1];
	} else {
		db_error("missing argument for '%s'.", arg);
	}
	return 1;
}

int db_arg_generator(char* arg, int argc, char* argv[]) {
	if (argc) {
		if (!generators) {
			generators = db_llNew();
		}
		db_llAppend(generators, argv[1]);
	} else {
		db_error("missing argument for '%s'.", arg);
	}
	return 1;
}

int db_arg_scope(char* arg, int argc, char* argv[]) {
	if (argc) {
		if (!scopes) {
			scopes = db_llNew();
		}
		db_llInsert(scopes, argv[1]);
	} else {
		db_error("missing argument for '%s'.", arg);
	}
	return 1;
}

int db_arg_include(char* arg, int argc, char* argv[]) {
	if (argc) {
		if (!includes) {
			includes = db_llNew();
		}
		db_llAppend(includes, argv[0]);
	} else {
		db_error("missing argument for '%s'.", arg);
	}
	return 1;
}

int db_arg_language(char* arg, int argc, char* argv[]) {
    if (argc == 2) {
        if (!generators) {
            generators = db_llNew();
        }
        if (!strcmp(argv[1], "c")) {
            if (!attributes) {
                attributes = db_llNew();
            }
            db_llAppend(generators, "c_project");
            db_llAppend(generators, "c_type");
            db_llAppend(generators, "c_interface");
            db_llAppend(generators, "c_load");
            db_llAppend(generators, "c_api");
            db_llAppend(attributes, "c=src");
            db_llAppend(attributes, "h=include");

            lang = "c";
        } else if (!strcmp(argv[1], "cpp")) {
            db_llAppend(generators, "cpp_type");
            db_llAppend(generators, "cpp_class");
            db_llAppend(generators, "cpp_load");
        } else {
        	db_error("unknown language '%s'.", argv[1]);
        	goto error;
        }
    } else {
        db_error("invalid number of argument for '%s'.", arg);
        goto error;
    }
    return 1;
error:
	return 0;
}


/* Parse arguments */
static int db_parseArguments(int argc, char* argv[]) {
	/* Specifiy arguments and callbacks */
	db_argSet("attr", db_arg_attributes, 0, -1);
	db_argSet("name", db_arg_name, 0, 1);
	db_argSet("prefix", db_arg_prefix, 0, 1);
	db_argSet("g", db_arg_generator, 0, -1);
	db_argSet("scope", db_arg_scope, 1, -1);
	db_argSet("lang", db_arg_language, 0, 1);
	db_argSet(NULL, db_arg_include, 0, -1);

	/* Parse commandline */
	if (db_argParse(argc, argv)) {
		return -1;
	}
	return 0;
}

void printUsage(void) {
	printf("Usage:\n");
	printf("   dbgen [file 0..n] --scope [scope] --prefix [prefix] --lang [language] -g [generator 1..n] -attr [key=value]\n");
	printf("     file:     Any file that is loadable by Hyve (optional).\n");
	printf("     --scope:  Provide the scope for which code will be generated.\n");
	printf("     --prefix: Provide a prefix for the generated code (optional).\n");
	printf("     --lang:   Provide the language. Possible choices are c and cpp (optional).\n");
	printf("               Note that --lang is a shortcut to common generators and attributes for the chosen language.\n");
	printf("     -g:       Any code generator located in generator/bin (optional).\n");
	printf("     --attr:   Specify attributes that will be passed on to the generator (optional).\n");
	printf("               Attributes are specified using a key=value syntax.\n");
	printf("\n");
	printf("Examples:\n");
	printf("   dbgen Foo.hyve --scope Foo --lang c\n");
	printf("   dbgen Foo.hyve --scope Foo -g c_type -g c_interface -g c_load -g c_api --attr c=src --attr h=include\n");
	printf("   dbgen Foo.hyve --scope Foo --lang cpp\n");
	printf("   dbgen Foo.hyve --scope Foo --prefix Bar --lang cpp (replaces 'Foo' by 'Bar' in generated code)\n");
{'fraction_non_alphanumeric': 0.16036794857586167, 'fraction_numerical': 0.009531198049429236, 'mean_word_length': 2.899740708729473, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '321612', 'n_tokens_mistral': 3296, 'n_tokens_neox': 3152, 'n_words': 910}