Logo Search packages:      
Sourcecode: adonthell version File versions  Download package

void config::parse_arguments ( int  argc,
char *  argv[] 
)

See whether any options have been specified on the command line. Possible command line options are

  • \-h Print help message
  • \-d Print the data directory
  • \-v Print the version number
  • \-l List games found in the gamedir
  • \-g dir Play game contained in dir
  • \-c Byte-compile all Python scripts in the current directory

Parameters:
argc argument count
argv argument vector

Definition at line 116 of file prefs.cc.

References python::cleanup(), python::exec_string(), game_name, gamedir, game::global_data_dir(), python::init(), print_available_games(), and print_help_message().

{     
    int c;
    
    // Check for options
    while (1)
    {
        c = getopt (argc, argv, "lcdhvg:");
        if (c == -1)
            break;
        
        switch (c)
        { 
            case 'l':
                print_available_games (); 
                exit (0); 
                break;
                
            case 'd':
                cout << game::global_data_dir() << endl;
                exit (0); 
                break;
                
            case 'v':
                cout << VERSION << endl;
                exit (0); 
                break;
                
            case 'c':
            {
                python::init (); 
                python::exec_string ("import compileall; compileall.compile_dir (\".\", 0);");  
                python::cleanup (); 
                exit (0); 
                break;
            }

            case 'g':
            {
                gamedir = optarg;
                if (gamedir[gamedir.size () - 1] == '/')
                    gamedir.erase (gamedir.size () - 1);
                
                // Check whether the requested game directory exists
                DIR * mydir = opendir (gamedir.c_str ()); 
                
                if (!mydir) 
                {
                    cerr << "Cannot open directory " << gamedir << "!" << endl;
                    exit (1); 
                }
                closedir (mydir);
                
                break;
            }
            
            case '?':
            case 'h':
                print_help_message (argv);
                exit (0);
                break;                 
        }
    }

    // Check whether the GAME parameter is needed
    if (gamedir == "")
    {
        // Check whether the GAME parameter is given
        if (argc - optind != 1)
        {
            print_help_message (argv);
            exit (0);
        }
        
        // Check whether the requested game exists
        struct dirent * d;
        DIR * mydir = opendir ((game::global_data_dir() + "/games").c_str()); 
        bool found = false; 
        
        if (!mydir) 
        {
            cerr << "Cannot open directory " << game::global_data_dir() + "/games!" << endl;
            exit (1); 
        }
        
        while ((d = readdir (mydir)) != NULL)
        {
            if (string (d->d_name) == argv[optind]) found = true; 
        }
        
        closedir (mydir); 
        
        if (!found) 
        {
            cerr << "Game '" << argv[optind] << "' can't be found.\n"
                 << "Run '" << argv[0] << " -l' for a list of available games.\n"; 
            exit (1); 
        }
        
        // The game exists, so let the show continue...
        gamedir = game::global_data_dir() + "/games/"; 
        gamedir += argv[optind];
    }

    // Now check whether the directory is a valid game directory
    string tfile = gamedir + "/gamename.txt"; 
    ifstream f (tfile.c_str ()); 
    if (!f.is_open ()) 
    {
        cerr << "Directory " << gamedir << " is not a valid game directory.\n";
        exit (1); 
    }
    char t[256];
    f.getline (t, 256); 
    game_name = t;
    f.close ();
}


Generated by  Doxygen 1.6.0   Back to index