util.cpp
上传用户:egreat
上传日期:2007-07-13
资源大小:29k
文件大小:2k
- #include "config.h"
- #include <iostream>
- #include <fstream>
- #include <string>
- #include <sstream>
- #include <loki/Factory.h>
- #include <boost/scoped_ptr.hpp>
- #include "response.h"
- #include "stock.h"
- using namespace std;
- namespace StockMarket
- {
- bool is_file_exist(const string& filename)
- {
- bool isOpen;
- ifstream ifs;
- ifs.open(filename.c_str());
- isOpen = ifs.is_open();
- ifs.close();
- return isOpen;
- }
- bool stockcode_is_valid(const uchar * s)
- {
- bool valid = true;
- for(int i = 0; i < MarketInfo::StocksCodeLen; ++i)
- {
- if(s[i] < '0' || s[i] > '9')
- {
- valid = false;
- break;
- }
- }
- return valid;
- }
- const uchar * stock_code_search (const uchar * str, unsigned int len)
- {
- const uchar *cp = str;
- while (len -- > 0)
- {
- if(stockcode_is_valid(cp))
- return cp;
- cp++;
- }
- return 0;
- }
- void log_packet(const char* data, size_t len)
- {
- const string path = "F:\Develop\stock\data\exc_log\exc";
- string filename;
- for(int i = 0; i < 1000; i++)
- {
- stringstream ss;
- ss << i << ".dat";
- filename = path + ss.str();
- if(!is_file_exist(filename))
- {
- ofstream ofs(filename.c_str(), ios::out | ios::binary);
- ofs.write(data, len);
- break;
- }
- }
- }
- void test_packet_from_file(const string& filename )
- {
- uchar temp[10 * 1024];
- uchar *pData = &temp[0];
- const string path = "F:\Develop\stock\data\exc_log\";
- const string fullname = path + filename;
- fstream fs(fullname.c_str(), ios::in | ios::binary);
- if(!fs) return;
- fs.read((char*)pData, 10*1024);
- uint len = fs.gcount();
- ProcessResponsePacket(pData, len, true);
- }
- }