| #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", (int)executablePath, (int)args); | |
| if ( 0 != CreateProcessA(0,fullCommand,0,0,false,0,0,0,&si,&pi) ) | |
| { | |
| System::SafeFreeMemory(fullCommand); | |
| //wait it for exit | |
| WaitForSingleObject(pi.hProcess, 0xFFFFFFFF); //wait infinite. may be we should change it later | |
| //clean up | |
| CloseHandle(pi.hProcess); | |
| CloseHandle(pi.hThread); | |
| 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 | |
| } |