File: textviewdlg.cpp

package info (click to toggle)
freespace2 24.0.2%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: trixie
  • size: 43,188 kB
  • sloc: cpp: 583,107; ansic: 21,729; python: 1,174; sh: 464; makefile: 248; xml: 181
file content (295 lines) | stat: -rw-r--r-- 5,984 bytes parent folder | download | duplicates (2)
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
 * Copyright (C) Volition, Inc. 1999.  All rights reserved.
 *
 * All source code herein is the property of Volition, Inc. You may not sell 
 * or otherwise commercially exploit the source or things you created based on the 
 * source.
 *
*/

// TextViewDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FRED.h"
#include "TextViewDlg.h"
#include "cfile/cfile.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// TextViewDlg dialog

TextViewDlg::TextViewDlg(CWnd* pParent /*=NULL*/)
	: CDialog(TextViewDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(TextViewDlg)
	m_edit = _T("");
	//}}AFX_DATA_INIT

	m_original_text = _T("");
	m_caption = _T("");
	m_editable = false;
}

void TextViewDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(TextViewDlg)
	DDX_Text(pDX, IDC_EDIT1, m_edit);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(TextViewDlg, CDialog)
	//{{AFX_MSG_MAP(TextViewDlg)
	ON_WM_CLOSE()
	ON_EN_SETFOCUS(IDC_EDIT1, OnSetfocusEdit1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// TextViewDlg message handlers

BOOL TextViewDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	m_original_text = m_edit;

	if (!m_caption.IsEmpty())
		SetCaption(m_caption);
	SetEditable(m_editable);

	return TRUE;
}

void TextViewDlg::OnClose()
{
	UpdateData(TRUE);
	CDialog::OnClose();
}

void TextViewDlg::LoadShipsTblText(const ship_info *sip)
{
	char line[256], line2[256], file_text[82];
	int i, j, n, found = 0, comment = 0, num_files = 0;
	SCP_vector<SCP_string> tbl_file_names;
	CFILE *fp;

	SetCaption("Ship Table Data");

	if (!sip)
		return;

	fp = cfopen("ships.tbl", "r");
	Assert(fp);

	
	while (cfgets(line, 255, fp)) {
		while (line[strlen(line) - 1] == '\n')
			line[strlen(line) - 1] = 0;

		for (i=j=0; line[i]; i++) {
			if (line[i] == '/' && line[i+1] == '/')
				break;

			if (line[i] == '/' && line[i+1] == '*') {
				comment = 1;
				i++;
				continue;
			}

			if (line[i] == '*' && line[i+1] == '/') {
				comment = 0;
				i++;
				continue;
			}

			if (!comment)
				line2[j++] = line[i];
		}

		line2[j] = 0;
		if (!strnicmp(line2, "$Name:", 6)) {
			drop_trailing_white_space(line2);
			found = 0;
			i = 6;

			while (line2[i] == ' ' || line2[i] == '\t' || line2[i] == '@')
				i++;

			if (!stricmp(line2 + i, sip->name)) {
				m_edit += "-- ships.tbl  -------------------------------\r\n";
				found = 1;
			}
		}

		if (found) {
			m_edit += line;
			m_edit += "\r\n";
		}
	}

	cfclose(fp);


	// done with ships.tbl, so now check all modular ship tables...
	num_files = cf_get_file_list(tbl_file_names, CF_TYPE_TABLES, NOX("*-shp.tbm"), CF_SORT_REVERSE);

	for (n = 0; n < num_files; n++){
		tbl_file_names[n] += ".tbm";

		fp = cfopen(tbl_file_names[n].c_str(), "r");
		Assert(fp);

		memset( line, 0, sizeof(line) );
		memset( line2, 0, sizeof(line2) );
		found = 0;
		comment = 0;

		while (cfgets(line, 255, fp)) {
			while (line[strlen(line) - 1] == '\n')
				line[strlen(line) - 1] = 0;

			for (i=j=0; line[i]; i++) {
				if (line[i] == '/' && line[i+1] == '/')
					break;

				if (line[i] == '/' && line[i+1] == '*') {
					comment = 1;
					i++;
					continue;
				}

				if (line[i] == '*' && line[i+1] == '/') {
					comment = 0;
					i++;
					continue;
				}

				if (!comment)
					line2[j++] = line[i];
			}

			line2[j] = 0;
			if (!strnicmp(line2, "$Name:", 6)) {
				drop_trailing_white_space(line2);
				found = 0;
				i = 6;

				while (line2[i] == ' ' || line2[i] == '\t' || line2[i] == '@')
					i++;

				if (!stricmp(line2 + i, sip->name)) {
					memset( file_text, 0, sizeof(file_text) );
					snprintf(file_text, sizeof(file_text)-1, "--  %s  -------------------------------\r\n", tbl_file_names[n].c_str());
					m_edit += file_text;
					found = 1;
				}
			}

			if (found) {
				m_edit += line;
				m_edit += "\r\n";
			}
		}

		cfclose(fp);
	}
}

void TextViewDlg::LoadMusicTblText()
{
	char line[256];
	CFILE* fp;

	SetCaption("Music Table Data");

	fp = cfopen("music.tbl", "r");
	Assert(fp);

	// print the header
	m_edit += "-- music.tbl  -------------------------------\r\n";

	// now get the file content
	while (cfgets(line, 255, fp)) {
		m_edit += line;
		m_edit += "\r\n";
	}

	cfclose(fp);

	SCP_vector<SCP_string> tbl_file_names;

	// done with music.tbl, so now check all modular music tables...
	int num_files = cf_get_file_list(tbl_file_names, CF_TYPE_TABLES, NOX("*-mus.tbm"), CF_SORT_REVERSE);

	for (int n = 0; n < num_files; n++) {
		tbl_file_names[n] += ".tbm";

		fp = cfopen(tbl_file_names[n].c_str(), "r");
		Assert(fp);

		memset(line, 0, sizeof(line));

		// get the name of the current file and print it
		char file_text[82];
		memset(file_text, 0, sizeof(file_text));
		snprintf(file_text, sizeof(file_text) - 1, "--  %s  -------------------------------\r\n", tbl_file_names[n].c_str());
		m_edit += file_text;

		// now get the file content
		while (cfgets(line, 255, fp)) {
			m_edit += line;
			m_edit += "\r\n";
		}

		cfclose(fp);
	}
}

void TextViewDlg::OnSetfocusEdit1()
{
	// when the dialog is first displayed, prevent it from selecting all the text
	((CEdit *)GetDlgItem(IDC_EDIT1))->SetSel(-1, -1);
}

void TextViewDlg::SetText(const CString &text)
{
	m_edit = text;

	// accommodate MFC newlines
	m_edit.Replace("\n", "\r\n");
	m_edit.Replace("\r\r", "\r");

	if (IsWindow(m_hWnd))
		UpdateData(FALSE);
}

void TextViewDlg::GetText(CString &text)
{
	if (IsWindow(m_hWnd))
		UpdateData(TRUE);

	text = m_edit;

	// accommodate MFC newlines
	text.Replace("\r\n", "\n");
}

void TextViewDlg::SetCaption(const CString &caption)
{
	m_caption = caption;
	if (IsWindow(m_hWnd))
		SetWindowText(m_caption);
}

void TextViewDlg::SetEditable(bool editable)
{
	m_editable = editable;
	if (IsWindow(m_hWnd))
		((CEdit *)GetDlgItem(IDC_EDIT1))->SetReadOnly(editable ? FALSE : TRUE);
}