[dad864]: tlkhandler.cpp  Maximize  Restore  History

Download this file

165 lines (153 with data), 3.9 kB

  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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "stdafx.h"
#include <io.h>
#include <fcntl.h>
#include <sys/stat.h>
#include "chitem.h"
#include "chitemDlg.h"
#include "compat.h"
bool truncate_references(long maxnumber, int which)
{
tlk_entry *newtlkentries;
int i;
if(!tlk_entries[which])
{
return false;
}
if(maxnumber>=tlk_headerinfo[which].entrynum)
{
return false;
}
newtlkentries=new tlk_entry[maxnumber];
if(!newtlkentries)
{
return false; //failed due to memory
}
for(i=0;i<maxnumber;i++)
{
newtlkentries[i]=tlk_entries[which][i];
}
delete [] tlk_entries[which];
tlk_entries[which]=newtlkentries;
tlk_headerinfo[which].entrynum=maxnumber;
return true;
}
CString CChitemDlg::GetTlkFileName(int which)
{
CString folder;
CString tlkfilename;
if (language.GetLength())
{
folder = bgfolder+"lang/"+language+"/";
}
else
{
folder = bgfolder;
}
if(which==1)
{
tlkfilename.Format("%sdialogf.tlk",folder);
}
else
{
tlkfilename.Format("%sdialog.tlk",folder);
}
return tlkfilename;
}
int CChitemDlg::write_file_progress(int which)
{
int ret;
if(!m_progressdlg && do_progress)
{
m_progressdlg=new progressbar;
if(m_progressdlg) m_progressdlg->Create(IDD_PROGRESS);
}
ret=write_file(GetTlkFileName(which),which);
if(m_progressdlg)
{
m_progressdlg->DestroyWindow();
delete m_progressdlg;
m_progressdlg=0;
}
return ret;
}
//this one flushes the memory to disk
int CChitemDlg::write_file(CString str, int which)
{
int fhandle;
int ret;
int i;
bool backup;
long len;
long offset;
CString old, tlkname;
old=str+".old";
ret=false;
backup=false;
if(global_changed[which]==true)
{
if(!access(str,0) )
{
retry:
if(rename(str,old))
{
switch(MessageBox("Overwrite "+old+"?","Backup file already exists.",MB_YESNOCANCEL|MB_TASKMODAL) )
{
case IDYES:
unlink(old);
goto retry;
case IDNO:
break;
case IDCANCEL:
return false;
}
}
else backup=true;
}
fhandle=creat(str,S_IREAD|S_IWRITE);
if(fhandle<1) return false;
TRY
{
tlkname.Format("Writing %s",GetTlkFileName(which));
if(m_progressdlg) m_progressdlg->SetRange(tlk_headerinfo[which].entrynum*2,tlkname);
tlk_headerinfo[which].start=tlk_headerinfo[which].entrynum*sizeof(tlk_reference)+sizeof(tlk_header);
if(write(fhandle,&tlk_headerinfo[which],sizeof(tlk_header) )!=sizeof(tlk_header) )
{
AfxThrowUserException();
}
offset=0;
for(i=0;i<tlk_headerinfo[which].entrynum;i++)
{
set_progress(i);
tlk_entries[which][i].reference.length=tlk_entries[which][i].text.GetLength();
tlk_entries[which][i].reference.offset=offset;
if(write(fhandle,&tlk_entries[which][i].reference,sizeof(tlk_reference) )!=sizeof(tlk_reference) )
{
AfxThrowUserException();
}
offset+=tlk_entries[which][i].reference.length;
}
for(i=0;i<tlk_headerinfo[which].entrynum;i++)
{
set_progress(tlk_headerinfo[which].entrynum+i);
len=tlk_entries[which][i].text.GetLength();
if(write(fhandle,tlk_entries[which][i].text, len )!=len)
{
AfxThrowUserException();
}
}
global_changed[which]=false;
m_event.Empty();
if(backup) log("Done writing! Old tlk file has been backed up.");
else log("Done writing!");
ret=true;
}
CATCH_ALL(e)
{
ret=false;
MessageBox("Runtime exception...","Error",MB_ICONSTOP|MB_OK|MB_TASKMODAL);
}
END_CATCH_ALL;
close(fhandle);
}
return ret;
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks