libs.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. #include "stdafx.h"
  2. #include "libs.h"
  3. #include "editor.h"
  4. #include "blitzide.h"
  5. static map<string,string> keyhelps;
  6. int linker_ver,runtime_ver;
  7. static string execProc( const string &proc ){
  8. HANDLE rd,wr;
  9. SECURITY_ATTRIBUTES sa={sizeof(sa),0,true};
  10. if( CreatePipe( &rd,&wr,&sa,0 ) ){
  11. STARTUPINFO si={sizeof(si)};
  12. si.dwFlags=STARTF_USESTDHANDLES;
  13. si.hStdOutput=si.hStdError=wr;
  14. PROCESS_INFORMATION pi={0};
  15. if( CreateProcess( 0,(char*)proc.c_str(),0,0,true,DETACHED_PROCESS,0,0,&si,&pi ) ){
  16. CloseHandle( pi.hProcess );
  17. CloseHandle( pi.hThread );
  18. CloseHandle( wr );
  19. string t;
  20. char *buf=new char[1024];
  21. for(;;){
  22. unsigned long sz;
  23. int n=ReadFile( rd,buf,1024,&sz,0 );
  24. if( !n && GetLastError()==ERROR_BROKEN_PIPE ) break;
  25. if( !n ){ t="";break; }
  26. if( !sz ) break;
  27. t+=string( buf,sz );
  28. }
  29. delete[] buf;
  30. CloseHandle(rd);
  31. return t;
  32. }
  33. CloseHandle( rd );
  34. CloseHandle( wr );
  35. }
  36. AfxMessageBox( (proc+" failed").c_str() );
  37. ExitProcess(0);
  38. return "";
  39. }
  40. int version( string vers,string t ){
  41. t+=" version:";
  42. int n=vers.find( t );n+=t.size();
  43. int maj=atoi( vers.substr(n) );n=vers.find( '.',n )+1;
  44. int min=atoi( vers.substr(n) );
  45. return maj*100+min;
  46. }
  47. void initLibs(){
  48. string valid=execProc( prefs.homeDir+"/bin/blitzcc -q" );
  49. if( valid.size() ){
  50. AfxMessageBox( ("Compiler environment error: "+valid).c_str() );
  51. ExitProcess(0);
  52. }
  53. string vers=tolower( execProc( prefs.homeDir+"/bin/blitzcc -v" ) );
  54. linker_ver=version( vers,"linker" );
  55. runtime_ver=version( vers,"runtime" );
  56. //generate keywords!
  57. string kws=execProc( prefs.homeDir+"/bin/blitzcc +k" );
  58. if( !kws.size() ){
  59. AfxMessageBox( "Error generating keywords" );
  60. ExitProcess(0);
  61. }
  62. int pos=0,n;
  63. while( (n=kws.find( '\n',pos ))!=string::npos ){
  64. string t=kws.substr( pos,n-pos-1 );
  65. for( int q=0;(q=t.find('\r',q))!=string::npos; ) t=t.replace( q,1,"" );
  66. string help=t;
  67. int i=t.find(' ');
  68. if( i!=string::npos ){
  69. t=t.substr(0,i);if( !t.size() ){
  70. AfxMessageBox( "Error in keywords" );
  71. ExitProcess(0);
  72. }
  73. if( !isalnum(t[t.size()-1]) ) t=t.substr(0,t.size()-1);
  74. }
  75. Editor::addKeyword(t);
  76. keyhelps[t]=help;
  77. pos=n+1;
  78. }
  79. }
  80. string quickHelp( const string &kw ){
  81. map<string,string>::const_iterator it=keyhelps.find(kw);
  82. return it==keyhelps.end() ? "" : it->second;
  83. }
  84. bool isMediaFile( const string &f ){
  85. #ifndef PRO
  86. return false;
  87. #endif
  88. static char *exts[]={
  89. "bmp","jpg","png","tga","iff","pcx",
  90. "wav","mid","mp3","mod","s3m","xm","it","rmi","sgt",
  91. "x","3ds",0
  92. };
  93. int i=f.rfind( '.' );
  94. if( i==string::npos || i+1==f.size() ) return false;
  95. string ext=f.substr( i+1 );
  96. char **p=exts;
  97. while( const char *e=*p++ ){
  98. string t(e);
  99. if( i+t.size()+1!=f.size() ) continue;
  100. if( ext==t ) return true;
  101. }
  102. return false;
  103. }