From 121adade0d2a73be5f0e050fc8bcb7ac515f0c4d Mon Sep 17 00:00:00 2001 From: ruanshudong Date: Sun, 29 Mar 2020 19:28:25 +0800 Subject: [PATCH] fix tc_file get ext bug, add cmake_tars_server.bat for windows --- examples/UdpDemo/Server/Hello.h | 471 ------------------- examples/UtilDemo/demo-util/test_tc_file.cpp | 33 +- servant/script/cmake_http_server.bat | 1 + servant/script/cmake_tars_server.bat | 1 + servant/tup/Tars.h | 2 +- servant/tup/TarsJson.h | 4 +- servant/tup/TarsType.h | 3 +- util/include/util/tc_port.h | 4 + util/src/tc_ex.cpp | 2 +- util/src/tc_file.cpp | 104 ++-- util/src/tc_port.cpp | 18 + util/src/tc_socket.cpp | 4 + 12 files changed, 137 insertions(+), 510 deletions(-) delete mode 100644 examples/UdpDemo/Server/Hello.h create mode 100644 servant/script/cmake_http_server.bat create mode 100644 servant/script/cmake_tars_server.bat diff --git a/examples/UdpDemo/Server/Hello.h b/examples/UdpDemo/Server/Hello.h deleted file mode 100644 index f85e089..0000000 --- a/examples/UdpDemo/Server/Hello.h +++ /dev/null @@ -1,471 +0,0 @@ -// ********************************************************************** -// This file was generated by a TARS parser! -// TARS version 2.0.0. -// ********************************************************************** - -#ifndef __HELLO_H_ -#define __HELLO_H_ - -#include -#include -#include -#include "tup/Tars.h" -#include "tup/TarsJson.h" -using namespace std; -#include "servant/ServantProxy.h" -#include "servant/Servant.h" - - -namespace TestApp -{ - - /* callback of async proxy for client */ - class HelloPrxCallback: public tars::ServantProxyCallback - { - public: - virtual ~HelloPrxCallback(){} - virtual void callback_test(tars::Int32 ret) - { throw std::runtime_error("callback_test() override incorrect."); } - virtual void callback_test_exception(tars::Int32 ret) - { throw std::runtime_error("callback_test_exception() override incorrect."); } - - virtual void callback_testHello(tars::Int32 ret, const std::string& sRsp) - { throw std::runtime_error("callback_testHello() override incorrect."); } - virtual void callback_testHello_exception(tars::Int32 ret) - { throw std::runtime_error("callback_testHello_exception() override incorrect."); } - - public: - virtual const map & getResponseContext() const - { - CallbackThreadData * pCbtd = CallbackThreadData::getData(); - assert(pCbtd != NULL); - - if(!pCbtd->getContextValid()) - { - throw TC_Exception("cann't get response context"); - } - return pCbtd->getResponseContext(); - } - - public: - virtual int onDispatch(tars::ReqMessagePtr msg) - { - static ::std::string __Hello_all[]= - { - "test", - "testHello" - }; - pair r = equal_range(__Hello_all, __Hello_all+2, string(msg->request.sFuncName)); - if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; - switch(r.first - __Hello_all) - { - case 0: - { - if (msg->response->iRet != tars::TARSSERVERSUCCESS) - { - callback_test_exception(msg->response->iRet); - - return msg->response->iRet; - } - tars::TarsInputStream _is; - - _is.setBuffer(msg->response->sBuffer); - tars::Int32 _ret; - _is.read(_ret, 0, true); - - CallbackThreadData * pCbtd = CallbackThreadData::getData(); - assert(pCbtd != NULL); - - pCbtd->setResponseContext(msg->response->context); - - callback_test(_ret); - - pCbtd->delResponseContext(); - - return tars::TARSSERVERSUCCESS; - - } - case 1: - { - if (msg->response->iRet != tars::TARSSERVERSUCCESS) - { - callback_testHello_exception(msg->response->iRet); - - return msg->response->iRet; - } - tars::TarsInputStream _is; - - _is.setBuffer(msg->response->sBuffer); - tars::Int32 _ret; - _is.read(_ret, 0, true); - - std::string sRsp; - _is.read(sRsp, 2, true); - CallbackThreadData * pCbtd = CallbackThreadData::getData(); - assert(pCbtd != NULL); - - pCbtd->setResponseContext(msg->response->context); - - callback_testHello(_ret, sRsp); - - pCbtd->delResponseContext(); - - return tars::TARSSERVERSUCCESS; - - } - } - return tars::TARSSERVERNOFUNCERR; - } - - }; - typedef tars::TC_AutoPtr HelloPrxCallbackPtr; - - /* callback of coroutine async proxy for client */ - class HelloCoroPrxCallback: public HelloPrxCallback - { - public: - virtual ~HelloCoroPrxCallback(){} - public: - virtual const map & getResponseContext() const { return _mRspContext; } - - virtual void setResponseContext(const map &mContext) { _mRspContext = mContext; } - - public: - int onDispatch(tars::ReqMessagePtr msg) - { - static ::std::string __Hello_all[]= - { - "test", - "testHello" - }; - - pair r = equal_range(__Hello_all, __Hello_all+2, string(msg->request.sFuncName)); - if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; - switch(r.first - __Hello_all) - { - case 0: - { - if (msg->response->iRet != tars::TARSSERVERSUCCESS) - { - callback_test_exception(msg->response->iRet); - - return msg->response->iRet; - } - tars::TarsInputStream _is; - - _is.setBuffer(msg->response->sBuffer); - try - { - tars::Int32 _ret; - _is.read(_ret, 0, true); - - setResponseContext(msg->response->context); - - callback_test(_ret); - - } - catch(std::exception &ex) - { - callback_test_exception(tars::TARSCLIENTDECODEERR); - - return tars::TARSCLIENTDECODEERR; - } - catch(...) - { - callback_test_exception(tars::TARSCLIENTDECODEERR); - - return tars::TARSCLIENTDECODEERR; - } - - return tars::TARSSERVERSUCCESS; - - } - case 1: - { - if (msg->response->iRet != tars::TARSSERVERSUCCESS) - { - callback_testHello_exception(msg->response->iRet); - - return msg->response->iRet; - } - tars::TarsInputStream _is; - - _is.setBuffer(msg->response->sBuffer); - try - { - tars::Int32 _ret; - _is.read(_ret, 0, true); - - std::string sRsp; - _is.read(sRsp, 2, true); - setResponseContext(msg->response->context); - - callback_testHello(_ret, sRsp); - - } - catch(std::exception &ex) - { - callback_testHello_exception(tars::TARSCLIENTDECODEERR); - - return tars::TARSCLIENTDECODEERR; - } - catch(...) - { - callback_testHello_exception(tars::TARSCLIENTDECODEERR); - - return tars::TARSCLIENTDECODEERR; - } - - return tars::TARSSERVERSUCCESS; - - } - } - return tars::TARSSERVERNOFUNCERR; - } - - protected: - map _mRspContext; - }; - typedef tars::TC_AutoPtr HelloCoroPrxCallbackPtr; - - /* proxy for client */ - class HelloProxy : public tars::ServantProxy - { - public: - typedef map TARS_CONTEXT; - tars::Int32 test(const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) - { - tars::TarsOutputStream _os; - std::map _mStatus; - shared_ptr rep = tars_invoke(tars::TARSNORMAL,"test", _os, context, _mStatus); - if(pResponseContext) - { - pResponseContext->swap(rep->context); - } - - tars::TarsInputStream _is; - _is.setBuffer(rep->sBuffer); - tars::Int32 _ret; - _is.read(_ret, 0, true); - return _ret; - } - - void async_test(HelloPrxCallbackPtr callback,const map& context = TARS_CONTEXT()) - { - tars::TarsOutputStream _os; - std::map _mStatus; - tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback); - } - - void coro_test(HelloCoroPrxCallbackPtr callback,const map& context = TARS_CONTEXT()) - { - tars::TarsOutputStream _os; - std::map _mStatus; - tars_invoke_async(tars::TARSNORMAL,"test", _os, context, _mStatus, callback, true); - } - - tars::Int32 testHello(const std::string & sReq,std::string &sRsp,const map &context = TARS_CONTEXT(),map * pResponseContext = NULL) - { - tars::TarsOutputStream _os; - _os.write(sReq, 1); - _os.write(sRsp, 2); - std::map _mStatus; - shared_ptr rep = tars_invoke(tars::TARSNORMAL,"testHello", _os, context, _mStatus); - if(pResponseContext) - { - pResponseContext->swap(rep->context); - } - - tars::TarsInputStream _is; - _is.setBuffer(rep->sBuffer); - tars::Int32 _ret; - _is.read(_ret, 0, true); - _is.read(sRsp, 2, true); - return _ret; - } - - void async_testHello(HelloPrxCallbackPtr callback,const std::string &sReq,const map& context = TARS_CONTEXT()) - { - tars::TarsOutputStream _os; - _os.write(sReq, 1); - std::map _mStatus; - tars_invoke_async(tars::TARSNORMAL,"testHello", _os, context, _mStatus, callback); - } - - void coro_testHello(HelloCoroPrxCallbackPtr callback,const std::string &sReq,const map& context = TARS_CONTEXT()) - { - tars::TarsOutputStream _os; - _os.write(sReq, 1); - std::map _mStatus; - tars_invoke_async(tars::TARSNORMAL,"testHello", _os, context, _mStatus, callback, true); - } - - HelloProxy* tars_hash(int64_t key) - { - return (HelloProxy*)ServantProxy::tars_hash(key); - } - - HelloProxy* tars_consistent_hash(int64_t key) - { - return (HelloProxy*)ServantProxy::tars_consistent_hash(key); - } - - HelloProxy* tars_set_timeout(int msecond) - { - return (HelloProxy*)ServantProxy::tars_set_timeout(msecond); - } - - static const char* tars_prxname() { return "HelloProxy"; } - }; - typedef tars::TC_AutoPtr HelloPrx; - - /* servant for server */ - class Hello : public tars::Servant - { - public: - virtual ~Hello(){} - virtual tars::Int32 test(tars::TarsCurrentPtr current) = 0; - static void async_response_test(tars::TarsCurrentPtr current, tars::Int32 _ret) - { - if (current->getRequestVersion() == TUPVERSION ) - { - UniAttribute tarsAttr; - tarsAttr.setVersion(current->getRequestVersion()); - tarsAttr.put("", _ret); - - vector sTupResponseBuffer; - tarsAttr.encode(sTupResponseBuffer); - current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); - } - else - { - tars::TarsOutputStream _os; - _os.write(_ret, 0); - - current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); - } - } - - virtual tars::Int32 testHello(const std::string & sReq,std::string &sRsp,tars::TarsCurrentPtr current) = 0; - static void async_response_testHello(tars::TarsCurrentPtr current, tars::Int32 _ret, const std::string &sRsp) - { - if (current->getRequestVersion() == TUPVERSION ) - { - UniAttribute tarsAttr; - tarsAttr.setVersion(current->getRequestVersion()); - tarsAttr.put("", _ret); - tarsAttr.put("sRsp", sRsp); - - vector sTupResponseBuffer; - tarsAttr.encode(sTupResponseBuffer); - current->sendResponse(tars::TARSSERVERSUCCESS, sTupResponseBuffer); - } - else - { - tars::TarsOutputStream _os; - _os.write(_ret, 0); - - _os.write(sRsp, 2); - - current->sendResponse(tars::TARSSERVERSUCCESS, _os.getByteBuffer()); - } - } - - public: - int onDispatch(tars::TarsCurrentPtr _current, vector &_sResponseBuffer) - { - static ::std::string __TestApp__Hello_all[]= - { - "test", - "testHello" - }; - - pair r = equal_range(__TestApp__Hello_all, __TestApp__Hello_all+2, _current->getFuncName()); - if(r.first == r.second) return tars::TARSSERVERNOFUNCERR; - switch(r.first - __TestApp__Hello_all) - { - case 0: - { - tars::TarsInputStream _is; - _is.setBuffer(_current->getRequestBuffer()); - if (_current->getRequestVersion() == TUPVERSION) - { - UniAttribute tarsAttr; - tarsAttr.setVersion(_current->getRequestVersion()); - tarsAttr.decode(_current->getRequestBuffer()); - } - else - { - } - tars::Int32 _ret = test(_current); - if(_current->isResponse()) - { - if (_current->getRequestVersion() == TUPVERSION ) - { - UniAttribute tarsAttr; - tarsAttr.setVersion(_current->getRequestVersion()); - tarsAttr.put("", _ret); - tarsAttr.encode(_sResponseBuffer); - } - else - { - tars::TarsOutputStream _os; - _os.write(_ret, 0); - _os.swap(_sResponseBuffer); - } - } - return tars::TARSSERVERSUCCESS; - - } - case 1: - { - tars::TarsInputStream _is; - _is.setBuffer(_current->getRequestBuffer()); - std::string sReq; - std::string sRsp; - if (_current->getRequestVersion() == TUPVERSION) - { - UniAttribute tarsAttr; - tarsAttr.setVersion(_current->getRequestVersion()); - tarsAttr.decode(_current->getRequestBuffer()); - tarsAttr.get("sReq", sReq); - tarsAttr.getByDefault("sRsp", sRsp, sRsp); - } - else - { - _is.read(sReq, 1, true); - _is.read(sRsp, 2, false); - } - tars::Int32 _ret = testHello(sReq,sRsp, _current); - if(_current->isResponse()) - { - if (_current->getRequestVersion() == TUPVERSION ) - { - UniAttribute tarsAttr; - tarsAttr.setVersion(_current->getRequestVersion()); - tarsAttr.put("", _ret); - tarsAttr.put("sRsp", sRsp); - tarsAttr.encode(_sResponseBuffer); - } - else - { - tars::TarsOutputStream _os; - _os.write(_ret, 0); - _os.write(sRsp, 2); - _os.swap(_sResponseBuffer); - } - } - return tars::TARSSERVERSUCCESS; - - } - } - return tars::TARSSERVERNOFUNCERR; - } - }; - - -} - - - -#endif diff --git a/examples/UtilDemo/demo-util/test_tc_file.cpp b/examples/UtilDemo/demo-util/test_tc_file.cpp index 4637838..08d1e1b 100644 --- a/examples/UtilDemo/demo-util/test_tc_file.cpp +++ b/examples/UtilDemo/demo-util/test_tc_file.cpp @@ -1,4 +1,4 @@ -// +// // Created by jarod on 2020/2/20. // @@ -56,7 +56,38 @@ void Test_TC_File::test() cout << "simplifyDirectory:" << TC_File::simplifyDirectory("/.") << endl; cout << "simplifyDirectory:" << TC_File::simplifyDirectory("/./ab/tt//t///t//../tt/") << endl; + assert(TC_File::simplifyDirectory("/./ab/tt//t///t//../tt/") == "ab\\tt\\t\\tt"); #if TARGET_PLATFORM_WINDOWS cout << "simplifyDirectory:" << TC_File::simplifyDirectory("c:/ab/tt//t///t//../tt/") << endl; + assert(TC_File::simplifyDirectory("c:/ab/tt//t///t//../tt/") == "c:\\ab\\tt\\t\\tt"); #endif + + assert(TC_File::extractFileExt("/usr/local/app/bin.exe") == "exe"); + assert(TC_File::extractFileExt("/usr/local/app/bin") == ""); + assert(TC_File::extractFileExt("/usr/local/app.dir/bin.exe") == "exe"); + assert(TC_File::extractFileExt("c:\\usr\\local\\app.dir\\bin.exe") == "exe"); + assert(TC_File::extractFileExt("c:\\usr\\local\\app.dir\\bin") == ""); + + assert(TC_File::extractFileName("/usr/local/app/bin.exe") == "bin.exe"); + assert(TC_File::extractFileName("/usr/local/app/bin") == "bin"); + assert(TC_File::extractFileName("/usr/local/app.dir/bin.exe") == "bin.exe"); + assert(TC_File::extractFileName("c:\\usr\\local\\app.dir\\bin.exe") == "bin.exe"); + assert(TC_File::extractFileName("c:\\usr\\local\\app.dir\\bin") == "bin"); + assert(TC_File::extractFileName("bin.exe") == "bin.exe"); + + assert(TC_File::extractFilePath("/usr/local/app/bin.exe") == "/usr/local/app/"); + assert(TC_File::extractFilePath("/usr/local/app/bin") == "/usr/local/app/"); + assert(TC_File::extractFilePath("/usr/local/app.dir/bin.exe") == "/usr/local/app.dir/"); + assert(TC_File::extractFilePath("c:\\usr\\local\\app.dir\\bin.exe") == "c:\\usr\\local\\app.dir\\"); + assert(TC_File::extractFilePath("c:\\usr\\local\\app.dir\\bin") == "c:\\usr\\local\\app.dir\\"); + assert(TC_File::extractFilePath("temp.gif") == string(".") + FILE_SEP); + + assert(TC_File::excludeFileExt("/usr/local/app/bin.exe") == "/usr/local/app/bin"); + + assert(TC_File::excludeFileExt("/usr/local/app/bin") == "/usr/local/app/bin"); + assert(TC_File::excludeFileExt("/usr/local/app.dir/bin.exe") == "/usr/local/app.dir/bin"); + assert(TC_File::excludeFileExt("c:\\usr\\local\\app.dir\\bin.exe") == "c:\\usr\\local\\app.dir\\bin"); + assert(TC_File::excludeFileExt("c:\\usr\\local\\app.dir\\bin") == "c:\\usr\\local\\app.dir\\bin"); + assert(TC_File::excludeFileExt("temp.gif") == "temp"); + } diff --git a/servant/script/cmake_http_server.bat b/servant/script/cmake_http_server.bat new file mode 100644 index 0000000..d563520 --- /dev/null +++ b/servant/script/cmake_http_server.bat @@ -0,0 +1 @@ +c:\\tars\\cpp\\script\\busybox.exe bash c:\\tars\\cpp\\script\\cmake_http_server.sh %1 %2 %3 c:/tars \ No newline at end of file diff --git a/servant/script/cmake_tars_server.bat b/servant/script/cmake_tars_server.bat new file mode 100644 index 0000000..d5d9165 --- /dev/null +++ b/servant/script/cmake_tars_server.bat @@ -0,0 +1 @@ +c:\\tars\\cpp\\script\\busybox.exe bash c:\\tars\\cpp\\script\\cmake_tars_server.sh %1 %2 %3 c:/tars diff --git a/servant/tup/Tars.h b/servant/tup/Tars.h index 981ecad..cc3c11d 100755 --- a/servant/tup/Tars.h +++ b/servant/tup/Tars.h @@ -1965,4 +1965,4 @@ namespace tars #include "tup/TarsDisplayer.h" #endif -#endif +#endif \ No newline at end of file diff --git a/servant/tup/TarsJson.h b/servant/tup/TarsJson.h index 9304fae..009f51a 100644 --- a/servant/tup/TarsJson.h +++ b/servant/tup/TarsJson.h @@ -8,7 +8,7 @@ #include #include #include - +#include "tup/TarsType.h" #include "util/tc_json.h" #include "util/tc_common.h" @@ -17,7 +17,7 @@ namespace tars class JsonInput { public: - static void readJson(Bool& b, const JsonValuePtr & p, bool isRequire = true) + static void readJson(tars::Bool& b, const JsonValuePtr & p, bool isRequire = true) { if(NULL != p.get() && p->getType() == eJsonTypeBoolean) { diff --git a/servant/tup/TarsType.h b/servant/tup/TarsType.h index 26738f6..a58f3a3 100644 --- a/servant/tup/TarsType.h +++ b/servant/tup/TarsType.h @@ -1,4 +1,4 @@ -/** +/** * Tencent is pleased to support the open source community by making Tars available. * * Copyright (C) 2016THL A29 Limited, a Tencent company. All rights reserved. @@ -43,6 +43,7 @@ typedef short Short; typedef float Float; typedef double Double; typedef int Int32; +struct TarsStructBase; typedef unsigned char UInt8; typedef unsigned short UInt16; diff --git a/util/include/util/tc_port.h b/util/include/util/tc_port.h index 4500ac7..ddfc481 100755 --- a/util/include/util/tc_port.h +++ b/util/include/util/tc_port.h @@ -62,6 +62,10 @@ public: static int closeSocket(int fd); static int64_t getpid(); + + static std::string getEnv(const std::string &name); + + static void setEnv(const std::string &name, const std::string &value); }; } diff --git a/util/src/tc_ex.cpp b/util/src/tc_ex.cpp index 78f63ba..5cd8c81 100644 --- a/util/src/tc_ex.cpp +++ b/util/src/tc_ex.cpp @@ -95,7 +95,7 @@ string TC_Exception::parseError(int err) FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, - NULL, err, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), + NULL, err, MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US), (LPTSTR) & lpMsgBuf, 0, NULL); // errMsg = Unicode2ANSI((LPCWSTR)lpMsgBuf); diff --git a/util/src/tc_file.cpp b/util/src/tc_file.cpp index aff02df..fd1568f 100644 --- a/util/src/tc_file.cpp +++ b/util/src/tc_file.cpp @@ -292,26 +292,28 @@ string TC_File::simplifyDirectory(const string& path) result = TC_Common::replace(result, "\\", "/"); #endif + string sep(FILE_SEP); + string::size_type pos; pos = 0; - while((pos = result.find(string(FILE_SEP) + FILE_SEP, pos)) != string::npos) + while((pos = result.find(sep + FILE_SEP, pos)) != string::npos) { result.erase(pos, 1); } pos = 0; - while((pos = result.find(string(FILE_SEP) + "." + FILE_SEP, pos)) != string::npos) + while((pos = result.find(sep+ "." + FILE_SEP, pos)) != string::npos) { result.erase(pos, 2); } - while(result.substr(0, 4) == string(FILE_SEP) + ".." + FILE_SEP) + while(result.substr(0, 4) == sep + ".." + FILE_SEP) { result.erase(0, 3); } - if(result.find(string(FILE_SEP) + ".." + FILE_SEP) != string::npos) + if(result.find(sep + ".." + FILE_SEP) != string::npos) { bool ab = TC_File::isAbsolute(result); @@ -350,12 +352,12 @@ string TC_File::simplifyDirectory(const string& path) #endif } - if(result == string(FILE_SEP) + ".") + if(result == sep + ".") { return result.substr(0, result.size() - 1); } - if(result.size() >= 2 && result.substr(result.size() - 2, 2) == string(FILE_SEP) + ".") + if(result.size() >= 2 && result.substr(result.size() - 2, 2) == sep + ".") { result.erase(result.size() - 2, 2); } @@ -370,7 +372,7 @@ string TC_File::simplifyDirectory(const string& path) result.erase(result.size() - 1); } - if(result == string(FILE_SEP) + "..") + if(result == sep + "..") { result = FILE_SEP; } @@ -458,61 +460,97 @@ string TC_File::extractFileName(const string &sFullFileName) return ""; } - string::size_type pos = sFullFileName.rfind(FILE_SEP); - if(pos == string::npos) + string::size_type found = sFullFileName.find_last_of("/\\"); + + // string::size_type pos = sFullFileName.rfind(FILE_SEP); + if(found == string::npos) { return sFullFileName; } - return sFullFileName.substr(pos + 1); + return sFullFileName.substr(found + 1); } string TC_File::extractFilePath(const string &sFullFileName) { -#if TARGET_PLATFORM_WINDOWS - string sFullFileNameTmp = TC_Common::replace(sFullFileName, "/", "\\"); -#else - string sFullFileNameTmp = TC_Common::replace(sFullFileName, "\\", "/"); -#endif +// #if TARGET_PLATFORM_WINDOWS +// string sFullFileNameTmp = TC_Common::replace(sFullFileName, "/", "\\"); +// #else +// string sFullFileNameTmp = TC_Common::replace(sFullFileName, "\\", "/"); +// #endif - if (sFullFileNameTmp.length() <= 0) + if (sFullFileName.length() <= 0) { return string(".") + FILE_SEP; } - string::size_type pos = 0; - - for (pos = sFullFileNameTmp.length(); pos != 0; --pos) + string::size_type found = sFullFileName.find_last_of("/\\"); + if(found == string::npos) { - if (sFullFileNameTmp[pos - 1] == FILE_SEP[0]) - { - return sFullFileNameTmp.substr(0, pos); - } + return string(".") + FILE_SEP; } - return string(".") + FILE_SEP; + return sFullFileName.substr(0, found+1); + + // string::size_type pos = 0; + + // for (pos = sFullFileNameTmp.length(); pos != 0; --pos) + // { + // if (sFullFileNameTmp[pos - 1] == FILE_SEP[0]) + // { + // return sFullFileNameTmp.substr(0, pos); + // } + // } + + // return string(".") + FILE_SEP; } string TC_File::extractFileExt(const string &sFullFileName) { - string::size_type pos; - if ((pos = sFullFileName.rfind('.')) == string::npos) + string::size_type found = sFullFileName.find_last_of("/\\"); + if(found == string::npos) { - return string(""); - } + if ((found = sFullFileName.rfind('.')) == string::npos) + { + return string(""); + } - return sFullFileName.substr(pos+1); + return sFullFileName.substr(found+1); + } + else + { + for(string::size_type i = sFullFileName.size()-1; i > found; i--) + { + if(sFullFileName[i] == '.') + { + return sFullFileName.substr(i+1); + } + } + + return ""; + } } string TC_File::excludeFileExt(const string &sFullFileName) { - string::size_type pos; - if ((pos = sFullFileName.rfind('.')) == string::npos) + string::size_type found = sFullFileName.find_last_of("./\\"); + if(found != string::npos) { - return sFullFileName; + if(sFullFileName[found] == '.') + { + return sFullFileName.substr(0, found); + } } - return sFullFileName.substr(0, pos); + return sFullFileName; + + // string::size_type pos; + // if ((pos = sFullFileName.rfind('.')) == string::npos) + // { + // return sFullFileName; + // } + + // return sFullFileName.substr(0, pos); } string TC_File::replaceFileExt(const string &sFullFileName, const string &sExt) diff --git a/util/src/tc_port.cpp b/util/src/tc_port.cpp index bed3025..2473ca6 100755 --- a/util/src/tc_port.cpp +++ b/util/src/tc_port.cpp @@ -28,6 +28,8 @@ #include "util/tc_strptime.h" #endif +using namespace std; + namespace tars { int TC_Port::strcasecmp(const char *s1, const char *s2) @@ -171,4 +173,20 @@ int64_t TC_Port::getpid() return pid; } +string TC_Port::getEnv(const string &name) +{ + char* p = getenv(name.c_str()); + string str = p ? string(p) : ""; + + return str; +} + +void TC_Port::setEnv(const string &name, const string &value) +{ +#if TARGET_PLATFORM_WINDOWS + SetEnvironmentVariable(name.c_str(), value.c_str()); +#else + setenv(name.c_str(), value.c_str(), true); +#endif +} } \ No newline at end of file diff --git a/util/src/tc_socket.cpp b/util/src/tc_socket.cpp index 7578995..5c68ada 100755 --- a/util/src/tc_socket.cpp +++ b/util/src/tc_socket.cpp @@ -209,7 +209,11 @@ SOCKET_TYPE TC_Socket::accept(TC_Socket &tcSock, struct sockaddr *pstSockAddr, S SOCKET_TYPE ifd; +#if TARGET_PLATFORM_WINDOWS + ifd = ::accept(_sock, pstSockAddr, &iSockLen); +#else while ((ifd = ::accept(_sock, pstSockAddr, &iSockLen)) < 0 && errno == EINTR); +#endif tcSock._sock = ifd; tcSock._iDomain = _iDomain;