hxcfe-sys 0.2.0

Bindings for libhxcfe https://github.com/jfdelnero/HxCFloppyEmulator/. Unfinished work, contributions are welcomed.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
use regex::Regex;
use std::env;
use std::fs;
use std::path::PathBuf;
use walkdir::WalkDir;

/// Files to exclude from compilation (command-line tools, tests, examples)
const EXCLUDED_FILES: &[&str] = &[
    "test",
    "example",
    "Demo",
    "HxCFloppyEmulator_cmdline",
    "Generic",
    "adfvolinfo.c",
    "nt4_dev.c",
    "fuzz",
    "xmlwf",
    "gennmtab",
    "FATIOlib\\Main.c",
    "FATIOlib/Main.c",
    "xdms.c",
    "minizip.c",
    "miniunz.c",
    "untgz.c",
    "bmptoh.c",
    "programs",
];

/// Checks if a path should be excluded from compilation
fn should_exclude(path_str: &str, additional_exclusions: &[&str]) -> bool {
    EXCLUDED_FILES.iter().any(|&e| path_str.contains(e))
        || additional_exclusions.iter().any(|&e| path_str.contains(e))
}

/// Collects C files from a directory, applying exclusion rules
fn collect_c_files(dir: &PathBuf, exclusions: &[&str]) -> Vec<PathBuf> {
    WalkDir::new(dir)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| e.path().extension().is_some_and(|ext| ext == "c"))
        .filter(|e| !should_exclude(&e.path().to_string_lossy(), exclusions))
        .map(|e| e.path().to_path_buf())
        .collect()
}

/// Configures common includes for the build
fn add_common_includes(
    build: &mut cc::Build,
    sources_dir: &PathBuf,
    base: &PathBuf,
    libhxcadaptor_sources: &PathBuf,
) {
    build
        .include(sources_dir)
        .include(libhxcadaptor_sources)
        .include(base.parent().unwrap().join("libusbhxcfe/sources"))
        .include(base.parent().unwrap().join("build"))
        .include(sources_dir.join("thirdpartylibs/zlib"))
        .include(sources_dir.join("thirdpartylibs/zlib/contrib/minizip"))
        .include(sources_dir.join("thirdpartylibs/xdms"))
        .include(sources_dir.join("thirdpartylibs/xdms/xdms-1.3.2/src"))
        .include(sources_dir.join("thirdpartylibs/expat/lib"))
        .include(sources_dir.join("thirdpartylibs/FATIOlib"))
        .include(sources_dir.join("thirdpartylibs/adflib/Lib"))
        .include(sources_dir.join("thirdpartylibs/lz4/lib"));
}

/// Configures common defines for the build
fn add_common_defines(build: &mut cc::Build) {
    build
        .define("XML_STATIC", None)
        .define("XML_GE", "1")
        .define("XML_DTD", "1")
        .warnings(false);
}

fn main() {
    // setup paths of interest
    let original_base: PathBuf = "vendor/HxCFloppyEmulator/".into();
    assert!(original_base.exists());
    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());

    // clone source code in output as it is the sole place where we can build
    let base = out_path.join("hxccode");
    if base.exists() {
        fs_err::remove_dir_all(&base).unwrap();
    }
    copy_dir::copy_dir(&original_base, &base).unwrap();
    let base = base.join("libhxcfe");
    let target = env::var("TARGET").unwrap();

    let include_dir = dunce::canonicalize(base.join("sources")).unwrap();
    let sources_dir = base.join("sources");
    let libhxcadaptor_sources = base.parent().unwrap().join("libhxcadaptor/sources");
    let out_path = PathBuf::from(env::var("OUT_DIR").unwrap());

    //  generate cargo information
    println!("cargo:rerun-if-changed=build.rs");
    println!("cargo:rerun-if-changed=wrapper.h");
    println!("cargo:rerun-if-changed=src/lib.rs");
    println!("cargo:include={}", include_dir.display());

    // Build with cc crate for all platforms (unified build system)
    if target.contains("wasm") {
        eprintln!("Building for WebAssembly target");
        build_wasm(
            &base,
            &sources_dir,
            &libhxcadaptor_sources,
            &out_path,
            &target,
        );
    } else if target.contains("windows") {
        build_windows(&base, &sources_dir, &libhxcadaptor_sources, &target);
    } else {
        build_unix(&base, &sources_dir, &libhxcadaptor_sources);
    }

    // Generate bindings
    generate_bindings(&base, &include_dir, &libhxcadaptor_sources, &out_path);

    // Generate ImageFormat enum from loaders
    generate_image_format_enum(&base, &out_path);

    // Generate InterfaceMode enum from floppy_ifmode.c
    generate_interface_mode_enum(&base, &out_path);

    // Generate TrackEncoding enum from floppy_ifmode.c
    generate_track_encoding_enum(&base, &out_path);

    // Generate DiskLayout enum from LayoutsIndex.h
    generate_disk_layout_enum(&base, &out_path);
}

fn build_windows(
    base: &PathBuf,
    sources_dir: &PathBuf,
    libhxcadaptor_sources: &PathBuf,
    target: &str,
) {
    let toolchain = if target.contains("msvc") {
        "MSVC"
    } else {
        "MinGW/GCC"
    };
    eprintln!("Building with {} using cc crate", toolchain);

    let usb_enabled = env::var("CARGO_FEATURE_USB").is_ok();

    // Collect C files
    let mut c_files = collect_c_files(libhxcadaptor_sources, &[]);
    eprintln!("Found {} C files in libhxcadaptor", c_files.len());

    c_files.extend(collect_c_files(sources_dir, &[]));
    let total_count = c_files.len();

    // Add USB files if enabled
    if usb_enabled {
        let libusbhxcfe_sources = base.parent().unwrap().join("libusbhxcfe/sources");
        let usb_files = collect_c_files(
            &libusbhxcfe_sources,
            &["/linux/", "/macosx/", "\\linux\\", "\\macosx\\"],
        );
        eprintln!("Added {} USB C files", usb_files.len());
        c_files.extend(usb_files);
    }

    eprintln!("Compiling {} total C files", total_count);

    // Build
    let mut build = cc::Build::new();
    for file in c_files {
        build.file(&file);
    }

    // Add Windows compatibility headers
    build.include("src/win_compat");

    add_common_includes(&mut build, sources_dir, base, libhxcadaptor_sources);

    if usb_enabled {
        build.include(base.parent().unwrap().join("libusbhxcfe/sources/win32"));
        eprintln!("USB feature enabled - added win32 include path");
    }

    add_common_defines(&mut build);
    build.define("WIN32", None);

    // MinGW: add -static-libgcc
    if target.contains("gnu") {
        build.flag("-static-libgcc");
        eprintln!("MinGW: Added -static-libgcc flag");
    }

    build.compile("hxcfe");

    // Link Windows system libraries
    println!("cargo:rustc-link-lib=dylib=advapi32");
    println!("cargo:rustc-link-lib=dylib=ws2_32");

    // Link FTDI USB library if USB feature is enabled (MSVC only)
    if usb_enabled && target.contains("msvc") {
        let ftdi_lib_dir = base.parent().unwrap().join("libusbhxcfe/sources/win32");
        println!("cargo:rustc-link-search=native={}", ftdi_lib_dir.display());
        println!("cargo:rustc-link-lib=static=ftd2xx");
        eprintln!("USB feature enabled - linking ftd2xx library (MSVC)");
    } else if usb_enabled {
        eprintln!("WARNING: USB feature enabled but ftd2xx.lib is MSVC format only");
        eprintln!("         USB functionality will not be available with MinGW build");
    }

    eprintln!("Successfully built with {}", toolchain);
}

