Česky
Kamil Dudka

GED 2006 (C++)

File detail

Name:Downloadged2006.cc [Download]
Location: ged2006 > src
Size:2.9 KB
Last modification:2007-08-29 02:16

Source code

/*
 * File: ged2006.cc - main module
 * Project: GED - bitmap editor (ICP)
 * Author: Kamil Dudka, xdudka00
 * Team: xdudka00, xfilak01, xhefka00, xhradi08
 * Created: 2006-03-12
 */
 
#include <cstring>
#include <stdlib.h>
#include <iostream>
#include <fstream>
 
#include <FL/Fl.H>
 
#include "gedconf.h"
#include "pluginsupport.h"
#include "appwnd.h"
 
namespace {
	/*
	 * Destroy all singletons
	 */
	void singDestroy () {
	#ifndef NDEBUG
		std::cerr << "Deleting singletons..\n";
	#endif
		AppWnd::destroy ();
		PluginList::destroy ();
		GedConf::destroy ();
	}
 
	/*
	 * Read configuration and load plugin
	 */
	void loadPlugins () {
		// Read configuration
		GedConf *conf = GedConf::ptr();
		conf -> readConfig ("./ged.conf");
		//conf -> readConfig ("/etc/ged.conf");
		//conf -> readConfig ("~/.ged.conf");
 
		// Scan directory for plugins
		PluginDirsList dirList = conf->getPluginDirs ();
		PluginList *pl = PluginList::ptr();
 
		for (PluginDirsListIterator i= dirList.begin(); i!= dirList.end(); i++) {
			try {
				pl->pluginDirLookup ((*i).first.c_str ());
			}
			catch (PluginList::ErrPluginDir) {
				std::cerr << "ged2006: error: ";
				std::cerr << (*i).first << ": directory does not exist!" << std::endl;
			}
		}
	}
 
	/*
	 * Open image from file "szFileName"
	 */
	void openImage (const char *szFileName) {
		try {
			// Create image object
			Image *img = Image::readImageFromFile (szFileName);
 
			// Create image window
			Document *doc = new Document (img);
			doc -> show ();
		}
		catch (Image::ErrUnknownFF) {
			std::cerr << "ged2006: error: ";
			std::cerr << szFileName << ": Unknown file format!" << std::endl;
		}
		catch (Image::ErrLoad) {
			std::cerr << "ged2006: error: ";
			std::cerr << szFileName << ": could not open image!" << std::endl;
		}
	}
 
	/*
	 * Open images specified in cmd-line
	 */
	void parseCmdLine (int *argc, char *argv[]) {
		bool bFileFound = false;
 		for (argv++; 0!= *argv; argv++) {
			const char *szFileName = *argv;
 
			// Test if file exist
			std::fstream file (szFileName, std::ios::in);
			if (file) {
				file.close ();
				openImage (szFileName);
				bFileFound = true;
			}
			else if (bFileFound) {
				std::cerr << "ged2006: error: fltk arguments have to be before image file names!" << std::endl;
			}
 
			// Truncate cmd-line to pass to Fl_Window::show()
			if (bFileFound) {
				*argv = 0;
				(*argc) --;
			}
		}
	}
}
 
/*
 * main ()
 */
int main (int argc, char *argv[]) {
	// Avoid recursion during plugin scan 
	for (char **i= argv+1; 0!= *i; i++)
		if (0==strcmp ("--name", *i))
			return 2;
 
	// Destroy all singletons on program exit
	atexit (singDestroy);
 
	// Read configuration and load plugins
	loadPlugins ();
 
	// Show main window
	AppWnd::ptr()->show ();
 
	// Parse cmd-line arguments
	parseCmdLine (&argc, argv);
	AppWnd::ptr()->show (argc, argv);
 
	// Message loop
	int ec = Fl::run();
 
	// Save configuration
	if (!ec)
		GedConf::ptr()->saveConfig ();
 
	return ec;
}