| 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 | // InfFile.cpp: implementation of the InfFile class. |
| 20 | // |
| 21 | ////////////////////////////////////////////////////////////////////// |
| 22 | |
| 23 | #include "inffile.h" |
| 24 | |
| 25 | #include "cfile.h" |
| 26 | #include "pstring.h" |
| 27 | #include "pserror.h" |
| 28 | |
| 29 | #include <string.h> |
| 30 | |
| 31 | #define INFFILE_NULL (-1024) |
| 32 | |
| 33 | ////////////////////////////////////////////////////////////////////// |
| 34 | // Construction/Destruction |
| 35 | ////////////////////////////////////////////////////////////////////// |
| 36 | |
| 37 | InfFile::InfFile() { m_fp = nullptr; } |
| 38 | |
| 39 | InfFile::~InfFile() { |
| 40 | // close file and free symbol lists |
| 41 | Close(); |
| 42 | } |
| 43 | |
| 44 | // symbol functions. |
| 45 | void InfFile::AddSymbol(const char *name, const char *text) { |
| 46 | tListNode<sym_info> *symbol = new tListNode<sym_info>; |
| 47 | |
| 48 | strcpy(symbol->t.name, name); |
| 49 | symbol->t.text = new char[strlen(text) + 1]; |
| 50 | strcpy(symbol->t.text, text); |
| 51 | |
| 52 | m_sym_list.link(symbol); |
| 53 | } |
| 54 | |
| 55 | void InfFile::FreeSymbols() { |
| 56 | tListNode<sym_info> *sym; |
| 57 | |
| 58 | m_sym_list.start(); |
| 59 | |
| 60 | while ((sym = m_sym_list.start()) != 0) { |
| 61 | sym = m_sym_list.unlink(); |
| 62 | if (sym->t.text) |
| 63 | delete sym->t.text; |
| 64 | delete sym; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | const char *InfFile::GetSymbolText(const char *name) { |
| 69 | tListNode<sym_info> *sym; |
| 70 | |
| 71 | m_sym_list.start(); |
| 72 | for (sym = m_sym_list.start(); sym; sym = sym->next) { |
| 73 | if (strcmp(sym->t.name, name) == 0) |
| 74 | return (const char *)sym->t.text; |
| 75 | } |
| 76 | |
| 77 | return nullptr; |
| 78 | } |
| 79 | |
| 80 | // opens an inf file, pass in a lexical analyzer that will return a command index. |
| 81 | // tag_check is a string that must appear at the very beginning of the file. |
| 82 | // lexfn is a function to match a command to a number. num <= 0 are taken. |
| 83 | bool InfFile::Open(const char *filename, const char *tag_check, int (*lexfn)(const char *command)) { |
| 84 | if (m_fp) |
| 85 | return false; |
| 86 | |
| 87 | m_fp = cfopen(filename, "rt"); |
| 88 | if (!m_fp) |
| 89 | return false; |
| 90 | |
| 91 | // do tag checking. |
| 92 | if (tag_check) { |
| 93 | cf_ReadString(m_linetext, INFFILE_LINELEN, m_fp); |
| 94 | if (strcmp(tag_check, m_linetext) != 0) { |
| 95 | cfclose(m_fp); |
| 96 | m_fp = NULL; |
| 97 | return false; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // okay we have a 'valid' inf file. |
| 102 | m_lexfn = lexfn; |
| 103 | m_lineptr = NULL; |
| 104 | m_endptr = NULL; |
| 105 | m_line = 0; |
| 106 | |
| 107 | return true; |
| 108 | } |
| 109 | |
| 110 | // closes the file |
| 111 | void InfFile::Close() { |
| 112 | if (!m_fp) |
| 113 | return; |
| 114 | |
| 115 | FreeSymbols(); |
| 116 | m_sym_list.free(); |
| 117 | cfclose(m_fp); |
| 118 | m_fp = NULL; |
| 119 | } |
| 120 | |
| 121 | // read a command line. returns false on eof. |
| 122 | bool InfFile::ReadLine() { |
| 123 | if (cfeof(m_fp)) |
| 124 | return false; |
| 125 | |
| 126 | cf_ReadString(m_linetext, INFFILE_LINELEN, m_fp); |
| 127 | |
| 128 | // start line. |
| 129 | m_lineptr = &m_linetext[0]; |
| 130 | m_endptr = m_lineptr + strlen(m_lineptr) + 1; |
| 131 | m_line++; |
| 132 | |
| 133 | return true; |
| 134 | } |
| 135 | |
| 136 | // parses a line of 'code'. return value from the lexfn |
| 137 | // a 0 for EOL (end of line). |
| 138 | // -1 for error. |
| 139 | // -2 for comment |
| 140 | int InfFile::ParseLine(char *operand, int oprlen) { |
| 141 | if (!m_lineptr) |
| 142 | return INFFILE_EOL; |
| 143 | |
| 144 | // tokenize line. |
| 145 | char command[32]; // Command read from line. |
| 146 | char *opr = NULL, *cmd; |
| 147 | int retval = INFFILE_NULL; |
| 148 | |
| 149 | if (strlen(m_lineptr) == 0) |
| 150 | return INFFILE_EOL; |
| 151 | |
| 152 | // parse out command and operand (command=str) |
| 153 | // m_lineptr = command, opr = operand, nextcmd = next lineptr. |
| 154 | cmd = strtok(m_lineptr, " \t=:"); |
| 155 | if (cmd) |
| 156 | opr = strtok(NULL, ",;"); |
| 157 | else |
| 158 | Int3(); |
| 159 | |
| 160 | // clean out trailing and preceeding trash (tabs, spaces, etc) |
| 161 | CleanupStr(command, cmd, sizeof(command)); |
| 162 | if (opr) |
| 163 | CleanupStr(operand, opr, oprlen); |
| 164 | else |
| 165 | operand[0] = 0; |
| 166 | |
| 167 | if (strlen(command) == 0) { |
| 168 | if (strlen(operand) == 0) |
| 169 | return INFFILE_EOL; |
| 170 | } |
| 171 | |
| 172 | // do predefined commands |
| 173 | if (command[0] == '@') |
| 174 | retval = INFFILE_COMMENT; |
| 175 | |
| 176 | // do symbolic commands. |
| 177 | if (operand[0] == '#') { |
| 178 | const char *sym = GetSymbolText(&operand[1]); |
| 179 | if (sym) |
| 180 | strcpy(operand, sym); |
| 181 | else |
| 182 | retval = INFFILE_ERROR; |
| 183 | } |
| 184 | |
| 185 | if (retval == INFFILE_NULL) { |
| 186 | if (command[0] == '#') { |
| 187 | AddSymbol(&command[1], operand); |
| 188 | retval = INFFILE_SYMBOL; |
| 189 | } else { |
| 190 | // do command through lex analyzer. if not preprocessed. |
| 191 | if (m_lexfn) |
| 192 | retval = (*m_lexfn)(command); |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | // adjust text pointers to prepare for next read |
| 197 | char *lastptr = (opr) ? opr : cmd; |
| 198 | m_lineptr = lastptr + strlen(lastptr) + 1; |
| 199 | if (m_lineptr >= m_endptr) |
| 200 | m_lineptr = NULL; |
| 201 | |
| 202 | // end |
| 203 | return retval; |
| 204 | } |