-
Notifications
You must be signed in to change notification settings - Fork 1
/
SaveLoad.cpp
142 lines (109 loc) · 3.04 KB
/
SaveLoad.cpp
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
136
137
138
139
140
141
142
// SaveLoad.cpp : implementation file
//
#include "stdafx.h"
#include "Sudoku.h"
#include "SaveLoad.h"
#include <fstream>
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CSaveAndLoad dialog
CSaveAndLoad::CSaveAndLoad(CWnd* pParent /*=NULL*/)
: CDialog(CSaveAndLoad::IDD, pParent)
{
//{{AFX_DATA_INIT(CSaveAndLoad)
m_Value = _T("");
//}}AFX_DATA_INIT
}
void CSaveAndLoad::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CSaveAndLoad)
DDX_Control(pDX, IDC_COMBO, m_Combo);
DDX_CBString(pDX, IDC_COMBO, m_Value);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CSaveAndLoad, CDialog)
//{{AFX_MSG_MAP(CSaveAndLoad)
ON_BN_CLICKED(IDC_LOAD, OnLoad)
ON_BN_CLICKED(IDC_SAVE, OnSave)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CSaveAndLoad message handlers
void CSaveAndLoad::OnLoad()
{
UpdateData(TRUE);
if(m_Value == "") return;
char sPath[512];
strcpy_s(sPath,512,m_Value);
int iLen = strlen(sPath);
if( sPath[iLen-4] != '.' ||
sPath[iLen-3] != 's' ||
sPath[iLen-2] != 'a' ||
sPath[iLen-1] != 'v')
m_Value += ".sav";
UpdateData(FALSE);
ifstream iRead(m_Value,ios::in |ios::binary);
if(iRead.is_open() == FALSE) {
MessageBox("File couldn't be opened","Sudoku",MB_OK|MB_ICONWARNING);
return;
}
char LastLine[512];
for(int iLines = 1;iLines <= MAX*MAX;iLines++) { ;
iRead.getline(LastLine,sizeof(LastLine));
if(atoi(LastLine) >= 1 && atoi(LastLine) <= 9) iSquares[iLines] = atoi(LastLine);
else iSquares[iLines] = 0;
}
iRead.close();
bLoad = TRUE;
CDialog::OnOK();
}
void CSaveAndLoad::OnSave()
{
UpdateData(TRUE);
if(m_Value == "") return;
char sPath[512];
strcpy_s(sPath,512,m_Value);
int iLen = strlen(sPath);
if( sPath[iLen-4] != '.' ||
sPath[iLen-3] != 's' ||
sPath[iLen-2] != 'a' ||
sPath[iLen-1] != 'v')
m_Value += ".sav";
UpdateData(FALSE);
ifstream iRead(m_Value,ios::in|ios::binary);
if(iRead.is_open() == TRUE) {
if(MessageBox("File already exists. Do you want to overwrite it?","Sudoku",MB_YESNO|MB_ICONQUESTION) == IDNO) return;
}
iRead.close();
LPTSTR cValue = "";
ofstream oWrite(m_Value,ios::binary);
if(oWrite.is_open() == TRUE) {
for(int iTemp = 1;iTemp <= MAX*MAX;iTemp++) {
if(iSquares[iTemp] < 0 || iSquares[iTemp]> MAX) iSquares[iTemp] = 0;
oWrite<<iSquares[iTemp]<<endl;
}
oWrite.close();
bLoad = FALSE;
CDialog::OnOK();
}
else MessageBox("Could not save current board","Sudoku",MB_OK|MB_ICONWARNING);
}
BOOL CSaveAndLoad::OnInitDialog()
{
CDialog::OnInitDialog();
m_Combo.ResetContent( );
char sPath[512] = "";
GetCurrentDirectory(512,sPath);
strcat_s(sPath,512,"\\*.sav");
m_Combo.Dir(DDL_READWRITE, sPath);
UpdateData(FALSE);
if(m_Combo.GetCount() > 0) m_Combo.SetCurSel(0);
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}