Skip to content

Commit

Permalink
variable replace is ok
Browse files Browse the repository at this point in the history
  • Loading branch information
vintagewang committed Jun 10, 2012
1 parent 6cdd6f9 commit af10fdd
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 11 deletions.
15 changes: 15 additions & 0 deletions src/JWUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,19 @@ namespace JWUtil
setenv(name, value, 1);
#endif
}

void replaceAll(std::string& source, const char* oldString, const char* newString)
{
assert(NULL != oldString);
assert(NULL != newString);

while(true) {
std::string::size_type pos = 0;
if((pos = source.find(oldString)) != std::string::npos) {
source.replace(pos, strlen(oldString), newString);
} else {
break;
}
}
}
}
7 changes: 7 additions & 0 deletions src/JWUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ namespace JWUtil
* 获取工作目录
*/
std::string getWorkingDir();

/**
* 获取当前程序可执行程序所在目录
*/
std::string getCurrentExeFileDir();

/**
* 获取当前程序可执行程序的全路径,含文件名
*/
Expand All @@ -36,6 +38,11 @@ namespace JWUtil
* 设置环境变量
*/
void setEnv(const char* name, const char* value);

/**
* 字符串替换
*/
void replaceAll(std::string& source, const char* oldString, const char* newString);
};

#endif // end of _JW_JWUTIL_H__
55 changes: 44 additions & 11 deletions src/JavaConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "JWUtil.h"
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/typeof/std/utility.hpp>
Expand Down Expand Up @@ -71,7 +72,12 @@ bool JavaConfig::load()
for(BOOST_AUTO(pos, child.begin());
pos != child.end();
++pos) {
this->propertyTable[pos->first.data()] = pos->second.data();
std::string value = pos->second.data();
if(this->expandMacro(value)) {
this->propertyTable[pos->first.data()] = value;
} else {
return false;
}
}
}

Expand All @@ -81,7 +87,12 @@ bool JavaConfig::load()
for(BOOST_AUTO(pos, child.begin());
pos != child.end();
++pos) {
this->classPathList.push_back(pos->second.data());
std::string value = pos->second.data();
if(this->expandMacro(value)) {
this->classPathList.push_back(value);
} else {
return false;
}
}
}

Expand Down Expand Up @@ -112,12 +123,28 @@ bool JavaConfig::load()
void JavaConfig::printAll()
{
if(this->debug) {
printf("javaHome = [%s]\n", this->javaHome.c_str());
printf("jvmType = [%s]\n", this->jvmType.c_str());
printf("mainClass = [%s]\n", this->mainClass.c_str());

for(size_t i = 0; i < this->classPathList.size(); i++) {
printf("classPathList %2d = [%s]\n", i, this->classPathList[i].c_str());
printf("javahome = [%s]\n", this->javaHome.c_str());
printf("jvmtype = [%s]\n", this->jvmType.c_str());
printf("mainclass = [%s]\n", this->mainClass.c_str());
{
printf("properties:\n");
PropertyTable::iterator it = this->propertyTable.begin();
for(; it != this->propertyTable.end(); it++) {
printf("\t %s = [%s]\n", it->first.c_str(), it->second.c_str());
}
}
{
printf("classpaths:\n");
for(size_t i = 0; i < this->classPathList.size(); i++) {
printf("\t %2d = [%s]\n", i, this->classPathList[i].c_str());
}
}
{
printf("options:\n");
OptionTable::iterator it = this->optionTable.begin();
for(; it != this->optionTable.end(); it++) {
printf("\t %s = [%s]\n", it->first.c_str(), it->second.c_str());
}
}
}
}
Expand All @@ -140,15 +167,21 @@ bool JavaConfig::expandMacro(std::string& value)

// 可执行程序所在目录
if(var == "cpd") {

value.replace(start - 2, end - start + 3, JWUtil::getCurrentExeFileDir());
}
// 当前工作目录
else if(var == "cwd") {

value.replace(start - 2, end - start + 3, JWUtil::getWorkingDir());
}
// 环境变量
else {

const char* env = getenv(var.c_str());
if(env != NULL) {
value.replace(start - 2, end - start + 3, env);
} else {
fprintf(stderr, "The environment variable <%s> is not exist\n", var.c_str());
result = false;
}
}
}
}
Expand Down

0 comments on commit af10fdd

Please sign in to comment.