| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | // DallasImportDlg.cpp : implementation file |
| 20 | // |
| 21 | |
| 22 | #include "stdafx.h" |
| 23 | #include "editor.h" |
| 24 | #include "cfile.h" |
| 25 | #include "DallasMainDlg.h" |
| 26 | #include "DallasUtilities.h" |
| 27 | #include "DallasImportDlg.h" |
| 28 | |
| 29 | #ifdef _DEBUG |
| 30 | #define new DEBUG_NEW |
| 31 | #undef THIS_FILE |
| 32 | static char THIS_FILE[] = __FILE__; |
| 33 | #endif |
| 34 | |
| 35 | ///////////////////////////////////////////////////////////////////////////// |
| 36 | // CDallasImportDlg dialog |
| 37 | |
| 38 | CDallasImportDlg::CDallasImportDlg(CWnd *pParent /*=NULL*/) : CDialog(CDallasImportDlg::IDD, pParent) { |
| 39 | //{{AFX_DATA_INIT(CDallasImportDlg) |
| 40 | // NOTE: the ClassWizard will add member initialization here |
| 41 | //}}AFX_DATA_INIT |
| 42 | m_ScriptName = ""; |
| 43 | m_Filename = ""; |
| 44 | } |
| 45 | |
| 46 | void CDallasImportDlg::DoDataExchange(CDataExchange *pDX) { |
| 47 | CDialog::DoDataExchange(pDX); |
| 48 | //{{AFX_DATA_MAP(CDallasImportDlg) |
| 49 | DDX_Control(pDX, IDC_SCRIPT_LIST, m_ScriptListBox); |
| 50 | //}}AFX_DATA_MAP |
| 51 | } |
| 52 | |
| 53 | BEGIN_MESSAGE_MAP(CDallasImportDlg, CDialog) |
| 54 | //{{AFX_MSG_MAP(CDallasImportDlg) |
| 55 | //}}AFX_MSG_MAP |
| 56 | END_MESSAGE_MAP() |
| 57 | |
| 58 | ///////////////////////////////////////////////////////////////////////////// |
| 59 | // CDallasImportDlg message handlers |
| 60 | |
| 61 | void CDallasImportDlg::OnOK() { |
| 62 | // TODO: Add extra validation here |
| 63 | int index = m_ScriptListBox.GetCurSel(); |
| 64 | if (index == LB_ERR) |
| 65 | return; |
| 66 | |
| 67 | m_ScriptListBox.GetText(index, m_ScriptName); |
| 68 | |
| 69 | CDialog::OnOK(); |
| 70 | } |
| 71 | |
| 72 | void CDallasImportDlg::OnCancel() { |
| 73 | // TODO: Add extra cleanup here |
| 74 | |
| 75 | CDialog::OnCancel(); |
| 76 | } |
| 77 | |
| 78 | BOOL CDallasImportDlg::OnInitDialog() { |
| 79 | CDialog::OnInitDialog(); |
| 80 | |
| 81 | // TODO: Add extra initialization here |
| 82 | FillScriptListBox(m_Filename.GetBuffer(0)); |
| 83 | m_ScriptListBox.SetCurSel(0); |
| 84 | |
| 85 | return TRUE; // return TRUE unless you set the focus to a control |
| 86 | // EXCEPTION: OCX Property Pages should return FALSE |
| 87 | } |
| 88 | |
| 89 | BOOL CDallasImportDlg::FillScriptListBox(char *filename) { |
| 90 | CFILE *infile; |
| 91 | char linebuf[2048]; |
| 92 | char *line; |
| 93 | int linenum; |
| 94 | int valid_lines_read; |
| 95 | |
| 96 | // Try to open the file for loading |
| 97 | infile = cfopen(filename, "rt"); |
| 98 | if (!infile) { |
| 99 | CString msg; |
| 100 | msg.Format("Unable to open \"%s\"!", filename); |
| 101 | MessageBox(msg, "Script Library File Not Found!", MB_OK | MB_ICONEXCLAMATION); |
| 102 | |
| 103 | return FALSE; |
| 104 | } |
| 105 | |
| 106 | linenum = 0; |
| 107 | |
| 108 | // Read in and parse each line of the file |
| 109 | while (!cfeof(infile)) { |
| 110 | |
| 111 | // Clear the buffer |
| 112 | strcpy(linebuf, ""); |
| 113 | |
| 114 | // Read in a line from the file |
| 115 | cf_ReadString(linebuf, sizeof(linebuf), infile); |
| 116 | linenum++; |
| 117 | |
| 118 | // Remove whitespace padding at start and end of line |
| 119 | RemoveTrailingWhitespace(linebuf); |
| 120 | line = SkipInitialWhitespace(linebuf); |
| 121 | |
| 122 | // Check for Start of Script Block Section |
| 123 | if (strncmp(line, SCRIPT_START_TAG, strlen(SCRIPT_START_TAG)) == 0) { |
| 124 | bool done = false; |
| 125 | |
| 126 | // Set valid line counter to track whether we're reading header info or tree nodes |
| 127 | valid_lines_read = 0; |
| 128 | |
| 129 | // Read all the lines in the block |
| 130 | while (!done && !cfeof(infile)) { |
| 131 | |
| 132 | strcpy(linebuf, ""); |
| 133 | cf_ReadString(linebuf, sizeof(linebuf), infile); |
| 134 | linenum++; |
| 135 | |
| 136 | // Remove whitespace padding at start and end of line |
| 137 | RemoveTrailingWhitespace(linebuf); |
| 138 | line = SkipInitialWhitespace(linebuf); |
| 139 | |
| 140 | // If it's an empty line or a comment, skip it |
| 141 | if (strlen(line) == 0 || strncmp(line, "//", 2) == 0) |
| 142 | continue; |
| 143 | |
| 144 | // Check for End of Script Block Section |
| 145 | if (strncmp(line, SCRIPT_END_TAG, strlen(SCRIPT_END_TAG)) == 0) { |
| 146 | done = true; |
| 147 | continue; |
| 148 | } |
| 149 | |
| 150 | // See if it should be the script name line |
| 151 | if (valid_lines_read == 0) { |
| 152 | m_ScriptListBox.AddString(line); |
| 153 | valid_lines_read++; |
| 154 | continue; |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | cfclose(infile); |
| 161 | |
| 162 | return TRUE; |
| 163 | } |