-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_fileloader.h
64 lines (59 loc) · 2.03 KB
/
util_fileloader.h
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
55
56
57
58
59
60
61
62
63
64
/*
* --------------------------------------------------------------
* File: util_fileloader.h
* Project: Code
* Created: Thursday, 28th February 2019 10:58:31 am
* @Author: molin@live.cn
* Last Modified: Thursday, 28th February 2019 10:58:33 am
* Copyright © Rockface 2018 - 2019
* --------------------------------------------------------------
*/
#ifndef FILE_LOADER
#define FILE_LOADER
#include <vector>
#include <fstream>
#include "util_type.h"
void load_portreb(std::string path, struct Constraint &constraint, struct asset **outAsset){
std::fstream data(path, std::ios::in);
std::string input_cache;
int line = 0;
if (!data.is_open()){
std::cout<<"CANNOT OPEN FILE\n";
std::cout<<"\t FILE PATH: "<<path<<std::endl;
return;
}
while( std::getline(data, input_cache)){
if(input_cache.length()<=1){
//std::cerr<<("Line: %d\n", line);
//std::cerr<<"Content:\t"<<input_cache<<std::endl;
continue;
}
std::istringstream line_cache(input_cache);
if(line == 0){
line_cache>>
constraint.num_assets>>
constraint.max_assets>>
constraint.transaction_limit>>
constraint.cash_change;
*outAsset = (struct asset*)malloc(constraint.num_assets*sizeof(struct asset));
}
else{
if(line <= constraint.num_assets){
(*outAsset)[line-1].init(line_cache);
}
if(constraint.num_assets<line && line<=(2*constraint.num_assets)){
int tran = line - constraint.num_assets - 1;
line_cache>>(*outAsset)[tran].mean_income>>(*outAsset)[tran].diviation_r;
}
}
line++;
}
}
std::string Trim(std::string& str)
{
//str.find_first_not_of(" \t\r\n"),在字符串str中从索引0开始,返回首次不匹配"\t\r\n"的位置
str.erase(0,str.find_first_not_of(" \t\r\n"));
str.erase(str.find_last_not_of(" \t\r\n") + 1);
return str;
}
#endif