#include #include #include #include #include #include #include #include #include using namespace std; #define MAX_PIC_EXTS 4 static int stringtolower(string& src) { for (unsigned int i = 0; i < src.size(); ++i) { if (src[i] >= 'A' && src[i] <= 'Z') { src[i] += 32; } } return 0; } int get_pic_file(const char* base_path, string& pic_name) { // static set pics; DIR *dir; struct dirent *ptr; const char* pic_exts[MAX_PIC_EXTS] = { ".bmp", ".png", ".jpg", ".jpeg"}; string ext; int i = 0; int ret = 2; if ((dir=opendir(base_path)) == NULL) { printf("[101] Failed to open dir(%s)\n", base_path); return 1; } while ((ptr=readdir(dir)) != NULL) { if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0) continue; else if(ptr->d_type == 8) { pic_name = ptr->d_name; for (i = 0; i < MAX_PIC_EXTS; ++i) { ext = pic_name.substr(pic_name.rfind(".")); // if (pic_name.rfind(pic_exts[i]) != string::npos && pics.find(pic_name) == pics.end()) stringtolower(ext); if (ext.rfind(pic_exts[i]) != string::npos) { break; } } if (i < MAX_PIC_EXTS) { // pics.insert(pic_name); ret = 0; break; } } } closedir(dir); return ret; } bool check_file(const char* file_path) { bool res = false; fstream fin(file_path, ios::in); if (fin) res = true; else res = false; fin.close(); return res; } bool get_file_content(const char* file_path, string& file_content) { file_content = ""; fstream fin(file_path, ios::in); if (!fin) { return false; } fin.seekg(0, fin.end); size_t file_size = fin.tellg(); fin.seekg(0, fin.beg); char* pcontent = new char[file_size + 1]; memset(pcontent, 0, file_size + 1); fin.read(pcontent, file_size); printf("the content of %s is %s\n", file_path, pcontent); file_content = pcontent; delete[] pcontent; fin.close(); return true; }