00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #include "toten.h"
00026
00027
00028
00029
00030
00031
00032
00033 void execute(char *buff);
00034 int check_toten(void);
00035
00036
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
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
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
00121
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;
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