| 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 | // HogDialog.cpp : implementation file |
| 20 | // |
| 21 | |
| 22 | #include "stdafx.h" |
| 23 | #include <afxtempl.h> |
| 24 | |
| 25 | #include "editor.h" |
| 26 | |
| 27 | #include "hogfile.h" |
| 28 | #include "HogDialog.h" |
| 29 | #include "pserror.h" |
| 30 | |
| 31 | #include <stdio.h> |
| 32 | #include <malloc.h> |
| 33 | #include <string.h> |
| 34 | |
| 35 | #ifdef _DEBUG |
| 36 | #define new DEBUG_NEW |
| 37 | #undef THIS_FILE |
| 38 | static char THIS_FILE[] = __FILE__; |
| 39 | #endif |
| 40 | |
| 41 | ///////////////////////////////////////////////////////////////////////////// |
| 42 | // data structures |
| 43 | /* |
| 44 | struct hog_library { |
| 45 | CList <hog_library_entry, hog_library_entry&> table; |
| 46 | FILE *fp; |
| 47 | }; |
| 48 | */ |
| 49 | |
| 50 | ///////////////////////////////////////////////////////////////////////////// |
| 51 | // Hogfile manipulation functions |
| 52 | /* |
| 53 | // creates a new hogfile |
| 54 | // does nothing else really |
| 55 | hog_library *new_hogfile(char *hogname) |
| 56 | { |
| 57 | FILE *fp; |
| 58 | hog_library *hogfile; |
| 59 | |
| 60 | fp = fopen( hogname, "wb" ); |
| 61 | if ( fp == NULL ) return NULL; |
| 62 | |
| 63 | fwrite(LIBRARY_TAG, 4, 1, fp); |
| 64 | |
| 65 | hogfile = new hog_library; |
| 66 | hogfile->fp = fp; |
| 67 | |
| 68 | return hogfile; |
| 69 | } |
| 70 | |
| 71 | |
| 72 | // this opens an existing hogfile |
| 73 | // loads in file listing and hog info. |
| 74 | hog_library *open_hogfile(char *hogname) |
| 75 | { |
| 76 | FILE *fp, *fplst; |
| 77 | char *strptr; |
| 78 | char filename[_MAX_PATH]; |
| 79 | int id, nfiles; |
| 80 | int i,offset,t; |
| 81 | index_entry *index; |
| 82 | hog_library *hogfile = NULL; |
| 83 | |
| 84 | strcpy(filename, hogname); |
| 85 | strptr = strstr(filename, ".hog"); |
| 86 | if (strptr) { |
| 87 | strptr[0] = 0; // terminate this string |
| 88 | } |
| 89 | strcat(filename, ".lst"); // load in list file too. |
| 90 | |
| 91 | fp = fopen( hogname, "rb" ); |
| 92 | if ( fp == NULL ) return NULL; |
| 93 | fplst = fopen(filename, "rb"); |
| 94 | if ( fplst == NULL ) { |
| 95 | fclose(fp); |
| 96 | OutrageMessageBox("Unable to open hogfile's database. <%s>\n", filename); |
| 97 | return (hog_library *)0xffffffff; |
| 98 | } |
| 99 | |
| 100 | fread( &id, sizeof(int), 1, fp ); |
| 101 | if ( strncmp( (char *) &id, LIBRARY_TAG, strlen(LIBRARY_TAG) ) ) { |
| 102 | fclose(fp); |
| 103 | fclose(fplst); |
| 104 | OutrageMessageBox("Invalid hog file <%s>\n", hogname); |
| 105 | return (hog_library *)0xffffffff; |
| 106 | } |
| 107 | |
| 108 | fread(&nfiles,sizeof(int),1,fp); // get number of files |
| 109 | index = new index_entry[nfiles]; // allocate file index. |
| 110 | hogfile = new hog_library; |
| 111 | hogfile->fp = fp; |
| 112 | |
| 113 | // id + nfiles + nfiles * (filename + size) |
| 114 | offset = sizeof(int)+sizeof(int)+nfiles*(LIB_FILENAME_LEN+sizeof(int)); |
| 115 | |
| 116 | // read in index table |
| 117 | t = fread( index, sizeof(index_entry), nfiles, fp ); |
| 118 | if ( t != nfiles ) { //eof here is ok |
| 119 | fclose(fp); |
| 120 | fclose(fplst); |
| 121 | OutrageMessageBox("Hogfile's file index corrupt <%s>.\n", hogname); |
| 122 | return (hog_library *)0xffffffff; //CF_BAD_LIB; |
| 123 | } |
| 124 | |
| 125 | // copy disk index to our internal structure (generate our dynamic list.) |
| 126 | for (i=0;i<nfiles;i++) |
| 127 | { |
| 128 | hog_library_entry entry; |
| 129 | |
| 130 | // get database path for filename |
| 131 | fgets(entry.path, LIB_PATHNAME_LEN, fplst); |
| 132 | if (entry.path[strlen(entry.path)-1] == '\n') |
| 133 | entry.path[strlen(entry.path)-1] = 0; // get rid of newline, if there |
| 134 | |
| 135 | // create library entry from index entry. |
| 136 | strcpy(entry.name, index[i].name); |
| 137 | entry.length = index[i].len; |
| 138 | entry.offset = offset; |
| 139 | #if LIB_HAS_DATE |
| 140 | entry.timestamp = index[i].timestamp; |
| 141 | #else |
| 142 | entry.timestamp = 0; |
| 143 | #endif |
| 144 | #if LIB_HAS_FLAGS |
| 145 | entry.flags = index[i].flags; |
| 146 | #else |
| 147 | entry.flags = 0; |
| 148 | #endif |
| 149 | hogfile->table.AddTail(entry); |
| 150 | |
| 151 | offset += entry.length; |
| 152 | } |
| 153 | |
| 154 | delete[] index; |
| 155 | |
| 156 | fclose(fplst); |
| 157 | |
| 158 | return hogfile; |
| 159 | } |
| 160 | |
| 161 | |
| 162 | // deallocates all info in library struct |
| 163 | void close_hogfile(hog_library *hogfile) |
| 164 | { |
| 165 | fclose(hogfile->fp); |
| 166 | delete hogfile; |
| 167 | } |
| 168 | |
| 169 | |
| 170 | // copies a file to the hogfile! |
| 171 | int copy_file_to_hog(hog_library *hogfile, FILE *srcfp) |
| 172 | { |
| 173 | |
| 174 | |
| 175 | |
| 176 | return 1; |
| 177 | } |
| 178 | |
| 179 | |
| 180 | // save hog file |
| 181 | // writes out complete hogfile copying info |
| 182 | // returns 0 if standard error |
| 183 | |
| 184 | int write_hogfile(hog_library *library) |
| 185 | { |
| 186 | index_entry *index = NULL; |
| 187 | int nfiles, index_pos; //, pos |
| 188 | |
| 189 | nfiles = library->table.GetCount(); |
| 190 | if (nfiles != 0) { |
| 191 | index = new index_entry[nfiles]; |
| 192 | if (index == NULL) { |
| 193 | OutrageMessageBox("Unable to allocate memory for hogfile write."); |
| 194 | return 0; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // write out num elements |
| 199 | fseek(library->fp, sizeof(int), SEEK_SET); |
| 200 | fwrite(&nfiles, sizeof(int), 1, library->fp); |
| 201 | |
| 202 | if (nfiles == 0) return 1; // if no files, we are done. |
| 203 | |
| 204 | // write out file dummy index |
| 205 | index_entry dummy; |
| 206 | index_pos= ftell(library->fp); |
| 207 | ZeroMemory(&dummy, sizeof(dummy)); |
| 208 | if (fwrite(&dummy, sizeof(index_entry), nfiles, library->fp) != (unsigned)nfiles) { |
| 209 | OutrageMessageBox("Out of space!!!"); |
| 210 | delete[] index; |
| 211 | return 0; |
| 212 | } |
| 213 | |
| 214 | // copy the files to the hogfile! |
| 215 | |
| 216 | |
| 217 | |
| 218 | return 1; |
| 219 | } |
| 220 | */ |
| 221 | |
| 222 | ///////////////////////////////////////////////////////////////////////////// |
| 223 | // CHogDialog dialog |
| 224 | |
| 225 | CHogDialog::CHogDialog(CWnd *pParent, char *filename) : CDialog(CHogDialog::IDD, pParent) { |
| 226 | //{{AFX_DATA_INIT(CHogDialog) |
| 227 | // NOTE: the ClassWizard will add member initialization here |
| 228 | //}}AFX_DATA_INIT |
| 229 | |
| 230 | m_HogName = filename; |
| 231 | } |
| 232 | |
| 233 | void CHogDialog::DoDataExchange(CDataExchange *pDX) { |
| 234 | CDialog::DoDataExchange(pDX); |
| 235 | //{{AFX_DATA_MAP(CHogDialog) |
| 236 | // NOTE: the ClassWizard will add DDX and DDV calls here |
| 237 | //}}AFX_DATA_MAP |
| 238 | } |
| 239 | |
| 240 | BEGIN_MESSAGE_MAP(CHogDialog, CDialog) |
| 241 | //{{AFX_MSG_MAP(CHogDialog) |
| 242 | ON_WM_SHOWWINDOW() |
| 243 | //}}AFX_MSG_MAP |
| 244 | END_MESSAGE_MAP() |
| 245 | |
| 246 | ///////////////////////////////////////////////////////////////////////////// |
| 247 | // CHogDialog message handlers |
| 248 | |
| 249 | BOOL CHogDialog::OnInitDialog() { |
| 250 | CDialog::OnInitDialog(); |
| 251 | |
| 252 | // TODO: Add extra initialization here |
| 253 | m_ListCtrl = (CListCtrl *)GetDlgItem(IDC_HOGLIST); |
| 254 | m_ListCtrl->InsertColumn(0, "Filename", LVCFMT_LEFT, 140); |
| 255 | m_ListCtrl->InsertColumn(1, "Date", LVCFMT_LEFT, 80); |
| 256 | m_ListCtrl->InsertColumn(2, "Length", LVCFMT_LEFT, 80); |
| 257 | m_ListCtrl->InsertColumn(3, "Attributes", LVCFMT_LEFT, 120); |
| 258 | |
| 259 | // open hogfile or create new one. |
| 260 | /* m_Library = open_hogfile(m_HogName); |
| 261 | if (!m_Library) { |
| 262 | int res = MessageBox("Hogfile does not exist. Do you wish to create a new one?", "Hogmaker", |
| 263 | MB_YESNO); if (res == IDNO) { EndDialog(IDCANCEL); return TRUE; |
| 264 | } |
| 265 | else { |
| 266 | m_Library = new_hogfile(m_HogName); |
| 267 | if (!m_Library) { |
| 268 | MessageBox("Unable to create hogfile.", "Error", MB_OK); |
| 269 | EndDialog(IDCANCEL); |
| 270 | return TRUE; |
| 271 | } |
| 272 | } |
| 273 | } |
| 274 | else if (m_Library == (hog_library *)0xffffffff) { |
| 275 | MessageBox("Hogmaker failure. Sorry...", "Hogmaker", MB_OK); |
| 276 | EndDialog(IDCANCEL); |
| 277 | return TRUE; |
| 278 | } |
| 279 | */ |
| 280 | return TRUE; // return TRUE unless you set the focus to a control |
| 281 | // EXCEPTION: OCX Property Pages should return FALSE |
| 282 | } |
| 283 | |
| 284 | void CHogDialog::OnShowWindow(BOOL bShow, UINT nStatus) { |
| 285 | CDialog::OnShowWindow(bShow, nStatus); |
| 286 | |
| 287 | // TODO: Add your message handler code here |
| 288 | } |