Skip to content

Instantly share code, notes, and snippets.

@chintak
Last active August 29, 2015 14:18
Show Gist options
  • Save chintak/811f91187fd02da45f19 to your computer and use it in GitHub Desktop.
Save chintak/811f91187fd02da45f19 to your computer and use it in GitHub Desktop.
C++ quick functions
/*
================================================================
GIST FOR C++ FUNCTIONS
================================================================
*/
// Efficient way for looping a cv::Mat
uchar* p;
for (uint i = 0; i < (uint) mat.rows; i++)
{
p = mat.ptr<uchar> (i);
for (uint j = 0; j < (uint) mat.cols; j++)
{
//
}
}
// Running a system command and getting the output as a string
std::string exec(const char* cmd) {
FILE* pipe = popen(cmd, "r");
if (!pipe) return "ERROR";
char buffer[128];
std::string result = "";
while(!feof(pipe)) {
if(fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
pclose(pipe);
return result;
}
// Print a vector
template <class T>
void printVec(vector<T> const &myvec){
typename vector<T>::const_iterator it;
unsigned int len = myvec.size();
if (len==0){
printf("Vector is empty\n");
}
else
{
for (it = myvec.begin(); it != myvec.end(); it++)
{
cout << " " << *it;
}
printf("\n");
}
}
// Move file into a sub folder and return the final name
void moveFile (const std::string &f, const std::string &d, std::string &dest)
{
if (!is_file_exist(f))
return;
std::stringstream com;
std::string dir = f.substr (0, f.find_last_of ('/') + 1);
std::string name = f.substr (f.find_last_of ('/')+1);
com.str(std::string());
com.clear();
com << "mkdir -p " << dir << "/" << d << "/";
if (system (com.str ().c_str ()));
com.str(std::string());
com.clear();
com << "mv " << f << ' ' << dir << '/' << d << '/' << name << "";
if (system (com.str ().c_str ()));
dest = dir + d + "/" + name;
}
// Trim leading and trailing white space
std::string trim(const std::string& str,
const std::string& whitespace)
{
const auto strBegin = str.find_first_not_of(whitespace);
if (strBegin == std::string::npos)
return ""; // no content
const auto strEnd = str.find_last_not_of(whitespace);
const auto strRange = strEnd - strBegin + 1;
return str.substr(strBegin, strRange);
}
// Replace the filename given the entire file path
std::string replaceFileName(std::string origFilePath, std::string newFile)
{
std::string dir = origFilePath.substr(0, origFilePath.find_last_of('/') +1);
std::string fullPath = dir + newFile;
return fullPath;
}
// Recursively find all files with a given file extension
int getdir (std::string dir, std::vector<std::string> &files, std::string ext)
{
DIR *dp;
struct dirent *dirp;
if((dp = opendir(dir.c_str())) == NULL)
{
std::cout << "Error(" << errno << ") opening "
<< dir << std::endl;
return errno;
}
while ((dirp = readdir(dp)) != NULL)
{
std::string only_name = std::string(dirp->d_name);
std::string name = dir + '/' + only_name;
if (only_name.compare (".") == 0 ||
only_name.compare ("..") == 0)
continue;
struct stat buffer;
stat(name.c_str (), &buffer);
if (S_ISREG (buffer.st_mode) &&
name.substr (name.find_last_of ('.') + 1) == ext)
{
files.push_back(name);
}
else if (S_ISDIR (buffer.st_mode))
{
std::vector<std::string> names;
if (getdir (name, names, ext) == 0)
files.insert (files.end (),
names.begin (),
names.end ());
}
}
closedir(dp);
return 0;
}
bool is_file_empty(std::ifstream& pFile)
{
return pFile.peek() == std::ifstream::traits_type::eof();
}
bool is_file_exist(std::string fileName)
{
std::ifstream infile(fileName.c_str());
return infile.good();
}
std::ifstream::pos_type getFileSize(std::string fileName)
{
std::ifstream infile(fileName, std::ifstream::ate | std::ifstream::binary);
return infile.tellg();
}
void removeLeadingTrailingSpaces(std::string &stripString)
{
while(std::isspace(*stripString.begin()))
stripString.erase(stripString.begin());
while(std::isspace(*stripString.rbegin()))
stripString.erase(stripString.length()-1);
}
std::string convIntToStr(int num, int leadingZeros)
{
std::stringstream ss;
if (leadingZeros >0)
ss << std::setw(leadingZeros) << std::setfill('0') << num;
else
ss << num;
return ss.str();
}
int convStrToInt(std::string str)
{
int retInt;
std::istringstream ss(str);
ss >> retInt;
return retInt;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment