Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions code/always.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,82 @@
#ifndef _stricmp
#define _stricmp stricmp
#endif


/*
** Define some Windows specific values that are used throghout the games
*/
#ifndef _WIN32

#define _MAX_FNAME 255
#define _MAX_EXT 8
#define _MAX_PATH 512
#define MAX_PATH _MAX_PATH
#define _CONTROL 0x20 // space, first non-control character in ASCII

#undef _stricmp
#define stricmp strcasecmp
#define _stricmp strcasecmp
#define strnicmp strncasecmp
#define memicmp strncasecmp
#define __cdecl

#include <cstdio>
#include <cstring>
#include <cctype>

inline static void _makepath(char* path, const char* drive, const char* dir, const char* fname, const char* ext)
{
if (!path || !fname || !ext) {
return;
}

sprintf(path, "%s%s%s", fname, (ext[0] == '.' ? "" : "."), ext);
}

inline static void _splitpath(const char* path, char* drive, char* dir, char* fname, char* ext)
{
if (!path || !ext) {
return;
}

while (*path != '\0') {
if (*path == '.') {
strcpy(ext, path + 1);
break;
}

++path;
}
}

inline static char* strupr(char* str)
{
char* ret = str;
while (*str != '\0') {
*str = toupper(*str);
++str;
}
return(ret);
}

inline static void strrev(char* str)
{
int len = strlen(str);

for (int i = 0; i < len / 2; i++) {
char c = str[i];
str[i] = str[len - i - 1];
str[len - i - 1] = c;
}
}

inline static void _strlwr(char* str)
{
while (*str != '\0') {
*str = tolower(*str);
++str;
}
}

#endif // not _WIN32
2 changes: 2 additions & 0 deletions code/blowfish.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@

#pragma once

#ifdef _WIN32
#include "win.h"
#endif

/// Names and comments from TLBs

Expand Down
105 changes: 2 additions & 103 deletions code/cdfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "cdfile.h"

#include <string>
#include <filesystem>

/*
** Pointer to the first search path record.
Expand Down Expand Up @@ -180,7 +181,7 @@ void CDFileClass::Set_User_Path(char const * path)
break;

default:
UserPath += '\\';
UserPath += std::filesystem::path::preferred_separator;
break;
}
}
Expand Down Expand Up @@ -459,105 +460,3 @@ int CDFileClass::Delete(void)

return(BASECLASS::Delete());
}


HANDLE FindFileHandle = INVALID_HANDLE_VALUE;

/// <summary>
/// Begins a search for the files matching the wildcard specified.
/// This routine will look in the current directory first and then work along the search
/// drive list, settling on the first drive that has a match. Only ordinary files qualify;
/// directories and system, hidden, or temporary files are passed over. Any search still
/// in progress is closed off first.
/// </summary>
/// <param name="fname">The wildcard to search for; filled in with the file found.</param>
/// <returns>bool; Was a matching file found?</returns>
/// <remarks>Be sure that the buffer is big enough to hold the filename returned.</remarks>
bool CDFileClass::Find_First_File(char *fname)
{
WIN32_FIND_DATAA fb;
char scan_path[MAX_PATH];
SearchDriveType *entry;

if (fname) {

Find_Close();

strcpy(scan_path, fname);

HANDLE file_handle = ::FindFirstFile(scan_path, &fb);
if (file_handle != INVALID_HANDLE_VALUE && !(fb.dwFileAttributes & (FILE_ATTRIBUTE_TEMPORARY|FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN))) {

strcpy(fname, fb.cFileName);
FindFileHandle = file_handle;

return(true);
}

entry = First;

if (entry != NULL) {

while (true) {

strcpy(scan_path, entry->Path);
strcat(scan_path, fname);

file_handle = ::FindFirstFile(scan_path, &fb);
if (file_handle != INVALID_HANDLE_VALUE && !(fb.dwFileAttributes & (FILE_ATTRIBUTE_TEMPORARY|FILE_ATTRIBUTE_DIRECTORY|FILE_ATTRIBUTE_SYSTEM|FILE_ATTRIBUTE_HIDDEN))) {
break;
}

entry = (SearchDriveType *)entry->Next;
if (entry == NULL) {
return(false);
}
}

strcpy(fname, fb.cFileName);
FindFileHandle = file_handle;

return(true);
}
}
return(false);
}


/// <summary>
/// Fetches the next file that matches the search in progress.
/// This routine continues the scan begun by Find_First_File, working through the rest of
/// the matches on whichever drive that routine settled upon.
/// </summary>
/// <param name="buffer">Buffer to fill in with the name of the file found.</param>
/// <returns>bool; Was another matching file found?</returns>
/// <remarks>Be sure that the buffer is big enough to hold the filename returned.</remarks>
bool CDFileClass::Find_Next_File(char *buffer)
{
WIN32_FIND_DATAA fb;

if (buffer) {

if (FindFileHandle != INVALID_HANDLE_VALUE && ::FindNextFile(FindFileHandle, &fb) == TRUE) {
strcpy(buffer, fb.cFileName);
return(true);
}

buffer[0] = '\0';
}
return(false);
}


/// <summary>
/// Closes off the file search that is in progress.
/// Call this routine when the results of a Find_First_File scan are no longer wanted, so
/// that the search handle held on the game's behalf is given back to the system.
/// </summary>
void CDFileClass::Find_Close(void)
{
if (FindFileHandle != INVALID_HANDLE_VALUE) {
FindClose(FindFileHandle);
FindFileHandle = INVALID_HANDLE_VALUE;
}
}
4 changes: 0 additions & 4 deletions code/cdfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,6 @@ class CDFileClass : public BufferIOFileClass
static void Set_User_Path(char const * path);
static char const * User_Path(void);

static bool Find_First_File(char *buffer);
static bool Find_Next_File(char *buffer);
static void Find_Close(void);

private:

char const * Capture_Name(char const * filename);
Expand Down
84 changes: 84 additions & 0 deletions code/file.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/*******************************************************************************
* O P E N T S
*******************************************************************************
* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright 2020-2024 Vanilla Conquer contributors
* Copyright 2026 OpenTS contributors
*
* Contains material derived from Vanilla Conquer (https://github.com/TheAssemblyArmada/Vanilla-Conquer).
* Modified by OpenTS contributors, 2026.
* See LICENSE.md for applicable additional terms and warranty disclaimers.
******************************************************************************/

