Skip to content

Commit

Permalink
#29: Initial C++ implementation of:
Browse files Browse the repository at this point in the history
 - PhpPostProcess
 - StampVer
 - SwigPrepare
  • Loading branch information
jumpinjackie committed Jun 23, 2018
1 parent 27e816a commit 541d2fe
Show file tree
Hide file tree
Showing 9 changed files with 492 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ obj
/thirdparty/php7/src/php-7.1.18
!/thirdparty/php7/src/php-7.1.18/main/config.w32.h
/build
/tools
/runtimes/php/Release
/runtimes/php/Release64
/src/Test/Php/*.dll
Expand Down
12 changes: 12 additions & 0 deletions src/Tools/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
cmake_minimum_required(VERSION 2.8)

if(COMMAND cmake_policy)
cmake_policy(SET CMP0003 NEW)
#cmake_policy(SET CMP0054 OLD)
endif(COMMAND cmake_policy)

set(TOOL_INSTALL_PATH "${CMAKE_CURRENT_SOURCE_DIR}/../../tools")

add_subdirectory(PhpPostProcess)
add_subdirectory(StampVer)
add_subdirectory(SwigPrepare)
165 changes: 165 additions & 0 deletions src/Tools/Common/helpers.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
#ifndef FS_HELPER_H
#define FS_HELPER_H

#include <iostream>
#include <fstream>
#include <string>
#include <sys/stat.h> // stat
#include <errno.h> // errno, ENOENT, EEXIST
#if defined(_WIN32)
#include <direct.h> // _mkdir
#endif

// Based on the following code samples:
// https://stackoverflow.com/questions/675039/how-can-i-create-directory-tree-in-c-linux
// https://stackoverflow.com/questions/1494399/how-do-i-search-find-and-replace-in-a-standard-string
// https://stackoverflow.com/questions/874134/find-if-string-ends-with-another-string-in-c
// http://www.cplusplus.com/doc/tutorial/files/

bool file_exists(const std::string &path)
{
#if defined(_WIN32)
struct _stat info;
if (_stat(path.c_str(), &info) != 0)
#else
struct stat info;
if (stat(path.c_str(), &info) != 0)
#endif
return false;
else
return true;
}

bool directory_exists(const std::string& path)
{
#if defined(_WIN32)
struct _stat info;
if (_stat(path.c_str(), &info) != 0)
{
return false;
}
return (info.st_mode & _S_IFDIR) != 0;
#else
struct stat info;
if (stat(path.c_str(), &info) != 0)
{
return false;
}
return (info.st_mode & S_IFDIR) != 0;
#endif
}

bool make_path(const std::string& path)
{
#if defined(_WIN32)
int ret = _mkdir(path.c_str());
#else
mode_t mode = 0755;
int ret = mkdir(path.c_str(), mode);
#endif
if (ret == 0)
return true;

switch (errno)
{
case ENOENT:
// parent didn't exist, try to create it
{
int pos = path.find_last_of('/');
if (pos == std::string::npos)
#if defined(_WIN32)
pos = path.find_last_of('\\');
if (pos == std::string::npos)
#endif
return false;
if (!make_path( path.substr(0, pos) ))
return false;
}
// now, try to create again
#if defined(_WIN32)
return 0 == _mkdir(path.c_str());
#else
return 0 == mkdir(path.c_str(), mode);
#endif

case EEXIST:
// done!
return directory_exists(path);

default:
return false;
}
}

bool str_ends_with(std::string const &fullString, std::string const &ending)
{
if (fullString.length() >= ending.length()) {
return (0 == fullString.compare (fullString.length() - ending.length(), ending.length(), ending));
} else {
return false;
}
}

void str_replace(std::string& str,
const std::string& oldStr,
const std::string& newStr)
{
std::string::size_type pos = 0u;
while((pos = str.find(oldStr, pos)) != std::string::npos) {
str.replace(pos, oldStr.length(), newStr);
pos += newStr.length();
}
}

bool replace_content_between(std::string& content,
const std::string& start,
const std::string& end,
const std::string& replace)
{
bool didAtLeastOneReplacement = false;
size_t pos_start = content.find(start);
while (std::string::npos != pos_start)
{
size_t pos_end = content.find(end, pos_start);
if (std::string::npos != pos_end)
{
size_t pos_repl = pos_start + replace.length() - 1;
content.replace(pos_repl, pos_end - pos_repl, replace);
didAtLeastOneReplacement = true;

pos_start = content.find(start, pos_end);
}
}
return didAtLeastOneReplacement;
}

bool write_all_text(const std::string& path, const std::string& content)
{
std::ofstream myfile(path.c_str());
if (myfile.is_open())
{
myfile << content;
myfile.close();
return true;
}
return false;
}

bool read_all_text(const std::string& path, std::string& content)
{
std::string line;
std::ifstream myfile(path.c_str());
if (myfile.is_open())
{
while (std::getline(myfile,line))
{
content += line;
content += "\n";
}
myfile.close();
return true;
}
return false;
}

#endif
22 changes: 22 additions & 0 deletions src/Tools/PhpPostProcess/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
project(PhpPostProcess)

include_directories(
"${CMAKE_CURRENT_SOURCE_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}/../Common"
)

set(PhpPostProcess_SRCS
main.cpp
)

add_executable(PhpPostProcess ${PhpPostProcess_SRCS})

if (MSVC)
set_target_properties(PhpPostProcess PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${TOOL_INSTALL_PATH} )
set_target_properties(PhpPostProcess PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${TOOL_INSTALL_PATH} )
set_target_properties(PhpPostProcess PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${TOOL_INSTALL_PATH} )
endif (MSVC)

if (UNIX)
install(TARGETS PhpPostProcess DESTINATION ${TOOL_INSTALL_PATH})
endif (UNIX)
98 changes: 98 additions & 0 deletions src/Tools/PhpPostProcess/main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
#include <iostream>
#include <set>
//#include <vector>
#include "helpers.h"

void check_traits(const std::string& line, std::set<std::string>& traits)
{
size_t pos = line.find("trait ");
if (pos != std::string::npos)
{
size_t pos_end = line.find("Patched ", pos + 6 /* "trait " */);
if (pos_end != std::string::npos)
{
std::string className = line.substr(pos + 6, pos_end - 6);
std::cout << "Found trait for class: " << className << std::endl;

traits.insert(className);
}
}
}

