Boost 範例程式:共享記憶體

主程式讀取 CSV 檔案 共享記憶體 搜尋資料 Python 版本

  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
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#include <iostream>
#include <iomanip>
#include <string>
#include <math.h>
#include <chrono>
#include <thread>

#include "TCSV.h"
#include "TMEMFile.h"

using namespace std;
using namespace Emprogria;

class Stock {
private:
	typedef struct {
		unsigned char date[10];
		float stockOpen, stockHigh, stockLow, stockClose, stockVol,
				stockAdjClose;
	} MEM_STOCK;

	MEM_STOCK Output(FIELDS fields) {
		MEM_STOCK data;

		// 更換日期格式
		std::string newDate = boost::replace_all_copy(fields.at(0), "/", "-");
		std::cout << newDate << '\t';

		std::memcpy(data.date, fields.at(0).c_str(),
				sizeof(char) * fields.at(0).size());

		for (unsigned i = 1; i < fields.size(); i++) {
			// 字串轉換成浮點數
			float v = std::stof(fields.at(i));

			switch (i) {
			case 1:
				data.stockOpen = v;
				break;
			case 2:
				data.stockHigh = v;
				break;
			case 3:
				data.stockLow = v;
				break;
			case 4:
				data.stockClose = v;
				break;
			case 5:
				v = log(v);
				data.stockVol = v;
				break;
			case 6:
				data.stockAdjClose = v;
				break;
			}

			std::cout << std::setprecision(2) << std::fixed << v << '\t';
		}

		std::cout << std::endl;
		return data;
	}
	;

	void Output(MEM_STOCK *data) {
		std::cout << data->date << '\t';
		std::cout << std::setprecision(2) << std::fixed << data->stockOpen << '\t';
		std::cout << std::setprecision(2) << std::fixed << data->stockHigh << '\t';
		std::cout << std::setprecision(2) << std::fixed << data->stockLow << '\t';
		std::cout << std::setprecision(2) << std::fixed << data->stockClose << '\t';
		std::cout << std::setprecision(2) << std::fixed << data->stockVol << '\t';
		std::cout << std::setprecision(2) << std::fixed << data->stockAdjClose << '\n';
	}
public:
	// 格式化輸出檔案資料
	void Print(ROWS rows) {
		TMEMFile *worker = new TMEMFile("STOCK",
				sizeof(MEM_STOCK) * rows.size());

		for (unsigned i = 0; i < rows.size(); i++) {
			MEM_STOCK data = this->Output(rows.at(i));
			worker->Put(i * sizeof(MEM_STOCK), (void *) &data, sizeof(MEM_STOCK));
		}

		std::chrono::duration<int, std::milli> timespan(1000 * 50);
		std::this_thread::sleep_for(timespan);

		delete worker;
	}

	// 格式化輸出共享記憶體內資料
	void Print() {
		TMEMFile *worker = new TMEMFile("STOCK", 0);

		size_t packs = worker->GetMemSize() / sizeof(MEM_STOCK);

		for (unsigned i = 0; i < packs; i++) {
			MEM_STOCK *data = (MEM_STOCK*) worker->Get(i * sizeof(MEM_STOCK),
					sizeof(MEM_STOCK));

			if (data == NULL) {
				break;
			}

			this->Output(data);
		}

		delete worker;
	}
};

// 主程式
int main(int argc, char* argv[]) {
	std::string csvFile = "";

	TCSV *csv = new TCSV();
	Stock *worker = new Stock();

	if (argc > 1) {
		csvFile = argv[1];

		ROWS rows = csv->Load(csvFile);
		if (rows.size() > 0) {
			worker->Print(rows);
		}
	} else {
		worker->Print();
	}

	delete worker;
	delete csv;

	return 0;
}