Newer
Older
wxmsg / wxmsg.cpp
/* $Id$ */
/*
 * wxmsg: a xmessage clone made with wxWidgets
 */

/*
 * Copyright (c) 2007 Andreas Jaggi <andreas.jaggi@waterwave.ch>
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <iostream>
#include <vector>
#include <string>

using namespace std;

const char usagemsg[] =
	"usage: -> man xmessage\n"
	"\n"
	" also: wxmsg -v    show version\n"
	"       wxmsg -h    display this help\n"
	"       wxmsg -l    display (BSD) license\n"
	;

const char licensemsg[] =
	"wxmsg is copyright (c) 2007 Andreas Jaggi <andreas.jaggi@waterwave.ch>\n"
	"All rights reserved.\n"
	"\n"
	"Redistribution and use in source and binary forms, with or without\n"
	"modification, are permitted provided that the following conditions\n"
	"are met:\n"
	"1. Redistributions of source code must retain the above copyright\n"
	"   notice, this list of conditions and the following disclaimer.\n"
	"2. Redistributions in binary form must reproduce the above copyright\n"
	"   notice, this list of conditions and the following disclaimer in the\n"
	"   documentation and/or other materials provided with the distribution.\n"
	"3. The name of the author may not be used to endorse or promote products\n"
	"   derived from this software without specific prior written permission.\n"
	"\n"
	"THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR\n"
	"IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n"
	"OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\n"
	"IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,\n"
	"INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT\n"
	"NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n"
	"DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n"
	"THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n"
	"(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF\n"
	"THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n"
	;

const char versionmsg[] = "wxmsg 0.0\n";


#define usage() cout << usagemsg
#define license() cout << licensemsg
#define version() cout << versionmsg

void read_buttons(string input, vector<string> &buttons);

int main ( int argc, char** argv ) {
	int args_consumed = 1;

	vector<string> buttons = vector<string>();
	string default_label = ""; // string with size 0 == no label given
	string message_file = "";
	int print = 0;
	int center = 0;
	int nearmouse = 0;
	int timeout = 0;

	int found = 1;

	while ( found ) {
		found = 0;

		if ( (argc-args_consumed > 0) && !strcmp(argv[args_consumed], "-h") ) {
			usage();
			exit(0);
		}
		if ( (argc-args_consumed > 0) && !strcmp(argv[args_consumed], "-l") ) {
			license();
			exit(0);
		}
		if ( (argc-args_consumed > 0) && !strcmp(argv[args_consumed], "-v") ) {
			version();
			exit(0);
		}

		if ( (argc-args_consumed > 0) && !strcmp(argv[args_consumed], "-buttons") ) {
			args_consumed++;
			if ( argc - args_consumed < 1 ) {
				// error: buttons argument w/o button definitions
				usage();
				exit(1);
			}
			read_buttons(argv[args_consumed], buttons);
			args_consumed++;
			found = 1;
		}

		if ( (argc-args_consumed > 0) && !strcmp(argv[args_consumed], "-default") ) {
			args_consumed++;

			if ( argc - args_consumed < 1 ) {
				// error: no label for -default parameter
				usage();
				exit(1);
			}

			default_label = argv[args_consumed]; // TODO: check that there exists such an label?
			args_consumed++;
			found = 1;
		}

		if ( (argc-args_consumed > 0) && !strcmp(argv[args_consumed], "-print") ) {
			args_consumed++;
			print = 1;
			found = 1;
		}

		if ( (argc-args_consumed > 0) && !strcmp(argv[args_consumed], "-center") ) {
			args_consumed++;
			if ( nearmouse ) {
				// error: -center AND -nearmouse were given
				usage();
				exit(1);
			}
			center = 1;
			found = 1;
		}

		if ( (argc-args_consumed > 0) && !strcmp(argv[args_consumed], "-nearmouse") ) {
			args_consumed++;
			if ( center ) {
				// error: -center AND -nearmouse were given
				usage();
				exit(1);
			}
			nearmouse = 1;
			found = 1;
		}

		if ( (argc-args_consumed > 0) && !strcmp(argv[args_consumed], "-timeout") ) {
			args_consumed++;
			if ( argc - args_consumed < 1 ) {
				// error: no timeout value given
				usage();
				exit(1);
			}
			timeout = atoi(argv[args_consumed]);
			if ( timeout < 0 ) {
				// error: negative timeout
				usage();
				exit(1);
			}
			args_consumed++;
			found = 1;
		}
	}

	if ( buttons.empty() ) {
		buttons.push_back("okay:0");
	}

	if ( (argc-args_consumed > 0) && !strcmp(argv[args_consumed], "-file") ) {

		args_consumed++;

		if ( argc - args_consumed < 1 ) {
			// error: no label for -default parameter
			usage();
			exit(1);
		}
		message_file = argv[args_consumed];
		args_consumed++;
	} else {
		if ( argc - args_consumed < 1 ) {
			// error: no message given
			usage();
			exit(1);
		}
	}

	if ( argc - args_consumed == 0 ) {
		// error: message AND file were given
		usage();
		exit(1);
	}


	/* TODO: add wxWidgets interface here (instead of this cruft) */
	while ( !buttons.empty() ) {
		cout << "Button: " << buttons[buttons.size()-1] << endl;
		buttons.pop_back();
	}
	if ( default_label != "" )
		cout << "default_label: " << default_label << endl;
	if ( message_file != "" )
		cout << "message_file: " << message_file << endl;
	if ( print )
		cout << "-print" << endl;
	if ( center )
		cout << "-center" << endl;
	if ( nearmouse )
		cout << "-nearmouse" << endl;
	if ( timeout > 0 )
		cout << "timeout: " << timeout << endl;


	exit(0);
}

void read_buttons ( string input, vector<string> &buttons ) {
	unsigned int commapos = string::npos;
	unsigned int lastpos = 0;

	while ( (commapos = input.find(",", lastpos)) != string::npos ) {
		buttons.push_back(input.substr(lastpos, (commapos - lastpos)));
		lastpos = commapos+1;
	}

	buttons.push_back(input.substr(lastpos, input.length()));
}