void apply_traits(const std::string& line, std::string& content, std::set<std::string>& traits)
{
size_t pos = line.find("class ");
if (pos != std::string::npos)
{
size_t pos_class_start = pos + 6;
size_t pos_end = line.find(" ", pos_class_start /* "class " */);
if (pos_end != std::string::npos)
{
std::string className = line.substr(pos_class_start, pos_end - pos_class_start);
std::cout << pos_class_start << ":" << pos_end << " Checking if trait exists for class: `" << className << "`" << std::endl;
if (traits.find(className) != traits.end())
{
content += " use ";
content += className;
content += "Patched; /* Inserted by PhpPostProcess tool */";
content += "\n";
std::cout << "Applied trait for class: " << className << std::endl;
}
}
}
}

int main(int argc, char **argv)
{
/*
std::vector<std::string> lines;
lines.push_back("<?php");
lines.push_back("trait MgExceptionPatched {");
lines.push_back("}");
lines.push_back("class MgException {");
lines.push_back(" function getMessage() { }");
lines.push_back("}");
lines.push_back("class MgSiteConnection {");
lines.push_back(" function CreateService($serviceType) { }");
lines.push_back("}");
lines.push_back("?>");
std::set<std::string> traits;
std::string content;
for (std::vector<std::string>::iterator it = lines.begin(); it != lines.end(); it++)
{
std::string line = *it;
check_traits(line, traits);
content += line;
content += "\n";
apply_traits(line, content, traits);
}
std::cout << content << std::endl;
return 0;
*/
if (argc != 2)
{
std::cout << "Usage: PhpPostProcess [path to MapGuideApi.php]" << std::endl;
return 1;
}

std::set<std::string> traits;
std::string content;

std::string line;
std::ifstream myfile(argv[1]);
if (myfile.is_open())
{
while (std::getline(myfile,line))
{
check_traits(line, traits);
content += line;
content += "\n";
apply_traits(line, content, traits);
}
myfile.close();
return 0;
}
return 1;
}
22 changes: 22 additions & 0 deletions src/Tools/StampVer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
project(StampVer)

include_directories(
"${CMAKE_CURRENT_SOURCE_DIR}"
"${CMAKE_CURRENT_SOURCE_DIR}/../Common"
)

set(StampVer_SRCS
main.cpp
)

add_executable(StampVer ${StampVer_SRCS})

if (MSVC)
set_target_properties(StampVer PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${TOOL_INSTALL_PATH} )
set_target_properties(StampVer PROPERTIES RUNTIME_OUTPUT_DIRECTORY_DEBUG ${TOOL_INSTALL_PATH} )
set_target_properties(StampVer PROPERTIES RUNTIME_OUTPUT_DIRECTORY_RELEASE ${TOOL_INSTALL_PATH} )
endif (MSVC)

if (UNIX)
install(TARGETS StampVer DESTINATION ${TOOL_INSTALL_PATH})
endif (UNIX)
Loading

0 comments on commit 541d2fe

Please sign in to comment.