-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmy_utils.cpp
More file actions
54 lines (44 loc) · 1.49 KB
/
my_utils.cpp
File metadata and controls
54 lines (44 loc) · 1.49 KB
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
50
51
52
53
54
#include "my_utils.h"
#include <cstdio>
#include <fstream>
#include <vector>
#include <codecvt>
#include <locale>
#include <iostream>
#include <iomanip>
#include <locale>
#define PBSTR "||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||"
#define PBWIDTH 60
// https://stackoverflow.com/a/36315819/3396222
void PrintProgressBar(float progress) {
int val = (int) (progress * 100);
int lpad = (int) (progress * PBWIDTH);
int rpad = PBWIDTH - lpad;
printf("\r%3d%% [%.*s%*s]", val, lpad, PBSTR, rpad, "");
fflush(stdout);
}
// https://stackoverflow.com/a/116220/3396222
std::string readFile2(const std::string &fileName)
{
std::ifstream ifs(fileName.c_str(), std::ios::in | std::ios::binary | std::ios::ate);
std::ifstream::pos_type fileSize = ifs.tellg();
ifs.seekg(0, std::ios::beg);
std::vector<char> bytes(fileSize);
ifs.read(bytes.data(), fileSize);
return std::string(bytes.data(), fileSize);
}
// https://stackoverflow.com/a/51356708/3396222
std::wstring widen(const std::string& utf8_string)
{
std::wstring_convert <std::codecvt_utf8_utf16 <wchar_t>> convert;
return convert.from_bytes(utf8_string);
}
std::wstring readFileWideChars(const std::string &fileName) {
time_t start, finish;
std::cout << "Reading file....." << '\n';
std::time(&start);
std::wstring data = widen(readFile2(fileName));
std::time(&finish);
std::cout << "Done in " << std::difftime(finish, start) << " seconds!!" << "\n\n";
return data;
}