COOPY » Guide
version 0.6.5
|
00001 #include "ssfossil.h" 00002 00003 #include <stdio.h> 00004 #include <string.h> 00005 #include <stdlib.h> 00006 #include <stdarg.h> 00007 00008 #include <string> 00009 using namespace std; 00010 00011 static int __ssfossil_debug = 0; 00012 static FossilHandler *_ssfossil_handler = NULL; 00013 00014 extern "C" int fossil_main(int argc, char **argv); 00015 extern "C" int _ssfossil_exit(int result); 00016 extern "C" int _ssfossil_printf(const char *format, ...); 00017 extern "C" int _ssfossil_fprintf(FILE *target, const char *format, ...); 00018 extern "C" int db_close(); 00019 00020 int ssfossil_debug() { 00021 return __ssfossil_debug; 00022 } 00023 00024 int ssfossil_call(int argc, char *argv[]) { 00025 if (getenv("SSFOSSIL_DEBUG")!=NULL) { 00026 if (!__ssfossil_debug) { 00027 __ssfossil_debug = 1; 00028 printf("*** Modified fossil, do not entrust it with your data ***\n"); 00029 } 00030 } 00031 int result = fossil_main(argc,argv); 00032 db_close(); 00033 return result; 00034 } 00035 00036 void ssfossil_set_handler(FossilHandler *handler) { 00037 _ssfossil_handler = handler; 00038 } 00039 00040 int _ssfossil_exit(int result) { 00041 if (_ssfossil_handler) { 00042 db_close(); 00043 return _ssfossil_handler->exit(result); 00044 // must not actually return! 00045 } 00046 exit(result); 00047 return 1; 00048 } 00049 00050 00051 int _ssfossil_printf(const char *format, ...) { 00052 va_list listPointer; 00053 00054 int result; 00055 if (_ssfossil_handler) { 00056 string str; 00057 int sz = 4192; 00058 00059 int n = 0; 00060 bool more = false; 00061 do { 00062 str.reserve(sz); 00063 va_start(listPointer,format); 00064 n = vsnprintf((char*)str.c_str(),sz,format,listPointer); 00065 va_end(listPointer); 00066 more = false; 00067 if (n<=-1) { 00068 sz *= 2; 00069 more = true; 00070 } else if (n>=sz) { 00071 sz = n+1; 00072 more = true; 00073 } 00074 } while (more); 00075 result = _ssfossil_handler->printf(str.c_str()); 00076 } else { 00077 va_start(listPointer,format); 00078 result = vprintf(format,listPointer); 00079 va_end(listPointer); 00080 } 00081 00082 return result; 00083 } 00084 00085 00086 00087 int _ssfossil_fprintf(FILE *target, const char *format, ...) { 00088 // exactly the same as _ssfossil_printf 00089 00090 va_list listPointer; 00091 00092 int result; 00093 if (_ssfossil_handler) { 00094 string str; 00095 int sz = 4192; 00096 00097 int n = 0; 00098 bool more = false; 00099 do { 00100 str.reserve(sz); 00101 va_start(listPointer,format); 00102 n = vsnprintf((char*)str.c_str(),sz,format,listPointer); 00103 va_end(listPointer); 00104 more = false; 00105 if (n<=-1) { 00106 sz *= 2; 00107 more = true; 00108 } else if (n>=sz) { 00109 sz = n+1; 00110 more = true; 00111 } 00112 } while (more); 00113 result = _ssfossil_handler->printf(str.c_str()); 00114 } else { 00115 va_start(listPointer,format); 00116 result = vprintf(format,listPointer); 00117 va_end(listPointer); 00118 } 00119 00120 return result; 00121 } 00122 00123