1 #include <exception>
2 #include "string_cast_sjis.h"
3 #include "Save.h"
4 #include "StdStream.h"
5 #include "StdStreamUtils.h"
6 #include "FilesystemUtils.h"
7 
CSave(const fs::path & basePath)8 CSave::CSave(const fs::path& basePath)
9     : m_basePath(basePath)
10 {
11 	auto iconSysPath = m_basePath / "icon.sys";
12 
13 	auto iconStream(Framework::CreateInputStdStream(iconSysPath.native()));
14 
15 	uint32 nMagic = iconStream.Read32();
16 	if(nMagic != 0x44325350)
17 	{
18 		throw std::runtime_error("Invalid 'icon.sys' file.");
19 	}
20 
21 	iconStream.Seek(6, Framework::STREAM_SEEK_SET);
22 	m_nSecondLineStartPosition = iconStream.Read16();
23 
24 	iconStream.Seek(192, Framework::STREAM_SEEK_SET);
25 	ReadName(iconStream);
26 
27 	char sBuffer[64];
28 
29 	iconStream.Read(sBuffer, 64);
30 	m_sNormalIconFileName = sBuffer;
31 
32 	iconStream.Read(sBuffer, 64);
33 	m_sCopyingIconFileName = sBuffer;
34 
35 	iconStream.Read(sBuffer, 64);
36 	m_sDeletingIconFileName = sBuffer;
37 
38 	m_sId = m_basePath.filename().string();
39 
40 	m_nLastModificationTime = Framework::ConvertFsTimeToSystemTime(fs::last_write_time(iconSysPath));
41 }
42 
GetName() const43 const wchar_t* CSave::GetName() const
44 {
45 	return m_sName.c_str();
46 }
47 
GetId() const48 const char* CSave::GetId() const
49 {
50 	return m_sId.c_str();
51 }
52 
GetSize() const53 unsigned int CSave::GetSize() const
54 {
55 	fs::directory_iterator itEnd;
56 	unsigned int nSize = 0;
57 
58 	for(fs::directory_iterator itElement(m_basePath);
59 	    itElement != itEnd;
60 	    itElement++)
61 	{
62 		if(!fs::is_directory(*itElement))
63 		{
64 			nSize += fs::file_size(*itElement);
65 		}
66 	}
67 
68 	return nSize;
69 }
70 
GetPath() const71 fs::path CSave::GetPath() const
72 {
73 	return m_basePath;
74 }
75 
GetIconPath(const ICONTYPE & iconType) const76 fs::path CSave::GetIconPath(const ICONTYPE& iconType) const
77 {
78 	fs::path iconPath;
79 	switch(iconType)
80 	{
81 	case ICON_NORMAL:
82 		iconPath = GetNormalIconPath();
83 		break;
84 	case ICON_DELETING:
85 		iconPath = GetDeletingIconPath();
86 		break;
87 	case ICON_COPYING:
88 		iconPath = GetCopyingIconPath();
89 		break;
90 	}
91 	return iconPath;
92 }
93 
GetNormalIconPath() const94 fs::path CSave::GetNormalIconPath() const
95 {
96 	return m_basePath / m_sNormalIconFileName;
97 }
98 
GetDeletingIconPath() const99 fs::path CSave::GetDeletingIconPath() const
100 {
101 	return m_basePath / m_sDeletingIconFileName;
102 }
103 
GetCopyingIconPath() const104 fs::path CSave::GetCopyingIconPath() const
105 {
106 	return m_basePath / m_sCopyingIconFileName;
107 }
108 
GetSecondLineStartPosition() const109 size_t CSave::GetSecondLineStartPosition() const
110 {
111 	return std::min<size_t>(m_nSecondLineStartPosition / 2, m_sName.length());
112 }
113 
GetLastModificationTime() const114 time_t CSave::GetLastModificationTime() const
115 {
116 	return m_nLastModificationTime;
117 }
118 
ReadName(Framework::CStream & inputStream)119 void CSave::ReadName(Framework::CStream& inputStream)
120 {
121 	char sNameSJIS[68];
122 	inputStream.Read(sNameSJIS, 68);
123 	m_sName = string_cast_sjis(sNameSJIS);
124 }
125