Example #1
0
int main(int argc, char const *argv[]) {

	// default to a table of 1,000 elements.
	size_t size = 1000;

	if (argc > 1) {
		size = atoi(argv[1]);
		if (!size) {
			// couldn't convert argv[1] to a number
			fprintf(stderr, USAGE);
			exit(EXIT_FAILURE);
		}
	}

	// create a new hash table
	Hash *h = hashCreate(size);

	if (!h) {
		// failed to create hash table
		fprintf(stderr, "failed to create hash table\n");
		exit(EXIT_FAILURE);
	} else {
		printf("Successfully created a hash table of size %zu.\n",size);
		FILE *opt;
		if ((opt = fopen(OPTIONS_FILE,"r"))) {
			char c;
			while((c = fgetc(opt)) != EOF) {
				putchar(c);
			}
			fclose(opt);
		} else {
			fprintf(stderr, "failed to open %s\n", OPTIONS_FILE);
			exit(EXIT_FAILURE);
		}
	}

	// create some variables
	char *command, *key, *obj, *res;
	
	bool hasSet,hasGet,onOwn;
	hasSet = hasGet = onOwn = false;

	bool keepGoing = true;

	while(keepGoing) {
		// initialize the variables and print the prompt
		command = key = obj = res = NULL;

		// encourage the user to try out the different commands
		if (!hasSet) {
			printf("%% (type 'set') ");
		} else if (!hasGet) {
			printf("%% (type 'get') ");
		} else if (!onOwn) {
			onOwn = true;
			printf("You're on your own now... available commands: set, get, delete, load\n");
			printf("%% ");
		} else {
			printf("%% ");
		}

		// read the command
		command = readString();

		// process a set command
		if (!strcasecmp(command,"set")) {
			hasSet = true;
			printf("\tkey: ");
			key = readString();

			printf("\tvalue: ");
			obj = readString();

			printf("\tresult: ");
			printf(hashSet(h,key,obj) ? "SUCCESS\n" : "FAILURE\n");

		// process a get command
		} else if (!strcasecmp(command,"get")) {
			hasGet = true;
			printf("\tkey: ");
			key = readString();
			if ((res = (char *) hashGet(h,key))){
				printf("\tresult: %s\n", res);
			} else {
				printf("\tresult: NULL\n");
			}

		// process a delete command
		} else if (!strcasecmp(command,"delete")) {
			printf("\tkey: ");
			key = readString();
			if ((res = (char *) hashDelete(h,key))){
				printf("\tresult: %s\n", res);
				free(res);
			} else {
				printf("\tresult: NULL\n");
			}

		// process a load command
		} else if (!strcasecmp(command,"load")) {
			printf("\tresult: %f\n", hashLoad(h));

		// let the user exit
		} else if (!strcasecmp(command,"exit") || !strcasecmp(command,"quit")) {
			keepGoing = false;			

		// invalid command
		} else if (strlen(command) > 0) {
			printf("invalid command\n");
		} 

		// free the command and key, as necessary
		if (command) { free(command); }
		if (key) { free(key); }

	}

	hashDestroy(h,destroy);
	return EXIT_SUCCESS;
}
Example #2
0
void MainWindow::createActions() {

    _openiTunesAction = new QAction(QIcon(":/images/iTunes.png"),
                                    tr("Open i&Tunes Library..."), this);
    _openiTunesAction->setShortcut(tr("Ctrl+T"));
    _openiTunesAction->setStatusTip(tr("Open iTunes Library file"));
    connect(_openiTunesAction, SIGNAL(triggered()), this, SLOT(openiTunesLibrary()));

    _openCollectionAction = new QAction(QIcon(":/images/openCollection.png"),
                                        tr("Open Collection File"), this);
    _openCollectionAction->setShortcut(tr("Ctrl+T"));
    _openCollectionAction->setStatusTip(tr("Open Collection file"));
    connect(_openCollectionAction, SIGNAL(triggered()), this, SLOT(openCollectionFile()));

    _exitAction = new QAction(tr("E&xit"), this);
    _exitAction->setShortcut(tr("Ctrl+Q"));
    connect(_exitAction, SIGNAL(triggered()), this, SLOT(close()));

    _coutAction = new QAction(tr("&Cout Library"), this);
    _coutAction->setShortcut(tr("Ctrl+E"));
    connect(_coutAction, SIGNAL(triggered()), this, SLOT(display()));

    _predictAction = new QAction(QIcon(":/images/predict.png"),
                                 tr("&Predict"), this);
    _predictAction->setShortcut(tr("Ctrl+3"));
    _predictAction->setStatusTip(tr("Predict the the placement of the prediction tracks"));
    connect(_predictAction, SIGNAL(triggered()), _display, SLOT(predict()));

    _extractAction = new QAction(QIcon(":/images/extract.png"),
                                 tr("&Extract"), this);
    _extractAction->setShortcut(tr("Ctrl+1"));
    _extractAction->setStatusTip(tr("Extract features from the defined training tracks"));
    connect(_extractAction, SIGNAL(triggered()), _display, SLOT(extract()));

    _trainingAction = new QAction(QIcon(":/images/train.png"),
                                  tr("&Train"), this);
    _trainingAction->setShortcut(tr("Ctrl+2"));
    _trainingAction->setStatusTip(tr("Train the grid using the defined training tracks"));
    connect(_trainingAction, SIGNAL(triggered()), _display, SLOT(train()));

    _initAction = new QAction(QIcon(":/images/init.png"), tr("&Initlize"), this);
    connect(_initAction, SIGNAL(triggered()), _display, SLOT(init()));

    _aboutAction = new QAction(tr("&About"), this);
    connect(_aboutAction, SIGNAL(triggered()), this, SLOT(about()));

    _saveGridAction = new QAction(tr("&Save Grid"), this);
    connect(_saveGridAction, SIGNAL(triggered()), this, SLOT(saveCurrentGrid()) );

    _loadGridAction = new QAction(tr("&Load Saved Grid"),this);
    connect(_loadGridAction, SIGNAL(triggered()), this, SLOT(openSavedGrid()));
    _playModeAction = new QAction(tr("&Continuous"), this);
    connect(_playModeAction, SIGNAL(triggered()), this, SLOT(changedPlayMode()));

    _cancelAction = new QAction(tr("&Cancel Action"), this);
    connect(_cancelAction, SIGNAL(triggered()), this, SLOT(cancelButton()));

    _normHashAction = new QAction(tr("Open Saved Hash"), this);
    connect(_normHashAction, SIGNAL(triggered()), _display, SLOT(hashLoad()));

    _fullScreenAction = new QAction (tr("&Full Screen Mouse Mode"), this);
    connect(_fullScreenAction, SIGNAL(triggered()), _display, SLOT(fullScreenMouse()));

    _colourMapMode = new QAction (tr("&Colour Mapping Mode"),this);
    connect(_colourMapMode, SIGNAL(triggered()), _display, SLOT(colourMapMode()));

    _resetButtonAction = new QAction (tr("&Reset"), this);
    connect (_resetButtonAction, SIGNAL(triggered()), this, SLOT(resetButtonPressed()));

    _optionsDialogAction = new QAction(tr("&Options"), this);
    connect(_optionsDialogAction, SIGNAL(triggered()), this, SLOT(optionsDialogTriggered()));
}