Simplify OS detect Macros, add "mkdir()" function to Utils.

1.3
Micooz 10 years ago
parent 0f55715713
commit 8e4ac7db5e

@ -5,6 +5,14 @@
#include <string>
#include <vector>
#if defined(WIN32) || defined(_WIN32)
#define OS_WINDOWS
#elif defined(__APPLE__) || defined(APPLE)
#define OS_UNIX
#elif defined(__linux__) || defined(linux)
#define OS_LINUX
#endif
#ifdef min
#undef min
#endif
@ -89,6 +97,10 @@ class Utils {
return prevCol[len2];
}
/*
* Create multi-level directories by given folder.
*/
static bool mkdir(const std::string folder);
};

@ -5,13 +5,12 @@
#include <ctime>
#include "easypr/plate_judge.h"
#include "easypr/plate_locate.h"
#include "easypr/util.h"
#if defined (WIN32) || defined (_WIN32)
#ifdef OS_WINDOWS
#include <io.h>
#endif
#include "easypr/util.h"
namespace easypr {
namespace preprocess {

@ -1,11 +1,10 @@
// ann_train.cpp : ann模型的训练文件主要用在OCR中
#include <opencv/cv.h>
#include <opencv/cvaux.h>
#include "opencv2/ml/ml.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <opencv2/opencv.hpp>
#include "easypr/util.h"
#include "easypr/plate_recognize.h"
#if defined (WIN32) || defined (_WIN32)
#ifdef OS_WINDOWS
#include <windows.h>
#endif
@ -13,15 +12,10 @@
#include <iostream>
#include <cstdlib>
#if defined (WIN32) || defined (_WIN32)
#ifdef OS_WINDOWS
#include <io.h>
#elif defined (linux) || defined (__linux__)
//#include <sys/io.h>
#endif
#include "easypr/plate_recognize.h"
#include "easypr/util.h"
using namespace easypr;
using namespace cv;
using namespace std;

@ -1,17 +1,21 @@
#include "easypr/util.h"
#if defined(WIN32) || defined(_WIN32)
#ifdef OS_WINDOWS
#include <windows.h>
#include <direct.h>
#include <io.h>
#elif defined(linux) || defined(__linux__) || defined(__APPLE__)
#define PATH_DELIMITER '\\'
#elif defined(OS_LINUX) || defined(OS_UNIX)
#include <cstring>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#define PATH_DELIMITER '/'
#endif
#if defined(__APPLE__)
#ifdef OS_UNIX
#include <sys/timeb.h>
@ -23,11 +27,11 @@ using namespace std;
using namespace easypr;
long Utils::getTimestamp() {
#if defined(WIN32) || defined(_WIN32)
#ifdef OS_WINDOWS
return GetTickCount();
#endif
#if (linux) || defined(__linux__)
#ifdef OS_LINUX
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
@ -35,7 +39,7 @@ long Utils::getTimestamp() {
return (ts.tv_sec * 1e3 + ts.tv_nsec / 1e6);
#endif
#if defined(__APPLE__)
#ifdef OS_UNIX
// there is no function provided by osx to get system tick count.
// but considering the purpose by using this function,
// we can simply return a millisecond since 1970/1/1 to calc the time elapse.
@ -48,7 +52,7 @@ long Utils::getTimestamp() {
std::string Utils::getFileName(const string& path,
const bool postfix /* = false */) {
if (!path.empty()) {
#if defined(WIN32) || defined(_WIN32)
#ifdef OS_WINDOWS
size_t last_slash_1 = path.find_last_of("\\");
size_t last_slash_2 = path.find_last_of("/");
size_t last_slash;
@ -110,7 +114,7 @@ vector<string> Utils::getFiles(const string& folder,
vector<string> files;
list<string> subfolders;
subfolders.push_back(folder);
#if defined(WIN32) || defined(_WIN32)
#ifdef OS_WINDOWS
while (!subfolders.empty()) {
string current_folder(subfolders.back());
@ -156,7 +160,7 @@ vector<string> Utils::getFiles(const string& folder,
} // while
_findclose(file_handler);
}
#elif defined(linux) || defined(__linux__) || defined(__APPLE__)
#elif defined(OS_LINUX) || defined(OS_UNIX)
while (!subfolders.empty()) {
string current_folder(subfolders.back());
@ -214,7 +218,36 @@ vector<string> Utils::getFiles(const string& folder,
} // while
closedir(pdir);
}
#endif
return files;
}
bool Utils::mkdir(const std::string folder) {
std::string folder_builder;
std::string sub;
sub.reserve(folder.size());
for (auto it = folder.begin(); it != folder.end(); ++it) {
const char c = *it;
sub.push_back(c);
if (c == PATH_DELIMITER || it == folder.end() - 1) {
folder_builder.append(sub);
#ifdef OS_WINDOWS
if (0 != ::_access(folder_builder.c_str(), 0)) {
#else
if (0 != ::access(folder_builder.c_str(), 0)) {
#endif
// this folder not exist
#ifdef OS_WINDOWS
if (0 != ::_mkdir(folder_builder.c_str())) {
#else
if (0 != ::mkdir(folder_builder.c_str(), S_IRWXU)) {
#endif
// create failed
return false;
}
}
sub.clear();
}
}
return true;
}

Loading…
Cancel
Save