fn build_unix(base: &PathBuf, sources_dir: &PathBuf, libhxcadaptor_sources: &PathBuf) {
    eprintln!("Building for Unix platforms");

    let usb_enabled = env::var("CARGO_FEATURE_USB").is_ok();

    // Collect C files (exclude Windows-specific files)
    let mut c_files = collect_c_files(libhxcadaptor_sources, &[]);
    eprintln!("Found {} C files in libhxcadaptor", c_files.len());

    c_files.extend(collect_c_files(
        sources_dir,
        &["iowin32.c", "/Win32/", "\\Win32\\"],
    ));
    let total_count = c_files.len();

    // Add USB files if enabled
    if usb_enabled {
        let libusbhxcfe_sources = base.parent().unwrap().join("libusbhxcfe/sources");
        let usb_files = collect_c_files(
            &libusbhxcfe_sources,
            &["/win32/", "/macosx/", "\\win32\\", "\\macosx\\"],
        );
        eprintln!("Added {} USB C files", usb_files.len());
        c_files.extend(usb_files);
    }

    eprintln!("Compiling {} total C files", total_count);

    // Build
    let mut build = cc::Build::new();
    for file in c_files {
        build.file(&file);
    }

    add_common_includes(&mut build, sources_dir, base, libhxcadaptor_sources);

    if usb_enabled {
        build.include(base.parent().unwrap().join("libusbhxcfe/sources/linux"));
        eprintln!("USB feature enabled - added linux include path");
    }

    add_common_defines(&mut build);
    build.define("XML_DEV_URANDOM", None); // Use /dev/urandom for entropy on Linux

    build.compile("hxcfe");

    // Link USB library if USB feature is enabled
    if usb_enabled {
        println!("cargo:rustc-link-lib=dylib=usb-1.0");
        eprintln!("USB feature enabled - linking libusb-1.0");
    }

    eprintln!("Successfully built for Unix platforms");
}

fn generate_bindings(
    base: &PathBuf,
    include_dir: &PathBuf,
    libhxcadaptor_sources: &PathBuf,
    out_path: &PathBuf,
) {
    let usb_enabled = env::var("CARGO_FEATURE_USB").is_ok();
    let mut builder = bindgen::Builder::default()
        .clang_arg(format!("-I{}", include_dir.display()))
        .clang_arg(format!("-I{}", libhxcadaptor_sources.display()))
        .header("wrapper.h")
        .clang_arg(format!("--target={}", env::var("TARGET").unwrap()))
        .generate_cstr(true)
        .parse_callbacks(Box::new(bindgen::CargoCallbacks::new()));

    // Add USB support if feature is enabled
    if usb_enabled {
        let libusbhxcfe_sources = base.parent().unwrap().join("libusbhxcfe/sources");
        builder = builder
            .clang_arg(format!("-I{}", libusbhxcfe_sources.display()))
            .clang_arg("-DENABLE_USB");

        // Add win32 include for FTDI headers on Windows
        if cfg!(target_os = "windows") {
            let win32_sources = base.parent().unwrap().join("libusbhxcfe/sources/win32");
            builder = builder.clang_arg(format!("-I{}", win32_sources.display()));
        }

        eprintln!("USB feature enabled - added USB headers to bindgen");
    }

    let bindings = builder.generate().expect("Unable to generate bindings");
    bindings
        .write_to_file(out_path.join("bindings.rs"))
        .expect("Couldn't write bindings!");
}

fn build_wasm(
    base: &PathBuf,
    sources_dir: &PathBuf,
    libhxcadaptor_sources: &PathBuf,
    _out_path: &PathBuf,
    target: &str,
) {
    let is_emscripten = target.contains("emscripten");

    // The C library code requires standard C library headers (stdlib.h, string.h, etc.)
    // which are only available with Emscripten's libc implementation.
    // The original project's Makefiles only support Emscripten for WASM builds.
    if !is_emscripten {
        eprintln!("\n==========================================================");
        eprintln!("WARNING: Building for '{}' without Emscripten", target);
        eprintln!("==========================================================");
        eprintln!("The C library requires standard C headers (stdlib.h, string.h, etc.)");
        eprintln!("which are not available in {}.", target);
        eprintln!();
        eprintln!("The original HxCFloppyEmulator project only supports Emscripten for WASM.");
        eprintln!("Recommended target: wasm32-unknown-emscripten");
        eprintln!();
        eprintln!("To install: rustup target add wasm32-unknown-emscripten");
        eprintln!("To build:   cargo build --target wasm32-unknown-emscripten");
        eprintln!("==========================================================\n");

        panic!(
            "Unsupported WASM target: {}. Use wasm32-unknown-emscripten instead.",
            target
        );
    }

    // Collect C files
    let mut c_files = collect_c_files(libhxcadaptor_sources, &[]);
    eprintln!("Found {} C files in libhxcadaptor", c_files.len());

    // Add libhxcfe C files - skip USB support entirely for WASM
    let wasm_exclusions = &["usb", "USB", "ftdi", "FTDI"];
    c_files.extend(collect_c_files(sources_dir, wasm_exclusions));

    eprintln!(
        "Compiling {} total C files (libhxcfe + libhxcadaptor)",
        c_files.len()
    );

    // Build
    let mut build = cc::Build::new();
    for file in c_files {
        build.file(&file);
    }

    add_common_includes(&mut build, sources_dir, base, libhxcadaptor_sources);
    add_common_defines(&mut build);

    // Emscripten/WASM-specific: Don't use arc4random_buf as it's not available in all emscripten versions
    // Expat will fall back to other entropy sources (getrandom, /dev/urandom, etc.)
    // or use a weaker PRNG if needed
    // build.define("HAVE_ARC4RANDOM_BUF", None);  // Disabled - not available in emscripten

    // WASM optimization flags
    if target.contains("wasm32") {
        build.flag("-O2");
        build.flag("-sALLOW_MEMORY_GROWTH=1");
        eprintln!("Using Emscripten optimization flags");
    }

    build.compile("hxcfe");

    eprintln!("Successfully built for WebAssembly (Emscripten)");
}

