Skip to content

Commit

Permalink
Initial release.
Browse files Browse the repository at this point in the history
  • Loading branch information
svaningelgem committed May 14, 2020
1 parent 4a26fb9 commit aa1d8d1
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,11 @@
*.exe
*.out
*.app
/build/
/build_32/
/build_64/
/_CPack_Packages/


*.zip
*.exe
42 changes: 42 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
cmake_minimum_required(VERSION 3.0)


project(ptime)


add_executable(
${PROJECT_NAME}

ptime.cpp
)


target_link_libraries(
${PROJECT_NAME}
Winmm
)


install(TARGETS ${PROJECT_NAME} RUNTIME DESTINATION .)


find_package(SelfPackers)
if(SELF_PACKER_FOR_EXECUTABLE)
set(release $<CONFIG:Release>)
add_custom_command(
TARGET ${PROJECT_NAME}
POST_BUILD
COMMAND "$<${release}:${SELF_PACKER_FOR_EXECUTABLE}>" "$<${release}:-9>" "$<${release}:$<TARGET_FILE:${PROJECT_NAME}>>"
)
endif()


set(CPACK_PACKAGE_NAME "${PROJECT_NAME}")
set(CPACK_PACKAGE_VENDOR "Salvania")
set(CPACK_GENERATOR "ZIP")
set(CPACK_PACKAGE_DIRECTORY "${CMAKE_SOURCE_DIR}")
set(CPACK_VERBATIM_VARIABLES true)
set(CPACK_PACKAGE_VERSION_MAJOR 1)
set(CPACK_PACKAGE_VERSION_MINOR 0)
set(CPACK_PACKAGE_VERSION_PATCH 0)
include(CPack)
14 changes: 14 additions & 0 deletions build_all.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
@echo off


set MY_DIR="%cd%"

cd "%~dp0"

call builder -32bit -Release -upx
call builder -64bit -Release -upx

cd %MY_DIR%


@echo on
55 changes: 55 additions & 0 deletions builder.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
@echo off


rem https://stackoverflow.com/questions/3785976/cmake-generate-visual-studio-2008-solution-for-win32-and-x64


rem ===== Usage
rem builder [-32bit|-64bit] [-Debug|-Release]
rem --> Defaults: -64bit -Release


rem Global configuration variables. Should be update based on your system
SET VISUAL_STUDIO_HOME=C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC
SET VISUAL_STUDIO_NAME=Visual Studio 16 2019
SET CMAKE_HOME=C:\Program Files\CMake

rem First parameter is project folder path
SET PROJECT_DIR="%~dp0"
SET ORIGINAL_DIR="%cd%"
SET CMAKE="%CMAKE_HOME%\bin\cmake.exe"

rem Go to project directory
cd %PROJECT_DIR%

rem Second parameter defines 32 bit or 64 bit compilation
if "%1"=="-32bit" (
echo === Generating 32bit project ===

rmdir /s /q build_32
md build_32
cd build_32
%CMAKE% .. -G "%VISUAL_STUDIO_NAME%" -A Win32
) ELSE (
echo === Generating 64bit project ===

rmdir /s /q build_64
md build_64
cd build_64
%CMAKE% .. -G "%VISUAL_STUDIO_NAME%" -A x64
)

rem Third parameter defines debug or release compilation
if "%2"=="-Debug" (
echo === Building in Debug mode ===
%CMAKE% --build . --target PACKAGE --config Debug
) ELSE (
echo === Building in Release mode ===
%CMAKE% --build . --target PACKAGE --config Release
)


rem Go to source code directory and finalize script
cd %ORIGINAL_DIR%

@echo on
71 changes: 71 additions & 0 deletions ptime.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#include <iostream>
#include <iomanip>
#include <cctype>
#include <algorithm>
#include <sstream>
#include <Windows.h>


using namespace std;


// https://stackoverflow.com/questions/3418231/replace-part-of-a-string-with-another-string
std::string ReplaceString(std::string subject, const std::string& search,
const std::string& replace) {
size_t pos = 0;
while ((pos = subject.find(search, pos)) != std::string::npos) {
subject.replace(pos, search.length( ), replace);
pos += replace.length( );
}
return subject;
}


string escape_shell_argument(string in) {
static const string single_quote = "\"";
static const string escaped_single_quote = "\\\"";

string quoted = ReplaceString(in, single_quote, escaped_single_quote);
if (count_if(quoted.begin( ), quoted.end( ), [] (unsigned char c) { return std::isspace(c); }) > 0) {
return single_quote + quoted + single_quote;
}
else {
return quoted;
}
}


void print_help( ) {
cerr << "Syntax: ptime command [arguments ...]" << endl
<< endl
<< "ptime will run the specified command and measure the execution time" << endl
<< "(run time) in seconds, accurate to 5 millisecond or better. It is an" << endl
<< "automatic process timer, or program timer." << endl;
}


int main(int argc, const char* argv[]) {
if (argc <= 1) {
print_help( );
return -1;
}

std::stringstream cmd_to_run;
for (int i = 1; i < argc; i++) {
if (i != 1) cmd_to_run << ' ';
cmd_to_run << escape_shell_argument(argv[i]);
}


cout << "=== " << cmd_to_run.str() << " ===" << endl;

timeBeginPeriod(1u);
DWORD start_time = timeGetTime( );
system(cmd_to_run.str().c_str());
DWORD total_time = timeGetTime( ) - start_time;
timeEndPeriod(1u);

cout << "=== Execution time: " << std::fixed << std::setprecision(3) << (total_time * 0.001) << " s ===" << endl;

return 0;
}

0 comments on commit aa1d8d1

Please sign in to comment.