#include "file.h"
#include <string.h>

#ifndef _WIN32
static void Resolve_File_Single(char* fname)
{
Find_File_Data* ffblk;

ffblk = Find_File_Data::CreateFindData();

if (ffblk == nullptr) {
return;
}

size_t name_len = strlen(fname);

if (ffblk->FindFirst(fname) && name_len == strlen(ffblk->GetFullName())) {
strncpy(fname, ffblk->GetFullName(), name_len + 1);
}

delete ffblk;
}
#endif

void Resolve_File(char* fname)
{
#ifndef _WIN32
// step through each sub-directory before going for the win
char* next = fname;
while (next = strchr(next, '/')) {
*next = '\0';
Resolve_File_Single(fname);
*next++ = '/';
}

Resolve_File_Single(fname);
#endif
}

bool Find_First(const char* fname, unsigned int mode, Find_File_Data** ffblk)
{
if (ffblk == nullptr) {
return false;
}
*ffblk = nullptr;

*ffblk = Find_File_Data::CreateFindData();
if ((*ffblk)->FindFirst(fname)) {
return true;
}

delete *ffblk;
*ffblk = nullptr;

return false;
}

bool Find_Next(Find_File_Data* ffblk)
{
if (ffblk == nullptr) {
return false;
}

return ffblk->FindNext();
}

void Find_Close(Find_File_Data* ffblk)
{
if (ffblk != nullptr) {
delete ffblk;
}
}
39 changes: 39 additions & 0 deletions code/file.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*******************************************************************************
* O P E N T S
*******************************************************************************
* SPDX-License-Identifier: GPL-3.0-or-later
* Copyright 2020-2024 Vanilla Conquer contributors
* Copyright 2026 OpenTS contributors
*
* Contains material derived from Vanilla Conquer (https://github.com/TheAssemblyArmada/Vanilla-Conquer).
* Modified by OpenTS contributors, 2026.
* See LICENSE.md for applicable additional terms and warranty disclaimers.
******************************************************************************/

#pragma once

void Resolve_File(char* fname);

class Find_File_Data
{
public:
static Find_File_Data* CreateFindData();

virtual ~Find_File_Data()
{
}
virtual const char* GetName() const = 0;
virtual const char* GetFullName() const
{
return nullptr;
};
virtual unsigned int GetTime() const = 0;

virtual bool FindFirst(const char* fname) = 0;
virtual bool FindNext() = 0;
virtual void Close() = 0;
};

extern bool Find_First(const char* fname, unsigned int mode, Find_File_Data** ffblk);
extern bool Find_Next(Find_File_Data* ffblk);
extern void Find_Close(Find_File_Data* ffblk);
Loading