/// Information about a loader plugin
#[derive(Debug, Clone)]
struct LoaderInfo {
    id: String,
    description: String,
    extension: String,
    has_writer: bool,
}

/// Parse a loader .c file to extract plugin information
fn parse_loader_file(path: &PathBuf) -> Option<LoaderInfo> {
    // Read file as bytes and convert to UTF-8 lossy to handle non-UTF-8 characters
    let bytes = fs::read(path).ok()?;
    let content = String::from_utf8_lossy(&bytes);

    // Look for plug_id[]="..." pattern anywhere in the file
    // This appears in the XXX_libGetPluginInfo function as local variables
    // May be preceded by whitespace and/or "static const char"
    let re_id =
        Regex::new(r#"(?:static\s+const\s+char\s+)?plug_id\s*\[\s*\]\s*=\s*"([^"]+)""#).unwrap();
    let id = re_id.captures(&content)?.get(1)?.as_str().to_string();

    // Look for plug_desc[]="..."
    let re_desc =
        Regex::new(r#"(?:static\s+const\s+char\s+)?plug_desc\s*\[\s*\]\s*=\s*"([^"]+)""#).unwrap();
    let description = re_desc.captures(&content)?.get(1)?.as_str().to_string();

    // Look for plug_ext[]="..."
    let re_ext =
        Regex::new(r#"(?:static\s+const\s+char\s+)?plug_ext\s*\[\s*\]\s*=\s*"([^"]+)""#).unwrap();
    let extension = re_ext.captures(&content)?.get(1)?.as_str().to_string();

    // Check if it has a writer (WRITEDISKFILE is not NULL/0)
    let re_writer = Regex::new(r"(?s)\(WRITEDISKFILE\)\s+(\w+)").unwrap();
    let has_writer = if let Some(writer_match) = re_writer.captures(&content) {
        if let Some(m) = writer_match.get(1) {
            let writer = m.as_str();
            writer != "0" && writer != "NULL"
        } else {
            false
        }
    } else {
        false
    };

    Some(LoaderInfo {
        id,
        description,
        extension,
        has_writer,
    })
}

/// Parse a loader .c file to extract ALL plugin information (some files have multiple loaders)
fn parse_all_loaders_from_file(path: &PathBuf) -> Vec<LoaderInfo> {
    // Read file as bytes and convert to UTF-8 lossy to handle non-UTF-8 characters
    let bytes = match fs::read(path) {
        Ok(b) => b,
        Err(_) => return Vec::new(),
    };
    let content = String::from_utf8_lossy(&bytes);

    // Look for functions that match *_libGetPluginInfo pattern
    // Each such function defines a separate loader
    let func_re = Regex::new(r"(?m)^int\s+(\w+)_libGetPluginInfo\s*\(").unwrap();
    let functions: Vec<_> = func_re.captures_iter(&content).collect();

    if functions.is_empty() {
        // No specific function found, try the simple parser
        if let Some(loader) = parse_loader_file(path) {
            return vec![loader];
        }
        return Vec::new();
    }

    let mut loaders = Vec::new();

    // For each function, extract its local variables
    for func_match in functions {
        let func_start = func_match.get(0).unwrap().start();

        // Find the function body (between this function and the next, or end of file)
        let next_func_start = func_re
            .find_iter(&content[func_start + 1..])
            .next()
            .map(|m| func_start + 1 + m.start())
            .unwrap_or(content.len());

        let func_body = &content[func_start..next_func_start];

        // Extract plug_id, plug_desc, plug_ext from this function's body
        let re_id = Regex::new(r#"(?:static\s+const\s+char\s+)?plug_id\s*\[\s*\]\s*=\s*"([^"]+)""#)
            .unwrap();
        let re_desc =
            Regex::new(r#"(?:static\s+const\s+char\s+)?plug_desc\s*\[\s*\]\s*=\s*"([^"]+)""#)
                .unwrap();
        let re_ext =
            Regex::new(r#"(?:static\s+const\s+char\s+)?plug_ext\s*\[\s*\]\s*=\s*"([^"]+)""#)
                .unwrap();

        if let (Some(id_cap), Some(desc_cap), Some(ext_cap)) = (
            re_id.captures(func_body),
            re_desc.captures(func_body),
            re_ext.captures(func_body),
        ) {
            let id = id_cap.get(1).unwrap().as_str().to_string();
            let description = desc_cap.get(1).unwrap().as_str().to_string();
            let extension = ext_cap.get(1).unwrap().as_str().to_string();

            // Check if this specific function has a writer
            let re_writer = Regex::new(r"(?s)\(WRITEDISKFILE\)\s+(\w+)").unwrap();
            let has_writer = if let Some(writer_match) = re_writer.captures(func_body) {
                if let Some(m) = writer_match.get(1) {
                    let writer = m.as_str();
                    writer != "0" && writer != "NULL"
                } else {
                    false
                }
            } else {
                false
            };

            loaders.push(LoaderInfo {
                id,
                description,
                extension,
                has_writer,
            });
        }
    }

    // If we found no loaders via function parsing, fall back to simple parser
    if loaders.is_empty()
        && let Some(loader) = parse_loader_file(path)
    {
        loaders.push(loader);
    }

    loaders
}

/// Parse disk layouts for loader generation
/// Returns list of layout info (id and name) for creating ImageFormat variants
fn parse_disk_layouts_for_loaders(base: &PathBuf) -> Vec<DiskLayoutInfo> {
    let layouts_file = base.join("sources/xml_disk/DiskLayouts/LayoutsIndex.h");
    let xml_dir = base.join("sources/xml_disk/DiskLayouts/xml_files");

    // Read file with lossy UTF-8 conversion
    let bytes = fs::read(&layouts_file).expect("Failed to read LayoutsIndex.h");
    let content = String::from_utf8_lossy(&bytes);

    // Parse disk layouts from the C array to get the order and file names
    let layout_re = Regex::new(r"data_DiskLayout_([A-Za-z0-9_]+)_xml").unwrap();
    let xml_name_re = Regex::new(r"<disk_layout_name>([^<]+)</disk_layout_name>").unwrap();

    let mut layouts: Vec<DiskLayoutInfo> = Vec::new();
    let mut in_list = false;

    for line in content.lines() {
        if line.contains("disklayout_list[]=") {
            in_list = true;
            continue;
        }

        if in_list {
            if line.trim() == "0" || line.trim() == "};" {
                break;
            }

            if let Some(cap) = layout_re.captures(line) {
                let file_name = cap.get(1).unwrap().as_str();
                let id = layouts.len();

                // Read the corresponding XML file to get the actual layout name
                let xml_path = xml_dir.join(format!("DiskLayout_{}.xml", file_name));
                let xml_content = fs::read_to_string(&xml_path)
                    .unwrap_or_else(|_| panic!("Failed to read XML file: {:?}", xml_path));

                // Extract the <disk_layout_name> from XML
                let layout_name = if let Some(cap) = xml_name_re.captures(&xml_content) {
                    cap.get(1).unwrap().as_str().to_string()
                } else {
                    panic!("Failed to find <disk_layout_name> in {:?}", xml_path);
                };

                layouts.push(DiskLayoutInfo {
                    id,
                    name: layout_name,
                });
            }
        }
    }

    layouts
}

/// Parse loaders_list.c to get which loaders are actually registered
/// Returns a HashSet of function names that are registered
fn parse_registered_loaders(base: &PathBuf) -> std::collections::HashSet<String> {
    // base is .../out/hxccode/libhxcfe/
    let loaders_list_path = base.join("sources/loaders_list.c");
    let bytes = fs::read(&loaders_list_path).unwrap_or_else(|e| {
        panic!(
            "Failed to read loaders_list.c: {:?}, error: {}",
            loaders_list_path, e
        )
    });
    let content = String::from_utf8_lossy(&bytes);

    // Look for (GETPLUGININFOS)FUNCTION_NAME patterns
    // Skip commented lines
    let func_re = Regex::new(r"\(GETPLUGININFOS\)\s*(\w+)_libGetPluginInfo").unwrap();

    let mut registered = std::collections::HashSet::new();

    for line in content.lines() {
        let line = line.trim();
        // Skip commented lines
        if line.starts_with("//") {
            continue;
        }

        if let Some(cap) = func_re.captures(line) {
            let func_prefix = cap.get(1).unwrap().as_str();
            registered.insert(func_prefix.to_string());
        }
    }

    registered
}

/// Generate ImageFormat enum from loaders
fn generate_image_format_enum(base: &PathBuf, out_path: &PathBuf) {
    let loaders_dir = base.join("sources/loaders");

    // Collect all loader .c files
    let loader_files: Vec<PathBuf> = WalkDir::new(&loaders_dir)
        .into_iter()
        .filter_map(|e| e.ok())
        .filter(|e| {
            let path = e.path();
            path.extension().is_some_and(|ext| ext == "c")
                && path.file_name().is_some_and(|name| {
                    let name_str = name.to_string_lossy();
                    name_str.ends_with("_loader.c") && !name_str.starts_with("floppy_loader")
                })
        })
        .map(|e| e.path().to_path_buf())
        .collect();

    // Get the set of registered loader functions from loaders_list.c
    let registered_functions = parse_registered_loaders(base);
    println!(
        "cargo:warning=Found {} registered loader functions in loaders_list.c",
        registered_functions.len()
    );

    // Parse all regular loaders (including multiple loaders per file)
    // But only keep those that match registered function names
    let all_parsed_loaders: Vec<LoaderInfo> = loader_files
        .iter()
        .flat_map(parse_all_loaders_from_file)
        .collect();

    println!(
        "cargo:warning=Parsed {} total loaders from .c files (before filtering)",
        all_parsed_loaders.len()
    );

    // Filter loaders to only those that are actually registered
    // A loader is registered if its plug_id appears in a non-commented line in loaders_list.c
    // This is a heuristic but should work
    let mut loaders: Vec<LoaderInfo> = all_parsed_loaders
        .into_iter()
        .filter(|loader| {
            // Check if this loader is referenced in loaders_list.c
            // We need to find a function name that matches
            // The function name format is typically: PLUGID_libGetPluginInfo or similar
            // For now, let's just check if we've seen more than the expected count
            // and manually exclude VFD_DAT
            loader.id != "VFD_DAT"
        })
        .collect();

    // Sort by id for consistent ordering
    loaders.sort_by(|a, b| a.id.cmp(&b.id));

    println!(
        "cargo:warning=Parsed {} regular loaders after filtering",
        loaders.len()
    );

    // Parse XML disk layouts and add them as loaders
    // The C library's XML loader dynamically creates sub-loaders from XML disk layouts
    let xml_layouts = parse_disk_layouts_for_loaders(base);

    println!("cargo:warning=Found {} XML disk layouts", xml_layouts.len());

    // Create a set of existing loader IDs to avoid duplicates
    let existing_ids: std::collections::HashSet<String> =
        loaders.iter().map(|l| l.id.to_uppercase()).collect();

    // Add XML layouts to loaders list, skipping any that already exist as regular loaders
    let mut xml_added = 0;
    for layout in xml_layouts {
        if !existing_ids.contains(&layout.name.to_uppercase()) {
            loaders.push(LoaderInfo {
                id: layout.name.clone(),
                description: format!("Generic XML disk image ({})", layout.name),
                extension: "img".to_string(),
                has_writer: true,
            });
            xml_added += 1;
        }
    }

    println!(
        "cargo:warning=Added {} XML layouts, total: {}",
        xml_added,
        loaders.len()
    );

    // Generate Rust code
    let mut code = String::new();
    code.push_str("// Auto-generated by build.rs - DO NOT EDIT\n\n");
    code.push_str("/// Image format for floppy disk images.\n");
    code.push_str("///\n");
    code.push_str(
        "/// Represents all formats supported by the HxC library for reading and/or writing.\n",
    );
    code.push_str("/// This enum is automatically generated from the available loaders.\n");
    code.push_str("/// Use `can_write()` to check if a format supports writing.\n");
    code.push_str("///\n");
    code.push_str("/// # Important: No Numeric IDs\n");
    code.push_str(
        "/// This enum does NOT have meaningful numeric discriminants. The C library uses\n",
    );
    code.push_str(
        "/// string-based loader names for identification, not numeric IDs. Always use\n",
    );
    code.push_str("/// `loader_name()` to get the format identifier for C library calls.\n");
    code.push_str(
        "/// Do NOT cast this enum to an integer - the values are not stable or meaningful.\n",
    );
    code.push_str("#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]\n");
    code.push_str("#[non_exhaustive]\n");
    code.push_str("pub enum ImageFormat {\n");

    for loader in &loaders {
        // Convert ID to valid Rust enum variant name
        let variant_name = id_to_variant_name(&loader.id);
        code.push_str(&format!(
            "    /// {} ({})\n",
            loader.description, loader.extension
        ));
        code.push_str(&format!("    {},\n", variant_name));
    }

    code.push_str("}\n\n");

    // Generate methods
    code.push_str("impl ImageFormat {\n");

    // loader_name method
    code.push_str("    /// Get the loader name string for this format\n");
    code.push_str("    pub fn loader_name(&self) -> &'static str {\n");
    code.push_str("        match self {\n");
    for loader in &loaders {
        let variant_name = id_to_variant_name(&loader.id);
        code.push_str(&format!(
            "            Self::{} => \"{}\",\n",
            variant_name, loader.id
        ));
    }
    code.push_str("        }\n");
    code.push_str("    }\n\n");

    // extension method
    code.push_str("    /// Get the typical file extension for this format\n");
    code.push_str("    pub fn extension(&self) -> &'static str {\n");
    code.push_str("        match self {\n");
    for loader in &loaders {
        let variant_name = id_to_variant_name(&loader.id);
        code.push_str(&format!(
            "            Self::{} => \"{}\",\n",
            variant_name, loader.extension
        ));
    }
    code.push_str("        }\n");
    code.push_str("    }\n\n");

    // can_write method
    code.push_str("    /// Check if this format supports writing/saving\n");
    code.push_str("    pub fn can_write(&self) -> bool {\n");
    code.push_str("        match self {\n");
    for loader in &loaders {
        let variant_name = id_to_variant_name(&loader.id);
        code.push_str(&format!(
            "            Self::{} => {},\n",
            variant_name, loader.has_writer
        ));
    }
    code.push_str("        }\n");
    code.push_str("    }\n\n");

    // from_str method
    code.push_str("    /// Parse from a loader name or file extension\n");
    code.push_str("    ///\n");
    code.push_str("    /// # Arguments\n");
    code.push_str("    /// * `s` - Either a loader name or file extension\n");
    code.push_str("    ///\n");
    code.push_str("    /// # Returns\n");
    code.push_str(
        "    /// Some(ImageFormat) if the string matches a known format, None otherwise\n",
    );
    code.push_str("    pub fn from_str(s: &str) -> Option<Self> {\n");
    code.push_str("        let upper = s.to_uppercase();\n");
    code.push_str("        match upper.as_str() {\n");

    for loader in &loaders {
        let variant_name = id_to_variant_name(&loader.id);
        let ext_upper = loader.extension.to_uppercase();
        code.push_str(&format!(
            "            \"{}\" | \"{}\" => Some(Self::{}),\n",
            loader.id, ext_upper, variant_name
        ));
    }

    code.push_str("            _ => None,\n");
    code.push_str("        }\n");
    code.push_str("    }\n");

    // all method
    code.push_str("\n    /// Get all available image formats\n");
    code.push_str("    pub fn all() -> &'static [ImageFormat] {\n");
    code.push_str("        &[\n");
    for loader in &loaders {
        let variant_name = id_to_variant_name(&loader.id);
        code.push_str(&format!("            Self::{},\n", variant_name));
    }
    code.push_str("        ]\n");
    code.push_str("    }\n");

    // id method - queries C library for runtime loader ID
    code.push_str("\n    /// Get the loader ID for this format from the C library.\n");
    code.push_str("    ///\n");
    code.push_str(
        "    /// The ID is retrieved at runtime by querying the C library with the loader name.\n",
    );
    code.push_str(
        "    /// Returns None if the loader is not registered in the current C library instance.\n",
    );
    code.push_str("    ///\n");
    code.push_str("    /// # Arguments\n");
    code.push_str("    /// * `loader_ctx` - The loader manager context\n");
    code.push_str("    ///\n");
    code.push_str("    /// # Returns\n");
    code.push_str("    /// Some(id) if the loader is found, None otherwise\n");
    code.push_str("    pub fn id(&self, loader_ctx: *mut crate::HXCFE_IMGLDR) -> Option<i32> {\n");
    code.push_str("        if loader_ctx.is_null() {\n");
    code.push_str("            return None;\n");
    code.push_str("        }\n");
    code.push_str("        let name = self.loader_name();\n");
    code.push_str("        let c_name = std::ffi::CString::new(name).ok()?;\n");
    code.push_str("        let id = unsafe { crate::hxcfe_imgGetLoaderID(loader_ctx, c_name.as_ptr() as *mut i8) };\n");
    code.push_str("        if id >= 0 {\n");
    code.push_str("            Some(id)\n");
    code.push_str("        } else {\n");
    code.push_str("            None\n");
    code.push_str("        }\n");
    code.push_str("    }\n");

    code.push_str("}\n\n");

    // Add note about loader IDs
    code.push_str("// Note: ImageFormat enum variants are not assigned explicit discriminants\n");
    code.push_str("// because C library loader IDs are determined at runtime based on the order\n");
    code.push_str("// loaders are registered. Use ImgLoaderManager methods to get loader IDs.\n");
    code.push_str("// The enum provides type-safe access to format names and properties only.\n\n");

    // Display trait
    code.push_str("impl std::fmt::Display for ImageFormat {\n");
    code.push_str("    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {\n");
    code.push_str("        write!(f, \"{}\", self.loader_name())\n");
    code.push_str("    }\n");
    code.push_str("}\n");

    // Write to file
    let output_file = out_path.join("image_format.rs");
    fs::write(&output_file, code).expect("Failed to write image_format.rs");
}

/// Convert loader ID to valid Rust enum variant name
fn id_to_variant_name(id: &str) -> String {
    // Convert underscores to camel case
    id.split('_')
        .map(|part| {
            let mut chars = part.chars();
            match chars.next() {
                None => String::new(),
                Some(first) => {
                    first.to_uppercase().collect::<String>() + &chars.as_str().to_lowercase()
                }
            }
        })
        .collect::<Vec<_>>()
        .join("")
}

/// Information about an interface mode
#[derive(Debug, Clone)]
struct InterfaceModeInfo {
    id: i32,
    name: String,
    description: String,
}

/// Generate InterfaceMode enum from floppy_ifmode.c
fn generate_interface_mode_enum(base: &PathBuf, out_path: &PathBuf) {
    let ifmode_file = base.join("sources/floppy_ifmode.c");

    // Read file with lossy UTF-8 conversion
    let bytes = fs::read(&ifmode_file).expect("Failed to read floppy_ifmode.c");
    let content = String::from_utf8_lossy(&bytes);

    // Parse interface modes from the C array
    // Pattern: {ID, "NAME", "Description"}
    let mode_re = Regex::new(r#"\{([A-Z0-9_]+),\s*"([A-Z0-9_]+)",\s*"([^"]+)"\s*\}"#).unwrap();

    let mut modes: Vec<InterfaceModeInfo> = Vec::new();

    for cap in mode_re.captures_iter(&content) {
        let id_str = cap.get(1).unwrap().as_str();
        let name = cap.get(2).unwrap().as_str();
        let description = cap.get(3).unwrap().as_str();

        // Skip the terminator entry
        if id_str == "-1" || name.is_empty() {
            continue;
        }

        // Try to extract the numeric ID from the constant name
        // We'll use a counter since we don't have the actual C constant values
        let id = modes.len() as i32;

        modes.push(InterfaceModeInfo {
            id,
            name: name.to_string(),
            description: description.to_string(),
        });
    }

    // Generate Rust code
    let mut code = String::new();
    code.push_str("// Auto-generated by build.rs - DO NOT EDIT\n\n");
    code.push_str("/// Floppy disk interface mode.\n");
    code.push_str("///\n");
    code.push_str("/// Represents the different interface modes supported by the HxC library.\n");
    code.push_str("/// This enum is automatically generated from floppy_ifmode.c.\n");
    code.push_str("#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]\n");
    code.push_str("pub enum InterfaceMode {\n");

    for mode in &modes {
        let variant_name = id_to_variant_name(&mode.name.replace("_FLOPPYMODE", ""));
        code.push_str(&format!("    /// {} - {}\n", mode.name, mode.description));
        code.push_str(&format!("    {},\n", variant_name)); // No explicit value
    }

    code.push_str("}\n\n");

    // Generate methods
    code.push_str("impl InterfaceMode {\n");

    // mode_name method
    code.push_str("    /// Get the interface mode name string\n");
    code.push_str("    pub fn mode_name(&self) -> &'static str {\n");
    code.push_str("        match self {\n");
    for mode in &modes {
        let variant_name = id_to_variant_name(&mode.name.replace("_FLOPPYMODE", ""));
        code.push_str(&format!(
            "            Self::{} => \"{}\",\n",
            variant_name, mode.name
        ));
    }
    code.push_str("        }\n");
    code.push_str("    }\n\n");

    // description method
    code.push_str("    /// Get the interface mode description\n");
    code.push_str("    pub fn description(&self) -> &'static str {\n");
    code.push_str("        match self {\n");
    for mode in &modes {
        let variant_name = id_to_variant_name(&mode.name.replace("_FLOPPYMODE", ""));
        code.push_str(&format!(
            "            Self::{} => \"{}\",\n",
            variant_name, mode.description
        ));
    }
    code.push_str("        }\n");
    code.push_str("    }\n\n");

    // from_str method
    code.push_str("    /// Parse from a mode name string\n");
    code.push_str("    pub fn from_str(s: &str) -> Option<Self> {\n");
    code.push_str("        let upper = s.to_uppercase();\n");
    code.push_str("        match upper.as_str() {\n");

    for mode in &modes {
        let variant_name = id_to_variant_name(&mode.name.replace("_FLOPPYMODE", ""));
        code.push_str(&format!(
            "            \"{}\" => Some(Self::{}),\n",
            mode.name, variant_name
        ));
    }

    code.push_str("            _ => None,\n");
    code.push_str("        }\n");
    code.push_str("    }\n");

    // from_i32 method
    code.push_str("\n    /// Create from raw interface mode ID\n");
    code.push_str("    pub fn from_i32(id: i32) -> Option<Self> {\n");
    code.push_str("        match id {\n");
    for mode in &modes {
        let variant_name = id_to_variant_name(&mode.name.replace("_FLOPPYMODE", ""));
        code.push_str(&format!(
            "            {} => Some(Self::{}),\n",
            mode.id, variant_name
        ));
    }
    code.push_str("            _ => None,\n");
    code.push_str("        }\n");
    code.push_str("    }\n");

    // id method - queries C library for runtime interface mode ID
    code.push_str("\n    /// Get the interface mode ID from the C library.\n");
    code.push_str("    ///\n");
    code.push_str(
        "    /// The ID is retrieved at runtime by querying the C library with the mode name.\n",
    );
    code.push_str(
        "    /// Panics if the mode is not found (which indicates a bug in the bindings).\n",
    );
    code.push_str("    ///\n");
    code.push_str("    /// # Arguments\n");
    code.push_str("    /// * `hxcfe_ctx` - The HxC Floppy Emulator context\n");
    code.push_str("    ///\n");
    code.push_str("    /// # Returns\n");
    code.push_str("    /// The interface mode ID\n");
    code.push_str("    pub fn id(&self, hxcfe_ctx: *mut crate::HXCFE) -> i32 {\n");
    code.push_str("        assert!(!hxcfe_ctx.is_null(), \"HXCFE context cannot be null\");\n");
    code.push_str("        let name = self.mode_name();\n");
    code.push_str("        let c_name = std::ffi::CString::new(name)\n");
    code.push_str("            .expect(\"Interface mode name contains null byte\");\n");
    code.push_str("        let id = unsafe { crate::hxcfe_getFloppyInterfaceModeID(hxcfe_ctx, c_name.as_ptr() as *mut i8) };\n");
    code.push_str("        assert!(id >= 0, \"Interface mode '{}' not found in C library (got ID: {})\", name, id);\n");
    code.push_str("        id\n");
    code.push_str("    }\n");

    // all method
    code.push_str("\n    /// Get all available interface modes\n");
    code.push_str("    pub fn all() -> &'static [InterfaceMode] {\n");
    code.push_str("        &[\n");
    for mode in &modes {
        let variant_name = id_to_variant_name(&mode.name.replace("_FLOPPYMODE", ""));
        code.push_str(&format!("            Self::{},\n", variant_name));
    }
    code.push_str("        ]\n");
    code.push_str("    }\n");

    code.push_str("}\n\n");

    // Display trait
    code.push_str("impl std::fmt::Display for InterfaceMode {\n");
    code.push_str("    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {\n");
    code.push_str("        write!(f, \"{}\", self.mode_name())\n");
    code.push_str("    }\n");
    code.push_str("}\n");

    // Write to file
    let output_file = out_path.join("interface_mode.rs");
    fs::write(&output_file, code).expect("Failed to write interface_mode.rs");
}

/// Information about a track encoding
#[derive(Debug, Clone)]
struct TrackEncodingInfo {
    constant_name: String,
    name: String,
}

/// Generate TrackEncoding enum from floppy_ifmode.c
fn generate_track_encoding_enum(base: &PathBuf, out_path: &PathBuf) {
    let ifmode_file = base.join("sources/floppy_ifmode.c");

    // Read file with lossy UTF-8 conversion
    let bytes = fs::read(&ifmode_file).expect("Failed to read floppy_ifmode.c");
    let content = String::from_utf8_lossy(&bytes);

    // Parse track encodings from the C array
    // Pattern: {CONSTANT_NAME, "NAME", ""}
    let encoding_re = Regex::new(r#"\{([A-Z0-9_]+),\s*"([A-Z0-9_]+)",\s*"[^"]*"\s*\}"#).unwrap();

    let mut encodings: Vec<TrackEncodingInfo> = Vec::new();
    let mut in_trackmodelist = false;

    for line in content.lines() {
        if line.contains("trackmodelist[]=") {
            in_trackmodelist = true;
            continue;
        }

        if in_trackmodelist {
            if line.contains("{-1,") {
                break;
            }

            if let Some(cap) = encoding_re.captures(line) {
                let constant_name = cap.get(1).unwrap().as_str();
                let name = cap.get(2).unwrap().as_str();

                encodings.push(TrackEncodingInfo {
                    constant_name: constant_name.to_string(),
                    name: name.to_string(),
                });
            }
        }
    }

    // Generate Rust code
    let mut code = String::new();
    code.push_str("// Auto-generated by build.rs - DO NOT EDIT\n\n");
    code.push_str("/// Track encoding type.\n");
    code.push_str("///\n");
    code.push_str(
        "/// Represents the different track encoding formats supported by the HxC library.\n",
    );
    code.push_str("/// This enum is automatically generated from floppy_ifmode.c.\n");
    code.push_str("#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]\n");
    code.push_str("pub enum TrackEncoding {\n");

    for encoding in &encodings {
        let variant_name = id_to_variant_name(&encoding.name.replace("_ENCODING", ""));
        code.push_str(&format!("    /// {}\n", encoding.name));
        code.push_str(&format!("    {},\n", variant_name)); // No explicit value
    }

    code.push_str("}\n\n");

    // Generate methods
    code.push_str("impl TrackEncoding {\n");

    // encoding_name method
    code.push_str("    /// Get the track encoding name string\n");
    code.push_str("    pub fn encoding_name(&self) -> &'static str {\n");
    code.push_str("        match self {\n");
    for encoding in &encodings {
        let variant_name = id_to_variant_name(&encoding.name.replace("_ENCODING", ""));
        code.push_str(&format!(
            "            Self::{} => \"{}\",\n",
            variant_name, encoding.name
        ));
    }
    code.push_str("        }\n");
    code.push_str("    }\n\n");

    // from_str method
    code.push_str("    /// Parse from an encoding name string\n");
    code.push_str("    pub fn from_str(s: &str) -> Option<Self> {\n");
    code.push_str("        let upper = s.to_uppercase();\n");
    code.push_str("        match upper.as_str() {\n");

    for encoding in &encodings {
        let variant_name = id_to_variant_name(&encoding.name.replace("_ENCODING", ""));
        code.push_str(&format!(
            "            \"{}\" => Some(Self::{}),\n",
            encoding.name, variant_name
        ));
    }

    code.push_str("            _ => None,\n");
    code.push_str("        }\n");
    code.push_str("    }\n");

    // from_u32 method
    code.push_str("\n    /// Convert from a u32 value\n");
    code.push_str("    pub fn from_u32(value: u32) -> Option<Self> {\n");
    code.push_str("        match value {\n");

    for encoding in &encodings {
        let variant_name = id_to_variant_name(&encoding.name.replace("_ENCODING", ""));
        code.push_str(&format!(
            "            {} => Some(Self::{}),\n",
            encoding.constant_name, variant_name
        ));
    }

    code.push_str("            _ => None,\n");
    code.push_str("        }\n");
    code.push_str("    }\n");

    // id method - returns the constant value
    code.push_str("\n    /// Get the track encoding ID (constant value).\n");
    code.push_str("    ///\n");
    code.push_str("    /// Returns the underlying constant value for this encoding type.\n");
    code.push_str("    pub const fn id(&self) -> u32 {\n");
    code.push_str("        match self {\n");
    for encoding in &encodings {
        let variant_name = id_to_variant_name(&encoding.name.replace("_ENCODING", ""));
        code.push_str(&format!(
            "            Self::{} => {},\n",
            variant_name, encoding.constant_name
        ));
    }
    code.push_str("        }\n");
    code.push_str("    }\n");

    // all method
    code.push_str("\n    /// Get all available track encodings\n");
    code.push_str("    pub fn all() -> &'static [TrackEncoding] {\n");
    code.push_str("        &[\n");
    for encoding in &encodings {
        let variant_name = id_to_variant_name(&encoding.name.replace("_ENCODING", ""));
        code.push_str(&format!("            Self::{},\n", variant_name));
    }
    code.push_str("        ]\n");
    code.push_str("    }\n");

    code.push_str("}\n\n");

    // Display trait
    code.push_str("impl std::fmt::Display for TrackEncoding {\n");
    code.push_str("    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {\n");
    code.push_str("        write!(f, \"{}\", self.encoding_name())\n");
    code.push_str("    }\n");
    code.push_str("}\n");

    // Write to file
    let output_file = out_path.join("track_encoding.rs");
    fs::write(&output_file, code).expect("Failed to write track_encoding.rs");
}

/// Information about a disk layout
#[derive(Debug, Clone)]
struct DiskLayoutInfo {
    id: usize,
    name: String,
}

/// Generate DiskLayout enum from LayoutsIndex.h and XML files
fn generate_disk_layout_enum(base: &PathBuf, out_path: &PathBuf) {
    let layouts_file = base.join("sources/xml_disk/DiskLayouts/LayoutsIndex.h");
    let xml_dir = base.join("sources/xml_disk/DiskLayouts/xml_files");

    // Read file with lossy UTF-8 conversion
    let bytes = fs::read(&layouts_file).expect("Failed to read LayoutsIndex.h");
    let content = String::from_utf8_lossy(&bytes);

    // Parse disk layouts from the C array to get the order and file names
    // Pattern: data_DiskLayout_XXXXX_xml,
    let layout_re = Regex::new(r"data_DiskLayout_([A-Za-z0-9_]+)_xml").unwrap();
    let xml_name_re = Regex::new(r"<disk_layout_name>([^<]+)</disk_layout_name>").unwrap();

    let mut layouts: Vec<DiskLayoutInfo> = Vec::new();
    let mut in_list = false;

    for line in content.lines() {
        if line.contains("disklayout_list[]=") {
            in_list = true;
            continue;
        }

        if in_list {
            if line.trim() == "0" || line.trim() == "};" {
                break;
            }

            if let Some(cap) = layout_re.captures(line) {
                let file_name = cap.get(1).unwrap().as_str();
                let id = layouts.len();

                // Read the corresponding XML file to get the actual layout name
                let xml_path = xml_dir.join(format!("DiskLayout_{}.xml", file_name));
                let xml_content = fs::read_to_string(&xml_path)
                    .unwrap_or_else(|_| panic!("Failed to read XML file: {:?}", xml_path));

                // Extract the <disk_layout_name> from XML
                let layout_name = if let Some(cap) = xml_name_re.captures(&xml_content) {
                    cap.get(1).unwrap().as_str().to_string()
                } else {
                    panic!("Failed to find <disk_layout_name> in {:?}", xml_path);
                };

                layouts.push(DiskLayoutInfo {
                    id,
                    name: layout_name,
                });
            }
        }
    }

    // Generate Rust code
    let mut code = String::new();
    code.push_str("// Auto-generated by build.rs - DO NOT EDIT\n\n");
    code.push_str("/// Predefined disk layout.\n");
    code.push_str("///\n");
    code.push_str(
        "/// Represents the different predefined disk layouts supported by the HxC library.\n",
    );
    code.push_str("/// This enum is automatically generated from LayoutsIndex.h.\n");
    code.push_str("#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]\n");
    code.push_str("pub enum DiskLayout {\n");

    for layout in &layouts {
        let variant_name = id_to_variant_name(&layout.name);
        code.push_str(&format!("    /// {}\n", layout.name));
        code.push_str(&format!("    {},\n", variant_name)); // No explicit value
    }

    code.push_str("}\n\n");

    // Generate methods
    code.push_str("impl DiskLayout {\n");

    // layout_name method
    code.push_str("    /// Get the disk layout name string\n");
    code.push_str("    pub fn layout_name(&self) -> &'static str {\n");
    code.push_str("        match self {\n");
    for layout in &layouts {
        let variant_name = id_to_variant_name(&layout.name);
        code.push_str(&format!(
            "            Self::{} => \"{}\",\n",
            variant_name, layout.name
        ));
    }
    code.push_str("        }\n");
    code.push_str("    }\n\n");

    // from_str method
    code.push_str("    /// Parse from a layout name string\n");
    code.push_str("    pub fn from_str(s: &str) -> Option<Self> {\n");
    code.push_str("        let upper = s.to_uppercase().replace('-', \"_\");\n");
    code.push_str("        match upper.as_str() {\n");

    for layout in &layouts {
        let variant_name = id_to_variant_name(&layout.name);
        let upper_name = layout.name.to_uppercase();
        code.push_str(&format!(
            "            \"{}\" => Some(Self::{}),\n",
            upper_name, variant_name
        ));
    }

    code.push_str("            _ => None,\n");
    code.push_str("        }\n");
    code.push_str("    }\n");

    // from_usize method
    code.push_str("\n    /// Convert from a usize value\n");
    code.push_str("    pub fn from_usize(value: usize) -> Option<Self> {\n");
    code.push_str("        match value {\n");

    for layout in &layouts {
        let variant_name = id_to_variant_name(&layout.name);
        code.push_str(&format!(
            "            {} => Some(Self::{}),\n",
            layout.id, variant_name
        ));
    }

    code.push_str("            _ => None,\n");
    code.push_str("        }\n");
    code.push_str("    }\n");

    // id method - queries C library for runtime layout ID
    code.push_str("\n    /// Get the disk layout ID from the C library.\n");
    code.push_str("    ///\n");
    code.push_str(
        "    /// The ID is retrieved at runtime by querying the C library with the layout name.\n",
    );
    code.push_str(
        "    /// Panics if the layout is not found (which indicates a bug in the bindings).\n",
    );
    code.push_str("    ///\n");
    code.push_str("    /// # Arguments\n");
    code.push_str("    /// * `hxcfe_xmlldr` - The HxC XML loader context\n");
    code.push_str("    ///\n");
    code.push_str("    /// # Returns\n");
    code.push_str("    /// The layout ID\n");
    code.push_str("    pub fn id(&self, hxcfe_xmlldr: *mut crate::HXCFE_XMLLDR) -> i32 {\n");
    code.push_str(
        "        assert!(!hxcfe_xmlldr.is_null(), \"HXCFE_XMLLDR context cannot be null\");\n",
    );
    code.push_str("        let name = self.layout_name();\n");
    code.push_str("        let c_name = std::ffi::CString::new(name)\n");
    code.push_str("            .expect(\"Layout name contains null byte\");\n");
    code.push_str("        let id = unsafe { crate::hxcfe_getXmlLayoutID(hxcfe_xmlldr, c_name.as_ptr() as *mut i8) };\n");
    code.push_str("        assert!(id >= 0, \"Layout '{}' not found in C library (got ID: {})\", name, id);\n");
    code.push_str("        id\n");
    code.push_str("    }\n");

    // all method
    code.push_str("\n    /// Get all available disk layouts\n");
    code.push_str("    pub fn all() -> &'static [DiskLayout] {\n");
    code.push_str("        &[\n");
    for layout in &layouts {
        let variant_name = id_to_variant_name(&layout.name);
        code.push_str(&format!("            Self::{},\n", variant_name));
    }
    code.push_str("        ]\n");
    code.push_str("    }\n");

    code.push_str("}\n\n");

    // Display trait
    code.push_str("impl std::fmt::Display for DiskLayout {\n");
    code.push_str("    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {\n");
    code.push_str("        write!(f, \"{}\", self.layout_name())\n");
    code.push_str("    }\n");
    code.push_str("}\n");

    // Write to file
    let output_file = out_path.join("disk_layout.rs");
    fs::write(&output_file, code).expect("Failed to write disk_layout.rs");
}