usage_tool.cpp
来自「股票分析源代码」· C++ 代码 · 共 966 行 · 第 1/2 页
CPP
966 行
#include "hbstock2/hbstock_config.h"
#ifdef _WIN32_
#include <windows.h>
#include <winbase.h>
#define dll_suffix ".dll"
#define exe_separator ";"
#else
#define dll_suffix ".so"
#define exe_separator ":"
#endif
#define CR 10
#define CR_CIN 13
#ifdef _MINGW32_
#include <conio.h>
#else
#include <sys/termios.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#endif
#include <iostream>
#include <cstring>
#include <ctype.h>
#include <ace/Dirent.h>
#include "gnu/usage_tool.h"
#include "gnu/md5.h"
/* must be at the end of include */
#include "gnu/i18n_debug.h"
using namespace std;
using namespace gnu;
// static var and function
static const char* LOGFILE = gnu::FILENAME(__FILE__);
static bool check_split_string(const string& str_file,const char* p_chr_pattern,
const char* p_file_prefix );
#ifdef _MINGW32_
static std::string get_passwd_win(const char* p_ch_prompt);
#else
static struct termio tty_mode;
static int tty_raw(int fd);
static int tty_reset(int fd);
static std::string get_passwd_unix(const char* p_ch_prompt);
#endif
bool gnu::get_dso_file(std::vector<std::string>& vect_dso_file,
const std::string& str_dir,const char* p_file_prefix)
{
bool isTrue = true;
ACE_Dirent dir;
int ret = dir.open(str_dir.c_str());
if (ret != 0)
{
//I18N_ERROR(_("%s [%s:%l] open dir %s error\n"),str_dir.c_str());
isTrue = false;
return isTrue;
}
dirent* dp = 0 ;
string str_dso_file;
string::size_type sz_ret = 0 ;
while ((dp = dir.read()) != NULL)
{
//I18N_DEBUG(_("%s [%s:%l] file : %s\n"),dp->d_name);
str_dso_file = dp->d_name;
sz_ret = str_dso_file.rfind(dll_suffix);
if ( sz_ret < string::npos)
{
if (p_file_prefix == NULL)
{
vect_dso_file.push_back(str_dir+ "/" + str_dso_file);
} else
{
if((sz_ret=str_dso_file.find(p_file_prefix)) < string::npos)
{
vect_dso_file.push_back(str_dir+ "/" + str_dso_file);
}
} // if (p_file_prefix == NULL)
} //if ( sz_ret < string::npos)
} // while ((dp = dir.read()) != NULL)
return isTrue;
}
std::string gnu::get_program_path_filename()
{
std::string str_path;
const int MAX_PATH_LEN = 256;
char location[MAX_PATH_LEN+1];
#ifdef _WIN32_
GetModuleFileName(NULL, location, sizeof (location));
#else
GetModuleFileName_Linux(NULL, location, sizeof (location));
#endif
transform_path_to_unix(location);
str_path = location;
return str_path;
}
void gnu::transform_path_to_unix(char* p_chr_path)
{
string str_path = p_chr_path;
#ifdef _CYGWIN_
string str_replace_ch = "/cygdrive/";
if (p_chr_path[1] == ':')
{
str_replace_ch = str_replace_ch + (char)tolower(p_chr_path[0]);
str_path.replace(0,2,str_replace_ch);
}
#endif
string::iterator iter;
for (iter=str_path.begin();iter!=str_path.end();iter++)
{
if (*iter == '\\')
{
*iter = '/';
}
}
strcpy(p_chr_path,str_path.c_str());
}
int gnu::GetModuleFileName_Linux( char* sModuleName, char* sFileName, int nSize)
{
int ret = -1;
char* p = getenv("_");
if( p != NULL )
{
if (sModuleName == NULL || (sModuleName != NULL && strstr( p, sModuleName ) != NULL))
{
ret = 0;
strcpy( sFileName, p );
} else
{
ret = -1;
strcpy( sFileName, p );
}
}
return ret;
}
bool gnu::get_files_in_dir(std::vector<std::string>& vect_file,
const std::string& str_dir,const char* p_file_prefix ,
const char* p_file_suffix)
{
bool isTrue = true;
ACE_Dirent dir;
int ret = dir.open(str_dir.c_str());
if (ret != 0)
{
//I18N_ERROR(_("%s [%s:%l] open dir %s error\n"),str_dir.c_str());
isTrue = false;
return isTrue;
}
dirent* dp = 0 ;
string str_file;
const char* p_chr_split = ",";
const char* p_chr_pattern = "*";
bool is_contain = false;
string::size_type sz_ret = 0 ;
std::vector<std::string> vect_str_split;
while ((dp = dir.read()) != NULL)
{
//I18N_DEBUG(_("%s [%s:%l] file : %s\n"),dp->d_name);
str_file = dp->d_name;
if (p_file_suffix != NULL)
{
sz_ret = str_file.rfind(p_file_suffix);
if ( sz_ret == string::npos)
{
continue;
}
} // if (p_file_suffix != NULL)
if (p_file_prefix == NULL || strlen(p_file_prefix) == 0)
{
vect_file.push_back(str_file);
} else
{
str_split(p_file_prefix,p_chr_split,vect_str_split);
for (unsigned int i=0;i<vect_str_split.size();i++)
{
is_contain = check_split_string(str_file,p_chr_pattern,vect_str_split[i].c_str());
if (is_contain)
{
//I18N_DEBUG(_("%s [%s:%l] add file : %s\n"),str_file.c_str());
vect_file.push_back(str_file);
break;
}
}
} // if (p_file_prefix == NULL)
} // while ((dp = dir.read()) != NULL)
return isTrue;
}
bool check_split_string(const string& str_file_in,const char* p_chr_pattern,const char* p_file_prefix )
{
std::vector<std::string> vect_str;
bool is_contain = false;
string::size_type sz_ret = 0 ;
string::size_type sz_ret_in =0;
std::string str_file_prefix;
std::string str_file;
std::vector<std::string> vect_file_prefix;
str_file = str_toupper(str_file_in);
// get file before .
str_split(str_file.c_str(),".",vect_file_prefix);
if (vect_file_prefix.size()>0)
{
str_file_prefix = vect_file_prefix[0];
}
vect_file_prefix.clear();
str_split(p_file_prefix,p_chr_pattern,vect_str);
if (vect_str.size()==1 && str_toupper(vect_str[0]) == str_toupper(p_file_prefix)) // no *
{
//I18N_DEBUG(_("%s [%s:%l] str_file_prefix %s : %s \n"),str_file_prefix.c_str(),p_file_prefix);
if(str_file_prefix == vect_str[0])
{
is_contain = true;
}
} else
{
sz_ret_in = 0;
sz_ret = 0;
is_contain = true;
//I18N_DEBUG(_("%s [%s:%l] vect_str.size : %d\n"),vect_str.size());
for (unsigned int i=0;i<vect_str.size();i++)
{
if (i == 0)
{
if (p_file_prefix[0] == '*')
{
if ((sz_ret=str_file.find(str_toupper(vect_str[i]),sz_ret_in)) < string::npos)
{
sz_ret_in = sz_ret + vect_str[i].size();
is_contain = true;
} else
{
is_contain = false;
break;
}
} else
{
if (str_toupper(vect_str[i]) == str_file.substr(0,vect_str[i].size()))
{
sz_ret_in = sz_ret + vect_str[i].size();
is_contain = true;
} else
{
is_contain = false;
break;
}
} //if (p_file_prefix[0] == '*')
} else if (i == vect_str.size()-1)
{
if (p_file_prefix[strlen(p_file_prefix)-1] == '*')
{
if ((sz_ret=str_file.find(str_toupper(vect_str[i]),sz_ret_in)) < string::npos)
{
sz_ret_in = sz_ret + vect_str[i].size();
is_contain = true;
} else
{
is_contain = false;
break;
}
} else
{
//I18N_DEBUG(_("%s [%s:%l] %s : %s\n"),vect_str[i].c_str(),
// str_file_prefix.substr((str_file_prefix.size()-vect_str[i].size())).c_str());
if (str_toupper(vect_str[i]) == str_file_prefix.substr((str_file_prefix.size()-vect_str[i].size())))
{
sz_ret_in = sz_ret + vect_str[i].size();
is_contain = true;
} else
{
is_contain = false;
break;
}
} //if (p_file_prefix[strlen(p_file_prefix)-1] == '*')
} else
{
if ((sz_ret=str_file.find(str_toupper(vect_str[i]),sz_ret_in)) < string::npos)
{
sz_ret_in = sz_ret + vect_str[i].size();
is_contain = true;
} else
{
is_contain = false;
break;
}
} // if (i == 0)
} // for (unsigned int i=0;i<vect_str.size();i++)
} //if (vect_str.size()==1 && vect_str[0] == string(p_file_prefix))
vect_str.clear();
return is_contain;
}
bool gnu::split_path_file(const std::string& str_path_file,
std::string& str_path,std::string& str_filename)
{
bool isTrue = true;
string::size_type sz_ret = 0 ;
string::size_type sz_ret_b = 0 ;
sz_ret = str_path_file.rfind("/");
sz_ret_b = str_path_file.rfind("\\");
if (sz_ret_b < sz_ret)
{
sz_ret = sz_ret_b;
}
if ( sz_ret < string::npos)
{
str_path = str_path_file.substr(0,sz_ret);
str_filename = str_path_file.substr(sz_ret+1);
} else
{
str_filename = str_path_file;
isTrue = false;
}
return isTrue;
}
void gnu::file_copy(FILE* ifp,FILE* ofp)
{
int c = 0;
while ((c = getc(ifp)) != EOF)
{
putc(c,ofp);
}
}
int gnu::getline(std::string& str_line,FILE* fp)
{
int c;
int ret = 0 ;
str_line.clear();
while ((c = getc(fp)) != EOF && c != '\n')
{
ret++;
str_line += c ;
}
if (c == EOF)
{
ret = -1;
}
return ret;
}
std::string gnu::str_toupper(const std::string& str)
{
std::string str_out ;
str_out = str;
string::iterator iter;
for (iter=str_out.begin();iter!=str_out.end();iter++)
{
*iter = toupper(*iter);
}
return str_out;
}
std::string& gnu::str_toupper(std::string& str)
{
string::iterator iter;
for (iter=str.begin();iter!=str.end();iter++)
{
*iter = toupper(*iter);
}
return str;
}
std::string gnu::str_tolower(const std::string& str)
{
std::string str_out ;
str_out = str;
string::iterator iter;
for (iter=str_out.begin();iter!=str_out.end();iter++)
{
*iter = tolower(*iter);
}
return str_out;
}
std::string& gnu::str_tolower(std::string& str)
{
string::iterator iter;
for (iter=str.begin();iter!=str.end();iter++)
{
*iter = tolower(*iter);
}
return str;
}
std::string gnu::str_trim(const std::string& str)
{
std::string str_out ;
str_out = str;
int i_pos = 0;
string::iterator iter;
for (iter=str_out.begin();iter!=str_out.end();iter++)
{
if (!isspace(*iter))
{
break;
}
i_pos ++ ;
}
if (i_pos>0)
{
str_out.erase(str_out.begin(),iter);
}
i_pos = 0;
string::reverse_iterator rev_iter;
for (rev_iter=str_out.rbegin();rev_iter!=str_out.rend();rev_iter++)
{
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?