| #include "System.h" |
| // |
| #include <stdio.h> |
| //#include <process.h> |
| #include <errno.h> |
| #include <memory.h> |
| #include "String.h" |
| #ifndef WINDOWS |
| #include <stdlib.h> |
| #include <sys/types.h> |
| #include <sys/wait.h> |
| #include <unistd.h> |
| #endif |
| |
| void* System::SafeAllocMemory(UINT size) |
| { |
| if ( 0 == size ) |
| return 0; |
| #ifdef WINDOWS |
| void* result = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size); |
| return result; |
| #else |
| void* result = malloc(size); |
| if ( 0 == result) |
| return 0; |
| memset(result, 0, size); |
| return result; |
| #endif |
| |
| } |
| |
| BOOL System::SafeFreeMemory(void* memory) |
| { |
| if (0 == memory) |
| return false; |
| #ifdef WINDOWS |
| return HeapFree(GetProcessHeap(),0, memory); |
| #else |
| free( memory); |
| return true; |
| #endif |
| |
| |
| } |
| void System::ShowErrorMessage(STRING message) |
| { |
| #ifdef WINDOWS |
| MessageBoxA(0, message, "Extracting error", MB_OK | MB_ICONERROR); |
| #else |
| fprintf( stderr, "%s\n",message); |
| fflush(stderr); |
| #endif |
| |
| } |
| |
| BOOL System::ExecuteAndWait(STRING executablePath, STRING args){ |
| #ifdef WINDOWS |
| STARTUPINFOA si; |
| PROCESS_INFORMATION pi; |
| GetStartupInfoA(&si); |
| STRING fullCommand = (STRING)System::SafeAllocMemory(String::StrLen(executablePath) + String::StrLen(args) + 20); |
| String::Sprintf2(fullCommand,"%s %s\0", executablePath, args); |
| if ( FALSE != CreateProcessA(0,fullCommand,0,0,FALSE,0,0,0,&si,&pi) ){ |
| |
| System::SafeFreeMemory(fullCommand); |
| //wait it for exit |
| DWORD waitResult = WaitForSingleObject(pi.hProcess, 0xFFFFFFFF); //wait infinite. may be we should change it later |
| //clean up |
| CloseHandle(pi.hProcess); |
| CloseHandle(pi.hThread); |
| if ( WAIT_FAILED == waitResult ){ |
| return FALSE; |
| } |
| return TRUE; |
| }else{ |
| System::SafeFreeMemory(fullCommand); |
| //DWORD lastErr = GetLastError(); |
| return FALSE; |
| } |
| |
| #else |
| //make it executable |
| STRING command = (STRING)System::SafeAllocMemory(strlen(executablePath) + 20); |
| strcpy(command, "chmod +x "); |
| strcat(command, executablePath); |
| system(command); |
| System::SafeFreeMemory(command); |
| pid_t child = fork(); |
| if ( -1 == child) |
| return false; |
| if ( 0 == child ) |
| { |
| //we are in child process |
| execl(executablePath, args, NULL); |
| return false; //never ever will return |
| } |
| else |
| { |
| waitpid(child, NULL, 0); |
| return true; |
| } |
| return true; |
| #endif |
| |
| } |
| |
| STRING System::GetTemporaryFolder() |
| { |
| #ifdef WINDOWS |
| STRING tempFolder = (STRING)System::SafeAllocMemory(MAX_PATH + 1); |
| if ( 0 == tempFolder) return 0; |
| |
| DWORD result = GetTempPathA(MAX_PATH, tempFolder); |
| if ( 0 == result ) |
| { |
| SafeFreeMemory(tempFolder); |
| return 0; //WARNING: no check made for buffer's enough capcity |
| } |
| return tempFolder; |
| #else |
| return "/tmp/"; // for all NIX systems |
| #endif |
| } |
| int System::MemCmp(BYTE* m1, BYTE* m2, int len) |
| { |
| for ( int i = 0; i < len; i++) |
| if ( *(m1 + i) != *(m2 + i) ) |
| return -1; |
| return 0; |
| } |
| |
| void System::MemCpy(BYTE* m1, BYTE* m2, int count) |
| { |
| while ( --count >= 0) |
| *(m1 + count) = *(m2 + count); |
| } |
| |
| |
| #ifdef WINDOWS |
| static HANDLE consoleStdOut = 0; |
| #endif |
| void System::WriteLog(STRING value) |
| { |
| #ifdef WINDOWS |
| DWORD dwWritten = 0; |
| WriteConsoleA(consoleStdOut, value, String::StrLen(value),&dwWritten,0); |
| #else |
| printf(value); |
| #endif |
| } |
| |
| BOOL System::AllocateConsole(STRING title) |
| { |
| #ifdef WINDOWS |
| BOOL result = AllocConsole(); |
| if (TRUE == result) |
| { |
| result = SetConsoleTitleA(title); |
| if ( TRUE == result) |
| consoleStdOut = GetStdHandle(STD_OUTPUT_HANDLE); |
| } |
| return result; |
| #else |
| return true; |
| #endif |
| } |
| |
| void System::WaitForAnyKey(){ |
| #ifdef WINDOWS |
| char buffer; |
| DWORD dwDummy; |
| ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE),&buffer, 1, &dwDummy, NULL ); |
| #else |
| int dummy; |
| scanf("%i", &dummy); |
| #endif |
| } |