stdutil.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #ifndef STDUTIL_H
  2. #define STDUTIL_H
  3. #include <set>
  4. #include <map>
  5. #include <vector>
  6. #include <string>
  7. #include <fstream>
  8. #include <iostream>
  9. #include <math.h>
  10. #include <time.h>
  11. #include <ctype.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdarg.h>
  15. #include <stdlib.h>
  16. #include <sys/stat.h>
  17. #if _WIN32
  18. #include <windows.h>
  19. #include <direct.h>
  20. #define mkdir(X,Y) mkdir(X)
  21. #define _realpath(X,Y) _fullpath(Y,X,MAX_PATH)
  22. #elif __APPLE__
  23. #include <unistd.h>
  24. #define _realpath realpath
  25. #include <signal.h>
  26. #include <ApplicationServices/ApplicationServices.h>
  27. int is_pid_native (pid_t pid);
  28. //only need these with gcc-3.3 : error with gcc4
  29. #ifndef isnan
  30. extern "C"{
  31. int isnan( double n );
  32. int isinf( double n );
  33. }
  34. #endif
  35. #elif __linux
  36. #include <unistd.h>
  37. #define _realpath realpath
  38. #endif
  39. #include <dirent.h>
  40. #ifndef MAX_PATH
  41. #if PATH_MAX
  42. #define MAX_PATH PATH_MAX
  43. #else
  44. #define MAX_PATH 4096
  45. #endif
  46. #endif
  47. #ifdef NDEBUG
  48. #undef NDEBUG
  49. #include <assert.h>
  50. #define NDEBUG
  51. #else
  52. #include <assert.h>
  53. #endif
  54. typedef long long int64;
  55. //wchar_t and wstring a total nightmare to get going - roll our own for now!
  56. typedef unsigned short bchar_t;
  57. namespace std{
  58. template<> struct char_traits<bchar_t>{
  59. typedef bchar_t char_type;
  60. typedef int int_type;
  61. static void assign( char_type &c,char_type a ){
  62. c=a;
  63. }
  64. static size_t length( const char_type *s ){
  65. int n=0;
  66. while( *s++ ) ++n;
  67. return n;
  68. }
  69. static char_type *assign( char_type *s,size_t n,char_type a ){
  70. for( size_t k=0;k<n;++k ) s[k]=a;
  71. return s;
  72. }
  73. static char_type *copy( char_type *s1,const char_type *s2,size_t n ){
  74. return static_cast<char_type*>( memcpy(s1,s2,n*sizeof(char_type)) );
  75. }
  76. static char_type *move( char_type *s1,const char_type *s2,size_t n ){
  77. return static_cast<char_type*>( memmove(s1,s2,n*sizeof(char_type)) );
  78. }
  79. static const char_type *find( const char_type *s,size_t n,char_type c ){
  80. for( size_t k=0;k<n;++k ) if( s[k]==c ) return s+k;
  81. return 0;
  82. }
  83. static int compare( const char_type *s1,const char_type *s2,size_t n ){
  84. for( size_t k=0;k<n;++k ) if( int t=s1[k]-s2[k] ) return t;
  85. return 0;
  86. }
  87. };
  88. }
  89. using namespace std;
  90. typedef basic_string<bchar_t> bstring;
  91. extern bool opt_trace; //-z BMK0: trace dependancies
  92. extern string opt_outfile; //-o BMK0: output exe
  93. extern bool opt_quiet; //-q
  94. extern bool opt_verbose; //-v
  95. extern bool opt_makeall; //-a
  96. extern bool opt_debug; //-d
  97. extern bool opt_release; //-r
  98. extern bool opt_threaded; //-h
  99. extern string opt_arch; //-g x86/ppc
  100. extern string opt_apptype; //-t apptype
  101. extern string opt_module; //-m 'modname'
  102. extern string opt_framework;//-f 'modname' or "*"
  103. extern string opt_infile;
  104. extern set<string> env_config;
  105. extern string env_blitzpath,env_platform,env_binpath,env_libpath,config_mung,global_mung;
  106. void stdutil_init( int argc,char *argv[] );
  107. //convert a dotted mod name to a path
  108. string modulePath( string mod,bool create );
  109. //split module components
  110. void splitModule( string mod,vector<string> &idents );
  111. //extract last component of a dotted mod name
  112. string moduleIdent( string mod );
  113. //return interface (.i) file for a module
  114. string moduleInterface( string mod );
  115. //enumerate modules starting with 'mod'
  116. void enumModules( string mod,vector<string> &mods );
  117. void sys( string cmd );
  118. string getcwd();
  119. void setcwd( string dir );
  120. time_t ftime( string path );
  121. string getdir( string path );
  122. string getext( string path );
  123. void fixpath( string &path );
  124. string stripdir( string path );
  125. string stripext( string path );
  126. string stripall( string path );
  127. string realpath( string path );
  128. int64 toint( string t );
  129. double tofloat( string t );
  130. string fromint( int64 n );
  131. string fromfloat( float n );
  132. string fromdouble( double n );
  133. string tolower( string str );
  134. string tostring( bstring w );
  135. bstring tobstring( string t );
  136. bstring tobstring( const char *p );
  137. extern string source_info;
  138. void fail( const char *fmt,... );
  139. #endif