main.m

Go to the documentation of this file.
00001 /*
00002     toten
00003     Copyright (C) 2004,2005 Toten Developers
00004 
00005     Toten is the legal property of its developers, whose names are
00006     too numerous to list here.  Please refer to the COPYRIGHT file
00007     for the full text of this license and to the AUTHORS file for
00008     the complete list of developers.
00009 
00010     This program is free software; you can redistribute it and/or
00011     modify it under the terms of the GNU Lesser General Public
00012     License as published by the Free Software Foundation; either
00013     version 2.1 of the License, or (at your option) any later version.
00014 
00015     This program is distributed in the hope that it will be useful,
00016     but WITHOUT ANY WARRANTY; without even the implied warranty of
00017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018     Lesser General Public License for more details.
00019 
00020     You should have received a copy of the GNU Lesser General Public
00021     License along with this program; if not, write to the Free Software
00022     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00023 */
00024 
00025 #include "toten.h"
00026 
00027 /**
00028  \file main.m
00029  Has the main function, log file support functions, and a self-check function. 
00030  */
00031 
00032 
00033 void execute(char *buff);
00034 int check_toten(void);
00035 
00036 /* Logging functions */
00037 int open_logfile(void);
00038 int write_log(char *log);
00039 
00040 static FILE *logfile;
00041 
00042 int main(int argc, char **argv)
00043 {
00044         open_logfile();
00045 
00046         if(argc > 1) {  
00047                 if(!strncmp(argv[1],"--check",7)) {             
00048                         return check_toten();
00049                 }
00050         }
00051         else {  
00052                 /* toten is supposed to be a network server.  In the interest of
00053                    getting the main game engine developed before the network portion,
00054                    I am putting in code here that should be in the client program for
00055                    testing purposes. */
00056 
00057                 /*
00058                 char buff[100];
00059 
00060                 for(;;) {
00061                         printf(": ");
00062                         fgets(buff,90,stdin);
00063                         execute(buff);
00064                         memset(buff,'\0',100);
00065                 }
00066                 */
00067 
00068                 setup_network_connection();
00069         }
00070         
00071         return 0;
00072 }
00073 
00074 void execute(char *buff)
00075 {
00076 
00077 }
00078 
00079 int check_toten(void)
00080 {
00081         TObject *obj;
00082         int ref;
00083 
00084         write_log("Starting the self-test...");
00085         
00086         printf("Creating a generic object...");
00087         obj = [TObject alloc];
00088         [obj init];
00089         printf("OK\n");
00090 
00091         printf("The current reference count on this object is %d.\n",[obj get_refcount]);
00092         printf("Incrementing the reference count twice...");
00093         [obj grab];
00094         [obj grab];
00095         printf("OK\n");
00096         printf("Decrementing the reference count four times...");
00097         [obj release];
00098         [obj release];
00099         ref = [obj release];
00100         printf("OK, new reference count is %d.\nObject is deallocated.\n",ref);
00101 
00102         printf("\nCreating a sword...");
00103         obj = [TItem alloc];
00104 
00105         [obj init:1:10:100:5:"kilograms":"Sword":"A sword"];    
00106 
00107         printf("OK\n");
00108         printf("Level:%d\nName:%s\nHP:%d\nPrice:%d\nWeight:%d %s\nDescription:%s\n",[obj get_lvl],[obj get_name],
00109                         [obj get_hp],[obj get_price],[obj get_weight],[obj get_weight_unit],[obj get_description]);
00110 
00111         printf("Deallocating the sword...");
00112         [obj release];
00113         printf("OK");
00114 
00115         write_log("Self-test finished, passed.");
00116         
00117 }
00118 
00119 /**
00120         If the directory ~/.toten doesn't exist, we assume that no logging is desired.
00121         Therefore, you should mkdir ~/.toten first if you want toten to log its activities.
00122  */
00123 int open_logfile(void)
00124 {
00125         char path[200];
00126         
00127         snprintf(path,199,"%s/.toten/log.txt",getenv("HOME"));
00128         
00129         if((logfile = fopen(path, "a+")) == NULL) {
00130                 logfile = stdout; /* STDOUT */
00131                 write_log("Couldn't open file, logging to stdout instead.\n");
00132         }
00133                 
00134 }
00135 
00136 int write_log(char *log)
00137 {       
00138         char timestring[100];
00139         struct tm *my_tm;
00140         time_t my_time;
00141         
00142         my_time = time(NULL);   
00143 
00144         my_tm = gmtime(&my_time);
00145         
00146         strftime(timestring,99,"%b %d %Y %H:%M:%S",my_tm);
00147 
00148         fprintf(logfile,"%s - %s\n",timestring,log);
00149                         
00150 }
00151 

Generated on Wed Dec 21 13:16:07 2005 for Toten by  doxygen 1.4.5 SourceForge.net Logo