Skip to content

Commit ae3b4a3

Browse files
authored
Handle limits better (#62)
* Update verbiage * rename linter script * do we need cstd17? * Compile C and C++ objects separately (#60) * builds working on macos * remove debug-vars target * attempt linux build * define M_PI manually * let's try using gnu standard? * add missing rule for linux/src dir * does order matter? * put linker flags at the end, where they were before * make macos consistent * convert long lines to multiline * try windows builds here we go... * have each OS only supply the impl not another unnecessary header * there is no std=c++11 flag for MSVC * fix include * does this work with MSVC? * remove old declarations * add missing extern C * add compat.h for cross platform limits * change rom_name to rom_filepath where appropriate * Use compat.h * set better defaults * use linux include
1 parent af1c1b4 commit ae3b4a3

File tree

10 files changed

+64
-34
lines changed

10 files changed

+64
-34
lines changed

linux/src/open_file_dialog.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "open_file_dialog.h"
2+
#include "compat.h"
23
#include <gtk/gtk.h>
34
#include <cstring>
45
#include <vector>
@@ -68,11 +69,11 @@ std::vector<std::string> open_file_dialog(const std::string &title, const std::s
6869
return result;
6970
}
7071

71-
int open_file_dialog(char *rom_name) {
72+
int open_file_dialog(char *rom_filepath) {
7273
std::vector<std::string> fileTypes = {"ch8", "CH8", "chip-8", "CHIP-8", "Chip-8"};
7374
const char* defaultDir = ""; // unify behavior: let OS choose last-used/home
7475
std::vector<std::string> files = open_file_dialog("Chip8", defaultDir, fileTypes);
7576
if (files.empty()) return 1;
76-
snprintf(rom_name, 256, "%s", files[0].c_str());
77+
snprintf(rom_filepath, PATH_MAX, "%s", files[0].c_str());
7778
return 0;
7879
}

macos/src/open_file_dialog.mm

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#import "open_file_dialog.h"
2+
#import "compat.h"
23
#import <Foundation/Foundation.h>
34
#import <Cocoa/Cocoa.h>
45
#include <string>
@@ -43,11 +44,11 @@
4344
return fileList;
4445
}
4546

46-
int open_file_dialog(char *rom_name) {
47+
int open_file_dialog(char *rom_filepath) {
4748
std::vector<std::string> fileTypes = {"ch8", "CH8", "chip-8", "CHIP-8", "Chip-8"};
4849
const char* defaultDir = ""; // unify behavior: let OS choose last-used/home
4950
std::vector<std::string> files = open_file_dialog("Chip8", defaultDir, fileTypes);
5051
if (files.empty()) return 1;
51-
snprintf(rom_name, 256, "%s", files[0].c_str());
52+
snprintf(rom_filepath, PATH_MAX, "%s", files[0].c_str());
5253
return 0;
5354
}

shared/chip8.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "profiles.h"
88
#include "sha256.h"
99
#include "toast.h"
10+
#include "compat.h"
1011
#include "open_file_dialog.h"
1112
#include <SDL2/SDL.h>
1213
#include <stdio.h>
@@ -193,10 +194,10 @@ int chip8_load_rom(const char *rom_filepath) {
193194

194195
} else {
195196
/* load ROM from GUI */
196-
char new_rom_name[PATH_MAX];
197-
open_file_dialog(new_rom_name) ?
197+
char new_rom_filepath[PATH_MAX];
198+
open_file_dialog(new_rom_filepath) ?
198199
printf("User aborted the open file dialog.\n") :
199-
chip8_load_rom(new_rom_name);
200+
chip8_load_rom(new_rom_filepath);
200201

201202
/* flip GUI toggle */
202203
gui.load_rom_flag = 0;

shared/chip8.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
extern "C" {
66
#endif
77

8+
#include "compat.h"
89
#include "bootrom.h" // Generated at build time from roms/Kiwi8_logo_2.ch8
910
#include "quirks.h"
1011
#include <stdlib.h>
@@ -62,7 +63,7 @@ struct chip8 {
6263
unsigned int rom_size;
6364

6465
/* rom profile tracking */
65-
char rom_filename[256]; /* basename of currently loaded ROM */
66+
char rom_filename[FILENAME_MAX]; /* basename of currently loaded ROM */
6667
int rom_loaded; /* 1 if user ROM loaded, 0 if bootrom */
6768

6869
/* registers */

shared/compat.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#ifndef COMPAT_H
2+
#define COMPAT_H
3+
4+
#include <limits.h>
5+
6+
// Handle missing PATH_MAX on some systems
7+
#if defined(__APPLE__)
8+
#include <sys/syslimits.h>
9+
#elif defined(__linux__)
10+
#include <linux/limits.h>
11+
#elif defined(_WIN32)
12+
#include <windows.h>
13+
#ifndef PATH_MAX
14+
#define PATH_MAX MAX_PATH
15+
#endif
16+
#endif
17+
18+
#ifndef PATH_MAX
19+
#define PATH_MAX 4096
20+
#endif
21+
#ifndef FILENAME_MAX
22+
#define FILENAME_MAX 256
23+
#endif
24+
#ifndef LINE_MAX
25+
#define LINE_MAX 2048
26+
#endif
27+
28+
#endif // COMPAT_H

shared/open_file_dialog.h

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,10 @@
55
extern "C" {
66
#endif
77

8-
#ifdef __APPLE__
9-
#include <limits.h> /* PATH_MAX */
10-
#endif
11-
12-
#ifdef _WIN32
13-
#include <windows.h> /* MAX_PATH */
14-
#define PATH_MAX MAX_PATH
15-
#endif
16-
17-
#ifdef __linux__
18-
#include <limits.h> /* PATH_MAX */
19-
#endif
20-
21-
int open_file_dialog(char *rom_name);
8+
int open_file_dialog(char *rom_filepath);
229

2310
#ifdef __cplusplus
2411
}
2512
#endif
2613

27-
#endif
14+
#endif // OPEN_FILE_DIALOG_H

shared/profiles.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "profiles.h"
2+
#include "compat.h"
23
#define STB_DS_IMPLEMENTATION
34
#include "stb_ds.h"
45
#include "sha256.h"
@@ -13,7 +14,7 @@
1314
static struct { sha256_hash_t key; struct profile value; } *profile_map = NULL;
1415

1516
/* Track which path we loaded profiles.ini from */
16-
static char loaded_profiles_path[512] = "";
17+
static char loaded_profiles_path[FILENAME_MAX] = "";
1718

1819
/* Quirk field descriptor table — generated from QUIRK_FIELDS X-macro in quirks.h */
1920
static const struct {
@@ -58,7 +59,7 @@ static void parse_profiles_ini(void) {
5859
FILE *file = fopen(loaded_profiles_path, "r");
5960
if (!file) return;
6061

61-
char line[512];
62+
char line[LINE_MAX];
6263
sha256_hash_t current_sha256 = {0};
6364
int has_current = 0;
6465
struct profile current_profile;
@@ -164,7 +165,7 @@ static int resolve_profiles_path(void) {
164165
}
165166

166167
/* Build search paths relative to executable */
167-
char search_paths[2][512];
168+
char search_paths[2][LINE_MAX];
168169
char *base_path = SDL_GetBasePath();
169170
if (!base_path) {
170171
printf("Warning: Could not determine executable path. Trying current directory.\n");

shared/profiles.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,13 @@ extern "C" {
88
#include <stdint.h>
99
#include "quirks.h"
1010
#include "sha256.h"
11+
#include "compat.h"
1112

1213
typedef struct { uint8_t bytes[32]; } sha256_hash_t;
1314

1415
struct profile {
1516
sha256_hash_t sha256;
16-
char rom_name[256];
17+
char rom_name[FILENAME_MAX];
1718
struct quirks quirks;
1819
};
1920

shared/usage.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#ifndef USAGE_H
22
#define USAGE_H
33

4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
48
/* Centralized usage/help text for CLI and GUI */
59
static const char *USAGE_TEXT =
610
"Usage: Kiwi8 [options] [rom_file]\n"
@@ -12,4 +16,8 @@ static const char *USAGE_TEXT =
1216
"\n"
1317
"Note: Quirks are configured per-ROM via profiles.ini or GUI.\n";
1418

19+
#ifdef __cplusplus
20+
}
21+
#endif
22+
1523
#endif /* USAGE_H */

windows/src/open_file_dialog.cc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
#include "open_file_dialog.h"
2+
#include "compat.h"
23
#include <windows.h>
34
#include <stdio.h>
45
#include <stdlib.h>
56
#include <string.h>
67

7-
int open_file_dialog(char *rom_name, char *filters) {
8+
int open_file_dialog(char *rom_filepath, char *filters) {
89
/* open file dialogue */
9-
char cwd[MAX_PATH];
10-
GetCurrentDirectory(MAX_PATH, cwd);
10+
char cwd[PATH_MAX];
11+
GetCurrentDirectory(PATH_MAX, cwd);
1112

1213
OPENFILENAME ofn;
1314

14-
char szFile[MAX_PATH];
15+
char szFile[PATH_MAX];
1516

1617
/* open a file name */
1718
ZeroMemory( &ofn , sizeof( ofn));
@@ -35,10 +36,10 @@ int open_file_dialog(char *rom_name, char *filters) {
3536
return 1;
3637
}
3738

38-
strcpy(rom_name, szFile);
39+
strcpy(rom_filepath, szFile);
3940
return 0;
4041
}
4142

42-
int open_file_dialog(char *rom_name) {
43-
return open_file_dialog(rom_name, "Chip8\0*.ch8\0All\0*.*\0");
43+
int open_file_dialog(char *rom_filepath) {
44+
return open_file_dialog(rom_filepath, "Chip8\0*.ch8\0All\0*.*\0");
4445
}

0 commit comments

Comments
 (0)