/
fileio.c
1413 lines (1147 loc) · 34.3 KB
/
fileio.c
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
/***************************************************************************
fileio.c
File access functions.
Copyright (c) 1996-2006, Nicola Salmoria and the MAME Team.
Visit http://mamedev.org for licensing and usage restrictions.
***************************************************************************/
#include <zlib.h>
#include "osdepend.h"
#include "driver.h"
#include "chd.h"
#include "hash.h"
#include "unzip.h"
#ifdef MESS
#include "image.h"
#endif
/***************************************************************************
DEBUGGING
***************************************************************************/
/* Verbose outputs to error.log ? */
#define VERBOSE 0
/* enable lots of logging */
#if VERBOSE
#define VPRINTF(x) logerror x
#else
#define VPRINTF(x)
#endif
/***************************************************************************
CONSTANTS
***************************************************************************/
#define PLAIN_FILE 0
#define RAM_FILE 1
#define ZIPPED_FILE 2
#define UNLOADED_ZIPPED_FILE 3
#define FILEFLAG_OPENREAD 0x0001
#define FILEFLAG_OPENWRITE 0x0002
#define FILEFLAG_HASH 0x0100
#define FILEFLAG_REVERSE_SEARCH 0x0200
#define FILEFLAG_VERIFY_ONLY 0x0400
#define FILEFLAG_NOZIP 0x0800
#define FILEFLAG_MUST_EXIST 0x1000
#define FILEFLAG_CREATE_GAMEDIR 0x8000
#ifdef MESS
#define FILEFLAG_GHOST 0x0004
#define FILEFLAG_ALLOW_ABSOLUTE 0x2000
#define FILEFLAG_ZIP_PATHS 0x4000
#endif
#ifdef MAME_DEBUG
#define DEBUG_COOKIE 0xbaadf00d
#endif
/***************************************************************************
TYPE DEFINITIONS
***************************************************************************/
/* typedef struct _mame_file mame_file -- declared in fileio.h */
struct _mame_file
{
#ifdef DEBUG_COOKIE
UINT32 debug_cookie;
#endif
osd_file * file;
UINT8 * data;
UINT64 offset;
UINT64 length;
UINT8 eof;
UINT8 type;
char hash[HASH_BUF_SIZE];
int back_char; /* Buffered char for unget. EOF for empty. */
};
/***************************************************************************
GLOBALS
***************************************************************************/
#ifdef MESS
int mess_ghost_images;
#endif
/***************************************************************************
PROTOTYPES
***************************************************************************/
static mame_file *generic_fopen(int pathtype, const char *gamename, const char *filename, const char *hash, UINT32 flags, osd_file_error *error);
static const char *get_extension_for_filetype(int filetype);
static int checksum_file(int pathtype, int pathindex, const char *file, UINT8 **p, UINT64 *size, char* hash);
static chd_interface_file *chd_open_cb(const char *filename, const char *mode);
static void chd_close_cb(chd_interface_file *file);
static UINT32 chd_read_cb(chd_interface_file *file, UINT64 offset, UINT32 count, void *buffer);
static UINT32 chd_write_cb(chd_interface_file *file, UINT64 offset, UINT32 count, const void *buffer);
static UINT64 chd_length_cb(chd_interface_file *file);
/***************************************************************************
HARD DISK INTERFACE
***************************************************************************/
static chd_interface mame_chd_interface =
{
chd_open_cb,
chd_close_cb,
chd_read_cb,
chd_write_cb,
chd_length_cb
};
/*-------------------------------------------------
fileio_init - initialize the internal file
I/O system; note that the OS layer is free to
call mame_fopen before fileio_init
-------------------------------------------------*/
void fileio_init(void)
{
chd_set_interface(&mame_chd_interface);
add_exit_callback(fileio_exit);
}
/*-------------------------------------------------
fileio_exit - clean up behind ourselves
-------------------------------------------------*/
void fileio_exit(void)
{
unzip_cache_clear();
}
/*-------------------------------------------------
mame_fopen_error - open a file for access and
return an error code
-------------------------------------------------*/
mame_file *mame_fopen_error(const char *gamename, const char *filename, int filetype, int openforwrite, osd_file_error *error)
{
/* first verify that we can handle this type of file */
switch (filetype)
{
/* read-only cases */
#ifndef MESS
case FILETYPE_IMAGE:
case FILETYPE_INI:
#endif
case FILETYPE_ROM:
case FILETYPE_SAMPLE:
case FILETYPE_HIGHSCORE_DB:
case FILETYPE_ARTWORK:
case FILETYPE_HISTORY:
case FILETYPE_LANGUAGE:
case FILETYPE_CTRLR:
if (openforwrite)
{
logerror("mame_fopen: type %02x write not supported\n", filetype);
return NULL;
}
break;
/* write-only cases */
case FILETYPE_SCREENSHOT:
case FILETYPE_MOVIE:
case FILETYPE_DEBUGLOG:
if (!openforwrite)
{
logerror("mame_fopen: type %02x read not supported\n", filetype);
return NULL;
}
break;
}
/* now open the file appropriately */
switch (filetype)
{
/* generic files that live in a single directory */
case FILETYPE_DEBUGLOG:
case FILETYPE_CTRLR:
case FILETYPE_LANGUAGE:
case FILETYPE_HIGHSCORE_DB:
return generic_fopen(filetype, NULL, filename, 0, openforwrite ? FILEFLAG_OPENWRITE : FILEFLAG_OPENREAD, error);
/* game-specific files that live in a single directory */
case FILETYPE_HIGHSCORE:
case FILETYPE_CONFIG:
case FILETYPE_INPUTLOG:
case FILETYPE_COMMENT:
case FILETYPE_INI:
case FILETYPE_HASH: /* MESS-specific */
return generic_fopen(filetype, NULL, gamename, 0, openforwrite ? FILEFLAG_OPENWRITE : FILEFLAG_OPENREAD, error);
/* generic multi-directory files */
case FILETYPE_SAMPLE:
case FILETYPE_ARTWORK:
return generic_fopen(filetype, gamename, filename, 0, openforwrite ? FILEFLAG_OPENWRITE : FILEFLAG_OPENREAD, error);
/* ROM files */
case FILETYPE_ROM:
return generic_fopen(filetype, gamename, filename, 0, FILEFLAG_OPENREAD | FILEFLAG_HASH, error);
/* memory card files */
case FILETYPE_MEMCARD:
return generic_fopen(filetype, gamename, filename, 0, openforwrite ? FILEFLAG_OPENWRITE | FILEFLAG_CREATE_GAMEDIR : FILEFLAG_OPENREAD, error);
/* cheat file */
case FILETYPE_CHEAT:
return generic_fopen(filetype, NULL, filename, 0, FILEFLAG_OPENREAD | (openforwrite ? FILEFLAG_OPENWRITE : 0), error);
/* disk images */
case FILETYPE_IMAGE:
#ifndef MESS
return generic_fopen(filetype, gamename, filename, 0, FILEFLAG_OPENREAD | FILEFLAG_NOZIP, error);
#else
{
int flags = FILEFLAG_ALLOW_ABSOLUTE;
switch (openforwrite)
{
case OSD_FOPEN_READ:
flags |= FILEFLAG_OPENREAD | FILEFLAG_ZIP_PATHS;
break;
case OSD_FOPEN_WRITE:
flags |= FILEFLAG_OPENWRITE;
break;
case OSD_FOPEN_RW:
flags |= FILEFLAG_OPENREAD | FILEFLAG_OPENWRITE | FILEFLAG_MUST_EXIST;
break;
case OSD_FOPEN_RW_CREATE:
flags |= FILEFLAG_OPENREAD | FILEFLAG_OPENWRITE;
break;
}
if (mess_ghost_images)
flags |= FILEFLAG_GHOST;
return generic_fopen(filetype, gamename, filename, 0, flags, error);
}
#endif
/* differencing disk images */
case FILETYPE_IMAGE_DIFF:
return generic_fopen(filetype, gamename, filename, 0, FILEFLAG_OPENREAD | FILEFLAG_OPENWRITE, error);
/* NVRAM files */
case FILETYPE_NVRAM:
#ifdef MESS
if (filename)
return generic_fopen(filetype, gamename, filename, 0, openforwrite ? FILEFLAG_OPENWRITE | FILEFLAG_CREATE_GAMEDIR : FILEFLAG_OPENREAD, error);
#endif
return generic_fopen(filetype, NULL, gamename, 0, openforwrite ? FILEFLAG_OPENWRITE : FILEFLAG_OPENREAD, error);
/* save state files */
case FILETYPE_STATE:
#ifndef MESS
return generic_fopen(filetype, NULL, filename, 0, openforwrite ? FILEFLAG_OPENWRITE : FILEFLAG_OPENREAD, error);
#else
return generic_fopen(filetype, NULL, filename, 0, FILEFLAG_ALLOW_ABSOLUTE | (openforwrite ? FILEFLAG_OPENWRITE : FILEFLAG_OPENREAD), error);
#endif
/* screenshot files */
case FILETYPE_SCREENSHOT:
case FILETYPE_MOVIE:
#ifndef MESS
return generic_fopen(filetype, NULL, filename, 0, FILEFLAG_OPENWRITE, error);
#else
return generic_fopen(filetype, NULL, filename, 0, FILEFLAG_ALLOW_ABSOLUTE | FILEFLAG_OPENWRITE, error);
#endif
/* history files */
case FILETYPE_HISTORY:
#ifndef MESS
return generic_fopen(filetype, NULL, filename, 0, FILEFLAG_OPENREAD, error);
#else
return generic_fopen(filetype, NULL, filename, 0, FILEFLAG_ALLOW_ABSOLUTE | FILEFLAG_OPENREAD, error);
#endif
/* anything else */
default:
logerror("mame_fopen(): unknown filetype %02x\n", filetype);
return NULL;
}
return NULL;
}
/*-------------------------------------------------
mame_fopen - open a file without returning
any error codes
-------------------------------------------------*/
mame_file *mame_fopen(const char *gamename, const char *filename, int filetype, int openforwrite)
{
return mame_fopen_error(gamename, filename, filetype, openforwrite, NULL);
}
/*-------------------------------------------------
mame_fopen_rom - similar to mame_fopen, but
lets you specify an expected checksum
-------------------------------------------------*/
mame_file *mame_fopen_rom(const char *gamename, const char *filename, const char *exphash)
{
return generic_fopen(FILETYPE_ROM, gamename, filename, exphash, FILEFLAG_OPENREAD | FILEFLAG_HASH, NULL);
}
/*-------------------------------------------------
mame_fclose - closes a file
-------------------------------------------------*/
void mame_fclose(mame_file *file)
{
#ifdef DEBUG_COOKIE
assert(file->debug_cookie == DEBUG_COOKIE);
file->debug_cookie = 0;
#endif
/* switch off the file type */
switch (file->type)
{
case PLAIN_FILE:
osd_fclose(file->file);
break;
case ZIPPED_FILE:
case RAM_FILE:
if (file->data)
free(file->data);
break;
}
/* free the file data */
free(file);
}
/*-------------------------------------------------
mame_faccess - determine whether or not the
given file exists in our search paths
-------------------------------------------------*/
int mame_faccess(const char *filename, int filetype)
{
const char *extension = get_extension_for_filetype(filetype);
int pathcount = osd_get_path_count(filetype);
char modified_filename[256];
int pathindex;
/* copy the filename and add an extension */
strncpy(modified_filename, filename, sizeof(modified_filename) - 1);
modified_filename[sizeof(modified_filename) - 1] = 0;
if (extension)
{
char *p = strchr(modified_filename, '.');
if (p)
{
strncpy(p, extension, sizeof(modified_filename) - (p - modified_filename) - 1);
modified_filename[sizeof(modified_filename) - 1] = 0;
}
else
{
strncat(modified_filename, ".", sizeof(modified_filename) - strlen(modified_filename) - 1);
strncat(modified_filename, extension, sizeof(modified_filename) - strlen(modified_filename) - 1);
}
}
/* loop over all paths */
for (pathindex = 0; pathindex < pathcount; pathindex++)
{
char name[256];
/* first check the raw filename, in case we're looking for a directory */
snprintf(name, sizeof(name), "%s", filename);
VPRINTF(("mame_faccess: trying %s\n", name));
if (osd_get_path_info(filetype, pathindex, name) != PATH_NOT_FOUND)
return 1;
/* try again with a .zip extension */
snprintf(name, sizeof(name), "%s.zip", filename);
VPRINTF(("mame_faccess: trying %s\n", name));
if (osd_get_path_info(filetype, pathindex, name) != PATH_NOT_FOUND)
return 1;
/* does such a directory (or file) exist? */
snprintf(name, sizeof(name), "%s", modified_filename);
VPRINTF(("mame_faccess: trying %s\n", name));
if (osd_get_path_info(filetype, pathindex, name) != PATH_NOT_FOUND)
return 1;
}
/* no match */
return 0;
}
/*-------------------------------------------------
mame_fread - read from a file
-------------------------------------------------*/
UINT32 mame_fread(mame_file *file, void *buffer, UINT32 length)
{
/* flush any buffered char */
file->back_char = EOF;
/* switch off the file type */
switch (file->type)
{
case PLAIN_FILE:
return osd_fread(file->file, buffer, length);
case ZIPPED_FILE:
case RAM_FILE:
if (file->data)
{
if (file->offset + length > file->length)
{
length = file->length - file->offset;
file->eof = 1;
}
memcpy(buffer, file->data + file->offset, length);
file->offset += length;
return length;
}
break;
}
return 0;
}
/*-------------------------------------------------
mame_fwrite - write to a file
-------------------------------------------------*/
UINT32 mame_fwrite(mame_file *file, const void *buffer, UINT32 length)
{
/* flush any buffered char */
file->back_char = EOF;
/* switch off the file type */
switch (file->type)
{
case PLAIN_FILE:
return osd_fwrite(file->file, buffer, length);
}
return 0;
}
/*-------------------------------------------------
mame_fseek - seek within a file
-------------------------------------------------*/
int mame_fseek(mame_file *file, INT64 offset, int whence)
{
int err = 0;
/* flush any buffered char */
file->back_char = EOF;
/* switch off the file type */
switch (file->type)
{
case PLAIN_FILE:
return osd_fseek(file->file, offset, whence);
case ZIPPED_FILE:
case RAM_FILE:
switch (whence)
{
case SEEK_SET:
file->offset = offset;
break;
case SEEK_CUR:
file->offset += offset;
break;
case SEEK_END:
file->offset = file->length + offset;
break;
}
file->eof = 0;
break;
}
return err;
}
/*-------------------------------------------------
mame_fchecksum - verify the existence and
length of a file given a hash checksum
-------------------------------------------------*/
int mame_fchecksum(const char *gamename, const char *filename, unsigned int *length, char *hash)
{
mame_file *file;
/* first open the file; we pass the source hash because it contains
the expected checksum for the file (used to load by checksum) */
file = generic_fopen(FILETYPE_ROM, gamename, filename, hash, FILEFLAG_OPENREAD | FILEFLAG_HASH | FILEFLAG_VERIFY_ONLY, NULL);
/* if we didn't succeed return -1 */
if (!file)
return -1;
/* close the file and save the length & checksum */
hash_data_copy(hash, file->hash);
*length = file->length;
mame_fclose(file);
return 0;
}
/*-------------------------------------------------
mame_fsize - returns the size of a file
-------------------------------------------------*/
UINT64 mame_fsize(mame_file *file)
{
/* switch off the file type */
switch (file->type)
{
case PLAIN_FILE:
{
int size, offs;
offs = osd_ftell(file->file);
osd_fseek(file->file, 0, SEEK_END);
size = osd_ftell(file->file);
osd_fseek(file->file, offs, SEEK_SET);
return size;
}
case RAM_FILE:
case ZIPPED_FILE:
return file->length;
}
return 0;
}
/*-------------------------------------------------
mame_fhash - returns the hash for a file
-------------------------------------------------*/
const char *mame_fhash(mame_file *file)
{
return file->hash;
}
/*-------------------------------------------------
mame_fgetc - read a character from a file
-------------------------------------------------*/
int mame_fgetc(mame_file *file)
{
unsigned char buffer;
/* handle ungetc */
if (file->back_char != EOF)
{
buffer = file->back_char;
file->back_char = EOF;
return buffer;
}
/* switch off the file type */
switch (file->type)
{
case PLAIN_FILE:
if (osd_fread(file->file, &buffer, 1) == 1)
return buffer;
return EOF;
case RAM_FILE:
case ZIPPED_FILE:
if (file->offset < file->length)
return file->data[file->offset++];
else
file->eof = 1;
return EOF;
}
return EOF;
}
/*-------------------------------------------------
mame_ungetc - put back a character read from
a file
-------------------------------------------------*/
int mame_ungetc(int c, mame_file *file)
{
file->back_char = c;
return c;
}
/*-------------------------------------------------
mame_fgets - read a line from a text file
-------------------------------------------------*/
char *mame_fgets(char *s, int n, mame_file *file)
{
char *cur = s;
/* loop while we have characters */
while (n > 0)
{
int c = mame_fgetc(file);
if (c == EOF)
break;
/* if there's a CR, look for an LF afterwards */
if (c == 0x0d)
{
int c2 = mame_fgetc(file);
if (c2 != 0x0a)
mame_ungetc(c2, file);
*cur++ = 0x0d;
n--;
break;
}
/* if there's an LF, reinterp as a CR for consistency */
else if (c == 0x0a)
{
*cur++ = 0x0d;
n--;
break;
}
/* otherwise, pop the character in and continue */
*cur++ = c;
n--;
}
/* if we put nothing in, return NULL */
if (cur == s)
return NULL;
/* otherwise, terminate */
if (n > 0)
*cur++ = 0;
return s;
}
/*-------------------------------------------------
mame_feof - return 1 if we're at the end
of file
-------------------------------------------------*/
int mame_feof(mame_file *file)
{
/* check for buffered chars */
if (file->back_char != EOF)
return 0;
/* switch off the file type */
switch (file->type)
{
case PLAIN_FILE:
return osd_feof(file->file);
case RAM_FILE:
case ZIPPED_FILE:
return (file->eof);
}
return 1;
}
/*-------------------------------------------------
mame_ftell - return the current file position
-------------------------------------------------*/
UINT64 mame_ftell(mame_file *file)
{
/* switch off the file type */
switch (file->type)
{
case PLAIN_FILE:
return osd_ftell(file->file);
case RAM_FILE:
case ZIPPED_FILE:
return file->offset;
}
return -1L;
}
/*-------------------------------------------------
mame_fread_swap - read from a data file,
swapping every other byte
-------------------------------------------------*/
UINT32 mame_fread_swap(mame_file *file, void *buffer, UINT32 length)
{
UINT8 *buf;
UINT8 temp;
int res, i;
/* standard read first */
res = mame_fread(file, buffer, length);
/* swap the result */
buf = buffer;
for (i = 0; i < res; i += 2)
{
temp = buf[i];
buf[i] = buf[i + 1];
buf[i + 1] = temp;
}
return res;
}
/*-------------------------------------------------
mame_fwrite_swap - write to a data file,
swapping every other byte
-------------------------------------------------*/
UINT32 mame_fwrite_swap(mame_file *file, const void *buffer, UINT32 length)
{
UINT8 *buf;
UINT8 temp;
int res, i;
/* swap the data first */
buf = (UINT8 *)buffer;
for (i = 0; i < length; i += 2)
{
temp = buf[i];
buf[i] = buf[i + 1];
buf[i + 1] = temp;
}
/* do the write */
res = mame_fwrite(file, buffer, length);
/* swap the data back */
for (i = 0; i < length; i += 2)
{
temp = buf[i];
buf[i] = buf[i + 1];
buf[i + 1] = temp;
}
return res;
}
/*-------------------------------------------------
mame_fputs - write a line to a text file
-------------------------------------------------*/
#if !defined(CRLF) || (CRLF < 1) || (CRLF > 3)
#error CRLF undefined: must be 1 (CR), 2 (LF) or 3 (CR/LF)
#endif
int mame_fputs(mame_file *f, const char *s)
{
char convbuf[1024];
char *pconvbuf;
for (pconvbuf = convbuf; *s; s++)
{
if (*s == '\n')
{
if (CRLF == 1) /* CR only */
*pconvbuf++ = 13;
else if (CRLF == 2) /* LF only */
*pconvbuf++ = 10;
else if (CRLF == 3) /* CR+LF */
{
*pconvbuf++ = 13;
*pconvbuf++ = 10;
}
}
else
*pconvbuf++ = *s;
}
*pconvbuf++ = 0;
return mame_fwrite(f, convbuf, strlen(convbuf));
}
/*-------------------------------------------------
mame_vfprintf - vfprintf to a text file
-------------------------------------------------*/
static int mame_vfprintf(mame_file *f, const char *fmt, va_list va)
{
char buf[1024];
vsnprintf(buf, sizeof(buf), fmt, va);
return mame_fputs(f, buf);
}
/*-------------------------------------------------
mame_fprintf - vfprintf to a text file
-------------------------------------------------*/
int CLIB_DECL mame_fprintf(mame_file *f, const char *fmt, ...)
{
int rc;
va_list va;
va_start(va, fmt);
rc = mame_vfprintf(f, fmt, va);
va_end(va);
return rc;
}
/*-------------------------------------------------
compose_path - form a pathname from standard
elements
-------------------------------------------------*/
INLINE void compose_path(char *output, size_t outputlen, const char *gamename, const char *filename, const char *extension)
{
char *filename_base = output;
*output = 0;
#ifdef MESS
if (filename && osd_is_absolute_path(filename))
{
strncpy(output, filename, outputlen - 1);
output[outputlen - 1] = 0;
return;
}
#endif
/* if there's a gamename, add that; only add a '/' if there is a filename as well */
if (gamename)
{
strncat(output, gamename, outputlen - strlen(output) - 1);
if (filename)
{
strncat(output, "/", outputlen - strlen(output) - 1);
filename_base = &output[strlen(output)];
}
}
/* if there's a filename, add that */
if (filename)
strncat(output, filename, outputlen - strlen(output) - 1);
/* if there's no extension in the filename, add the extension */
if (extension && !strchr(filename_base, '.'))
{
strncat(output, ".", outputlen - strlen(output) - 1);
strncat(output, extension, outputlen - strlen(output) - 1);
}
}
/*-------------------------------------------------
get_extension_for_filetype - return extension
for a given file type
-------------------------------------------------*/
static const char *get_extension_for_filetype(int filetype)
{
const char *extension;
/* now open the file appropriately */
switch (filetype)
{
case FILETYPE_RAW: /* raw data files */
case FILETYPE_ROM: /* ROM files */
case FILETYPE_HIGHSCORE_DB: /* highscore database/history files */
case FILETYPE_HISTORY: /* game history files */
case FILETYPE_CHEAT: /* cheat file */
default: /* anything else */
extension = NULL;
break;
#ifndef MESS
case FILETYPE_IMAGE: /* disk image files */
extension = "chd";
break;
#endif
case FILETYPE_IMAGE_DIFF: /* differencing drive images */
extension = "dif";
break;
case FILETYPE_SAMPLE: /* samples */
extension = "wav";
break;
case FILETYPE_ARTWORK: /* artwork files */
case FILETYPE_SCREENSHOT: /* screenshot files */
extension = "png";
break;
case FILETYPE_MOVIE: /* recorded movie files */
extension = "mng";
break;
case FILETYPE_NVRAM: /* NVRAM files */
extension = "nv";
break;
case FILETYPE_HIGHSCORE: /* high score files */
extension = "hi";
break;
case FILETYPE_LANGUAGE: /* language files */
extension = "lng";
break;
case FILETYPE_CTRLR: /* controller files */
case FILETYPE_CONFIG: /* config files */
extension = "cfg";
break;
case FILETYPE_INPUTLOG: /* input logs */
extension = "inp";
break;
case FILETYPE_STATE: /* save state files */
extension = "sta";
break;
case FILETYPE_MEMCARD: /* memory card files */
extension = "mem";
break;
case FILETYPE_INI: /* game specific ini files */
extension = "ini";
break;
case FILETYPE_COMMENT:
extension = "cmt";
break;
#ifdef MESS
case FILETYPE_HASH:
extension = "hsi";
break;
#endif
}
return extension;
}
/*-------------------------------------------------
generic_fopen - master logic for finding and
opening a file
-------------------------------------------------*/
static mame_file *generic_fopen(int pathtype, const char *gamename, const char *filename, const char* hash, UINT32 flags, osd_file_error *error)
{
static const char *access_modes[] = { "rb", "rb", "wb", "r+b", "rb", "rb", "wbg", "r+bg" };
const char *extension = get_extension_for_filetype(pathtype);
int pathcount = osd_get_path_count(pathtype);
int pathindex, pathstart, pathstop, pathinc;
mame_file file, *newfile;
char tempname[256];
osd_file_error dummy;
#ifdef MESS
int is_absolute_path = FALSE;
if (filename)
{
is_absolute_path = osd_is_absolute_path(filename);
if (is_absolute_path)