util.cpp 964 字节
Newer Older
H
hypox64 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
#include "util.hpp"
#include <stdio.h>
#include <sys/stat.h>
#include <unistd.h>
#include <iostream>
#include <list>
#include <vector>

namespace util {

void Timer::start() {
    tstart = clock();
}
void Timer::end() {
    tend = clock();
    double dur;
    dur = (double)(tend - tstart);
    std::cout << "Cost Time:" << (dur / CLOCKS_PER_SEC) << "\n";
}

std::string current_path() {
    char* buffer;
    buffer = getcwd(NULL, 0);
    return buffer;
}

std::string pathjoin(const std::list<std::string>& strs) {
    std::string res = "";
    int cnt = 0;
    for (std::string s : strs) {
        if (cnt == 0) {
            res += s;
        } else {
            if (s[0] != '/') {
                res += ("/" + s);
            } else {
                res += s;
            }
        }
        cnt++;
    }
    return res;
}

bool isfile(const std::string& name) {
    struct stat buffer;
    return (stat(name.c_str(), &buffer) == 0);
}
}  // namespace util