• R/O
  • HTTP
  • SSH
  • HTTPS

GVONavish: Commit

大航海時代Onlineのプレイを少しだけ助けてくれるツール。


Commit MetaInfo

Revision0484bdf943cbb46e6f0e40f8c2c1d27b61a3747a (tree)
Time2014-03-11 13:09:42
AuthorMandhelingFreak <mandheling30-freak@yaho...>
CommiterMandhelingFreak

Log Message

航路記録ファイル入出力をiostream系に変更。

Change Summary

Incremental Difference

--- a/GVONavish/GVONavish/GVONavish.cpp
+++ b/GVONavish/GVONavish/GVONavish.cpp
@@ -1,6 +1,7 @@
11 #include "stdafx.h"
22 #include <vector>
33 #include <list>
4+#include <fstream>
45
56 // PNGとかの読み込み用
67 #include <gdiplus.h>
@@ -150,7 +151,18 @@ int APIENTRY _tWinMain( _In_ HINSTANCE hInstance,
150151 // メイン メッセージ ループ:
151152 const LRESULT retVal = s_mainLoop();
152153
153- s_shipRouteList->saveToFile( k_routeListFilePath );
154+ try {
155+ std::ofstream ofs;
156+ ofs.exceptions( std::ios::badbit | std::ios::failbit );
157+ ofs.open( k_routeListFilePath, std::ios::out | std::ios::binary | std::ios::trunc );
158+ ofs << *s_shipRouteList;
159+ ofs.close();
160+ }
161+ catch ( const std::exception& e ) {
162+ ::OutputDebugStringA( (std::string( "file save error:" ) + e.what() + "\n").c_str() );
163+ ::MessageBox( NULL, L"航路保存に失敗しました。", k_appName, MB_ICONERROR );
164+ }
165+ //s_shipRouteList->saveToFile( k_routeListFilePath );
154166 s_gvoGameProcess.teardown();
155167
156168 s_config.save();
@@ -218,7 +230,17 @@ static BOOL InitInstance( HINSTANCE hInstance, int nCmdShow )
218230 s_renderer.setup( &s_config, g_hdcMain, &s_worldMap );
219231
220232 s_shipRouteList.reset( new GVOShipRouteList() );
221- s_shipRouteList->loadFromFile( k_routeListFilePath );
233+ try {
234+ std::ifstream ifs;
235+ ifs.exceptions( std::ios::badbit | std::ios::failbit );
236+ ifs.open( k_routeListFilePath, std::ios::in | std::ios::binary );
237+ ifs >> *s_shipRouteList;
238+ ifs.close();
239+ }
240+ catch ( const std::exception& e ) {
241+ ::OutputDebugStringA( (std::string( "file load error:" ) + e.what() + "\n").c_str() );
242+ ::MessageBox( NULL, L"航路読込に失敗しました。", k_appName, MB_ICONERROR );
243+ }
222244 s_pollingInterval = s_config.m_pollingInterval;
223245 s_gvoGameProcess.setup( s_config );
224246
--- a/GVONavish/GVONavish/GVOShipRouteList.cpp
+++ b/GVONavish/GVONavish/GVOShipRouteList.cpp
@@ -1,6 +1,6 @@
11 #include "stdafx.h"
22 #include "GVOShipRouteList.h"
3-
3+#include <fstream>
44
55 namespace {
66 struct FileHeaderV1 {
@@ -15,89 +15,98 @@ namespace {
1515 }
1616
1717
18-bool GVOShipRouteList::saveToFile( const std::wstring & filePath )
18+std::ostream & operator <<(std::ostream & os, const GVOShipRouteList & shipRouteList)
1919 {
20- std::unique_ptr<FILE, decltype(&::fclose)> file(::_wfopen(filePath.c_str(), L"wb"), &::fclose);
21- if ( !file ) {
22- return false;
20+ _ASSERT( os.good() );
21+ if ( !os.good() ) {
22+ throw std::runtime_error("output stream error.");
2323 }
2424
2525 FileHeader fileHeader;
2626
27- ::fseek( file.get(), sizeof(FileHeader), SEEK_SET );
27+ const auto headPos = os.tellp();
28+ os.seekp( sizeof(fileHeader), std::ios::cur );
2829
29- for ( const auto & shipRoute : m_shipRouteList ) {
30+ for ( const auto & shipRoute : shipRouteList.m_shipRouteList ) {
3031 if ( shipRoute->isFavorite() ) {
3132 ++fileHeader.favoritsCount;
3233
3334 std::time_t dateTime = std::time_t();
3435 dateTime = shipRoute->earliestDateTime();
35- ::fwrite( &dateTime, sizeof(dateTime), 1, file.get() );
36+ os.write( reinterpret_cast<const char *>(&dateTime), sizeof(dateTime) );
3637 dateTime = shipRoute->latestDateTime();
37- ::fwrite( &dateTime, sizeof(dateTime), 1, file.get() );
38+ os.write( reinterpret_cast<const char *>(&dateTime), sizeof(dateTime) );
3839
3940 const size_t lineCount = shipRoute->getLines().size();
40- ::fwrite( &lineCount, sizeof(lineCount), 1, file.get() );
41+ os.write( reinterpret_cast<const char *>(&lineCount), sizeof(lineCount) );
4142
4243 for ( const auto & line : shipRoute->getLines() ) {
4344 const size_t count = line.size();
44- ::fwrite( &count, sizeof(count), 1, file.get() );
45+ os.write( reinterpret_cast<const char *>(&count), sizeof(count) );
4546 if ( !line.empty() ) {
46- ::fwrite( &line[0], sizeof(line[0]), line.size(), file.get() );
47+ os.write( reinterpret_cast<const char *>(&line[0]), sizeof(line[0]) * line.size() );
4748 }
4849 }
4950 }
5051 }
5152
52- ::fseek( file.get(), 0, SEEK_SET );
53- ::fwrite( &fileHeader, sizeof(fileHeader), 1, file.get() );
54- return true;
53+ const auto tailPos = os.tellp();
54+
55+ os.seekp( headPos, std::ios::beg );
56+ os.write( reinterpret_cast<const char *>(&fileHeader), sizeof(fileHeader) );
57+ os.seekp( tailPos, std::ios::beg );
58+
59+ _ASSERT( os.good() );
60+ return os;
5561 }
5662
5763
58-bool GVOShipRouteList::loadFromFile( const std::wstring & filePath )
64+std::istream & operator >>(std::istream & is, GVOShipRouteList & shipRouteList)
5965 {
60- std::unique_ptr<FILE, decltype(&::fclose)> file( ::_wfopen( filePath.c_str(), L"rb" ), &::fclose );
61- if ( !file ) {
62- return false;
66+ _ASSERT( is.good() );
67+ if ( !is.good() ) {
68+ throw std::runtime_error( "input stream error." );
6369 }
6470
6571 FileHeader fileHeader;
66- ::fread( &fileHeader, sizeof( fileHeader ), 1, file.get() );
72+ is.read( reinterpret_cast<char *>(&fileHeader), sizeof(fileHeader) );
6773
6874 if ( fileHeader.version != FileHeader::k_FileVersion ) {
69- return false;
75+ throw std::runtime_error( "unknown file version." );
7076 }
7177
72- m_shipRouteList.clear();
78+ GVOShipRouteList::RouteList workRouteList;
7379
7480 for ( uint32_t i = 0; i < fileHeader.favoritsCount; ++i ) {
7581 GVOShipRoutePtr shipRoute( new GVOShipRoute() );
7682 shipRoute->setFavorite( true );
7783
7884 std::time_t dateTime = std::time_t();
79- ::fread( &dateTime, sizeof(dateTime), 1, file.get() );
85+ is.read( reinterpret_cast<char *>(&dateTime), sizeof(dateTime) );
8086 shipRoute->setEarliestDateTime( dateTime );
81- ::fread( &dateTime, sizeof(dateTime), 1, file.get() );
87+ is.read( reinterpret_cast<char *>(&dateTime), sizeof(dateTime) );
8288 shipRoute->setLatestDateTime( dateTime );
8389
8490 size_t lineCount = 0;
85- ::fread( &lineCount, sizeof(lineCount), 1, file.get() );
91+ is.read( reinterpret_cast<char *>(&lineCount), sizeof(lineCount) );
8692
8793 for ( size_t k = 0; k < lineCount; ++k ) {
8894 size_t pointCount = 0;
89- ::fread( &pointCount, sizeof(pointCount), 1, file.get() );
95+ is.read( reinterpret_cast<char *>(&pointCount), sizeof(pointCount) );
9096 if ( 0 < pointCount ) {
9197 GVOShipRoute::Line tmp( pointCount );
92- ::fread( &tmp[0], sizeof(tmp[0]), tmp.size(), file.get() );
98+ is.read( reinterpret_cast<char *>(&tmp[0]), sizeof(tmp[0]) * tmp.size() );
9399 shipRoute->addLine( std::move( tmp ) );
94100 }
95101 }
96102
97- m_shipRouteList.push_back( std::move( shipRoute ) );
103+ workRouteList.push_back( std::move( shipRoute ) );
98104 }
99105
100- return true;
106+ shipRouteList.m_shipRouteList.swap( workRouteList );
107+
108+ _ASSERT( is.good() );
109+ return is;
101110 }
102111
103112
--- a/GVONavish/GVONavish/GVOShipRouteList.h
+++ b/GVONavish/GVONavish/GVOShipRouteList.h
@@ -1,4 +1,5 @@
11 #pragma once
2+#include <iostream>
23 #include <list>
34 #include "GVONavish.h"
45 #include "GVOShipRoute.h"
@@ -9,6 +10,10 @@ class IGVOShipRouteListObserver;
910
1011 //!@brief 航路リスト管理クラス
1112 class GVOShipRouteList {
13+ //!@note バイナリ保存用
14+ friend std::ostream & operator <<(std::ostream & os, const GVOShipRouteList & shipRouteList);
15+ friend std::istream & operator >>(std::istream & is, GVOShipRouteList & shipRouteList);
16+
1217 private:
1318 typedef std::list<GVOShipRoutePtr> RouteList;
1419
@@ -21,13 +26,6 @@ public:
2126 GVOShipRouteList() = default;
2227 ~GVOShipRouteList() = default;
2328
24- //!@note あんまり行儀が良い事じゃないけれど。
25- bool saveToFile( const std::wstring & filePath );
26-
27- //!@note あんまり行儀が良い事じゃないけれど。
28- bool loadFromFile( const std::wstring & filePath );
29-
30-
3129 void setObserver( IGVOShipRouteListObserver * observer )
3230 {
3331 m_observer = observer;
Show on old repository browser