home *** CD-ROM | disk | FTP | other *** search
/ ftp.whtech.com / ftp.whtech.com.7z / ftp.whtech.com / emulators / v9t9 / linux / sources / V9t9 / source / fiad.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-10-19  |  10.8 KB  |  395 lines

  1. /*
  2.  *    File-in-a-directory utility routines
  3.  *
  4.  */
  5.  
  6. #ifndef __FIAD_H__
  7. #define __FIAD_H__
  8.  
  9. #include "centry.h"
  10.  
  11. extern int newfileformat, unknownfileistext, keepfileformat;
  12. extern int unknownfileistext;
  13. extern int repairbadfiles;
  14. extern int fixupoldv9t9filenames, generateoldv9t9filenames;
  15.  
  16. /*    In V9t9 6.0, we used 8.3 filenames, and these chars
  17.     were converted by adding 0x80 to the name on disk. 
  18.     In this version, we still have illegal chars, but
  19.     they are replaced with the escape sequence '&#xx;' as
  20.     in HTML. */
  21. #define DOS_illegalchars "<>=,;:*?[]/\\"
  22.  
  23. #define FIAD_esc '&'
  24. #define FIAD_illegalchars "<>,:*?/\\"
  25.  
  26. typedef void (*fiad_logger_func)(u32, const char *, ...);
  27.  
  28. /*
  29.  *    Install a new function to log warnings and errors
  30.  *    from the fiad_xxx routines.  NULL means no logging.
  31.  *    Returns the old logger function.
  32.  */
  33. fiad_logger_func
  34. fiad_set_logger(fiad_logger_func nw);
  35.  
  36.  
  37. /*    Convert a TI filename to a DOS 8.3 filename. */
  38. void
  39. fiad_filename_ti2dos(const char *tiname, int len, char *dosname);
  40.  
  41. /* Convert a TI filename to the host OS. */
  42. void
  43. fiad_filename_ti2host(const char *tiname, int len, char *hostname);
  44.  
  45. /*    Create a full path given a TI filename and an OS path. */
  46. OSError
  47. fiad_filename_to_spec(const OSPathSpec * path, const char *name, int len, 
  48.                       OSSpec * spec);
  49.  
  50. /*    Convert a filename to TI format,
  51.     return length of filename */
  52. int
  53. fiad_filename_host2ti(const char *hostname, char *tiname);
  54.  
  55. /*    Return length of filename in FDR */
  56. int
  57. fiad_filename_strlen(const char *name);
  58.  
  59. /*    Convert a directory leaf to a TI name at 'name' and return length. */
  60. int
  61. fiad_path_disk2ti(const OSPathSpec *path, char *name);
  62.  
  63. /*    Rename an old DOS-mangled V9t9 filename to the new format 
  64.     
  65.     name, len:  TI-format name 
  66.     spec:        spec for existing file in an old format
  67.  
  68.     If successful, OS_NOERR is returned and 'spec' is transformed
  69.     into the current filename.
  70. */
  71. OSError
  72. fiad_filename_fixup_old_filename(const char *name, int len,
  73.                                  OSSpec *spec);
  74.  
  75. /*    Format of FDR on disk  */
  76. enum
  77. {
  78.     F_V9t9 = 0,            // V9t9 format
  79.     F_TIFILES = 1,        // TIFILES format
  80.     F_TEXT = 2,            // treating as text
  81.     F_UNKNOWN = -1        // unknown
  82. };
  83.  
  84. extern int unknownfileistext;
  85. extern OSFileType    osV99Type, osTIFILESType;
  86.  
  87. #define    F_READFDR    1
  88. #define F_MAKEFDR    2
  89. #define    FDRSIZE        128
  90.  
  91. /*    FDR masks for file type */
  92. enum {
  93.     ff_variable = 0x80,
  94.     ff_backup = 0x10,        // set by MYARC HD
  95.     ff_protected = 0x8,
  96.     ff_internal = 0x2,
  97.     ff_program = 0x1
  98. };
  99.  
  100. #define FF_VALID_FLAGS    (ff_variable|ff_backup|ff_protected|ff_internal|ff_program)
  101.  
  102. typedef struct v9t9_fdr {
  103.     char        filenam[10];/* filename, padded with spaces */
  104.     u8          res10[2];    /* reserved */
  105.     u8          flags;        /* filetype flags */
  106.     u8          recspersec;    /* # records per sector, 
  107.                                256/reclen for FIXED,
  108.                                255/(reclen+1) for VAR,
  109.                                0 for program */
  110.     u16         secsused;    /* [big-endian]:  # sectors in file */
  111.     u8          byteoffs;    /* last byte used in file 
  112.                                    (0 = no last empty sector) */
  113.     u8          reclen;        /* record length, 0 for program */
  114.     u16         numrecs;    /* [little-endian]:  # records for FIXED file,
  115.                                # sectors for VARIABLE file,
  116.                                0 for program */
  117.     u8          rec20[8];    /* reserved */
  118.     u8          dcpb[100];    /* sector layout of file, ignored for v9t9 */
  119. } v9t9_fdr;
  120.  
  121. typedef struct tifiles_fdr {
  122.     u8          sig[8];        /* '\007TIFILES' */
  123.     u16         secsused;    /* [big-endian]:  # sectors in file */
  124.     u8          flags;        /* filetype flags */
  125.     u8          recspersec;    /* # records per sector, 
  126.                                256/reclen for FIXED,
  127.                                255/(reclen+1) for VAR,
  128.                                0 for program */
  129.     u8          byteoffs;      /* last byte used in file 
  130.                                (0 = no last empty sector) */
  131.     u8          reclen;        /* record length, 0 for program */
  132.     u16         numrecs;    /* [little-endian]:  # records for FIXED file,
  133.                                # sectors for VARIABLE file,
  134.                                0 for program */
  135.     u8          unused[112];    /* zero */
  136. } tifiles_fdr;
  137.  
  138. typedef struct fdrrec {
  139.     char        filenam[10];/* the filename the 9900 sees */
  140.     u8          flags;        /* filetype flags */
  141.     u8          recspersec;    /* # records per sector, 
  142.                                256/reclen for FIXED,
  143.                                255/(reclen+1) for VAR,
  144.                                0 for program */
  145.     u16         secsused;    /* [HOST ORDER]:  # sectors in file */
  146.     u8          byteoffs;    /* last byte used in file 
  147.                                    (0 = no last empty sector) */
  148.     u8          reclen;        /* record length, 0 for program */
  149.     u16         numrecs;    /* [little-endian]:  # records for FIXED file,
  150.                                # sectors for VARIABLE file,
  151.                                0 for program */
  152. } fdrrec;
  153.  
  154. #define FDR_LASTSEC(fdr)    ( (fdr)->secsused - 1 + \
  155.                                 ((fdr)->byteoffs ? 0 : 1))
  156.  
  157. // file size for var/program files
  158. #define FDR_PGMVARFILESIZE(fdr)    ( (((fdr)->secsused - 1) << 8) + \
  159.                                 ((fdr)->byteoffs ? (fdr)->byteoffs : 256))
  160. // file size for fixed files
  161. #define FDR_FIXFILESIZE(fdr)    ( ((fdr)->numrecs + (fdr)->recspersec - 1) / \
  162.                                 ((fdr)->recspersec ? (fdr)->recspersec : 1) * 256)
  163. #define FDR_FILESIZE(fdr)    (((fdr)->flags & (ff_variable|ff_program)) ? \
  164.                         FDR_PGMVARFILESIZE(fdr) : FDR_FIXFILESIZE(fdr))
  165.  
  166. /*    Verify a V9t9 FDR as a real v9t9 FDR.
  167.     Perform sanity checks to assert that a v9t9 file
  168.     is really a v9t9 file and not a text file. */
  169. bool
  170. fiad_fdr_matches_v9t9_fdr(struct v9t9_fdr *v9f, const char *filename, OSSize filesize);
  171.  
  172. /*    Setup the various flags in an FDR
  173.     according to the minimum filetype info */
  174. void
  175. fiad_fdr_setup(fdrrec *fdr, bool program, u8 flags, u8 reclen, u32 size);
  176.  
  177. /*    Repair various fields of the FDR according to
  178.     known bugs or common file-closing problems.
  179.     'filesize' should be the physical size of the file.
  180.  
  181.     Returns true if fdr was changed.
  182. */
  183. bool
  184. fiad_fdr_repair(fdrrec *fdr, OSSize filesize);
  185.  
  186. /*    Layout for our idea of an open file */
  187.  
  188. typedef struct fiad_tifile {
  189.     OSSpec      spec;        /* OS spec for file */
  190.     OSRef       handle;        /* OS handle for file */
  191.     bool        open;        /* file is open */
  192.     bool        readonly;    /* if we didn't or couldn't open file read/write */
  193.     bool        changed;    /* has file been dirtied? */
  194.     bool        changedfdr;    /* was FDR format or data changed when opened? */
  195.  
  196.     OSError        error;        /* last OSError we got */
  197.  
  198.     u16         cursec;        /* current sector offset in file */
  199.     u8          curoffs;    /* current offset into sector */
  200.     u8          curnrecs;    /* record offset into sector */
  201.     u16         currec;        /* current record # in file */
  202.  
  203.     int         format;        /* FDR format on disk (F_V9t9, F_TIFILES, F_TEXT) */
  204.     fdrrec      fdr;        /* essential info in FDR */
  205.  
  206.     u8          sector[256]; /* sector buffer */
  207. } fiad_tifile;
  208.  
  209. /*    Initialize a tifile */
  210. void
  211. fiad_tifile_clear(fiad_tifile *tf);
  212.  
  213. /*    Setup the FDR with the file path. */
  214. OSError
  215. fiad_tifile_setup_spec_with_file(fiad_tifile *tf, 
  216.                                  const OSPathSpec *path, 
  217.                                  const char *fname, int len);
  218.  
  219. /*    Setup the FDR with the OSSpec. */
  220. OSError
  221. fiad_tifile_setup_spec_with_spec(fiad_tifile *tf, 
  222.                                  OSSpec *spec);
  223.  
  224. /*    Read the FDR from a file,
  225.     return 0 if it's bad. */
  226. int
  227. fiad_tifile_read_fdr(fiad_tifile * tf);
  228.  
  229. /*    Write FDR to file, 
  230.     return 0 and report error if failed. */
  231. int
  232. fiad_tifile_write_fdr(fiad_tifile * tf);
  233.  
  234. /*    Verify a file, by classifying its type and checking invariants */
  235. int
  236. fiad_tifile_verify(fiad_tifile * tf, bool check_size);
  237.  
  238. OSFileType *
  239. fiad_get_file_type(int newfileformat);
  240.  
  241. /*    Initialize file pointers which keep track of current record */
  242. int
  243. fiad_tifile_init_file_pointers(fiad_tifile * tf);
  244.  
  245. /*    Read a sector from the file at tf->cursec. 
  246.     If tf points to the last empty sector of file, 
  247.     this is not an error.
  248.     Return 0 if sector not found.  */
  249. int
  250. fiad_tifile_read_sector(fiad_tifile * tf);
  251.  
  252. /*    Write a sector to the file at tf->cursec. 
  253.     Return 0 if sector not written.  */
  254. int
  255. fiad_tifile_write_sector(fiad_tifile * tf);
  256.  
  257. /*  Set file size according to cursec and curoffs */
  258. int
  259. fiad_tifile_set_file_size(fiad_tifile *tf);
  260.  
  261. /*
  262.   Open or create the file, either r/w or r/o.
  263.   We expect the spec to have been set up.
  264.  
  265.   create && always means, delete existing file.
  266.   create means, create file if not existing.
  267.  
  268.   create, if 2, means the same as create==true, 
  269.   with the added hint that the file size might
  270.   not match, so don't check this.
  271.  */
  272. OSError
  273. fiad_tifile_open_file(fiad_tifile *tf, int newfileformat,
  274.                       bool create, bool always, bool readonly);
  275.  
  276. /*
  277.  *    Reopen a file, keeping open file information intact.
  278.  */
  279. OSError
  280. fiad_tifile_reopen_file(fiad_tifile *tf, int newfileformat,
  281.                         bool readonly);
  282.  
  283. /*
  284.  *    Close a tifile.
  285.  */
  286. void
  287. fiad_tifile_close_file(fiad_tifile * tf);
  288.  
  289. /*
  290.   Rewrite FDR of closed file 
  291.  
  292.   This may be used to change the format (change tf->format)
  293.   or rewrite a fixed FDR on a readonly file.
  294. */
  295. void
  296. fiad_tifile_rewrite_fdr(fiad_tifile *tf);
  297.  
  298. /*    Flush dirty buffers */
  299. int
  300. fiad_tifile_flush(fiad_tifile *tf);
  301.  
  302. /*    Change the current sector */
  303. int
  304. fiad_tifile_seek_to_sector(fiad_tifile *tf, int secnum);
  305.  
  306. /*    Seek, logically, to the end of file */
  307. int
  308. fiad_tifile_seek_to_end(fiad_tifile * tf);
  309.  
  310. /*    Read a record from a file 
  311.  
  312.     Return 0 for success, 1 for EOF, and -1 for hardware failure
  313. */
  314. int fiad_tifile_read_record(fiad_tifile *tf, u8 *data, u8 *reclen);
  315.  
  316. /*    Write a record to the file.
  317.  
  318.     Return 0 for success, -1 for hardware failure, 1 for disk full 
  319. */
  320. int
  321. fiad_tifile_write_record(fiad_tifile *tf, u8 *data, u8 reclen);
  322.  
  323. /*    Seek to a given record.  For variable files, only 0 is accepted. 
  324.  
  325.     Return 0 for success, -1 for hardware failure, 1 for disk full
  326. */
  327. int
  328. fiad_tifile_seek_to_record(fiad_tifile *tf, u16 recnum);
  329.  
  330. /*    Read binary data from file at current position.
  331.  
  332.     Returns 0 for success, -1 for hardware failure, 1 for EOF.
  333. */
  334. int
  335. fiad_tifile_read_binary_image(fiad_tifile *tf, u8 *data, u16 maxread, u16 *gotread);
  336.  
  337. /*    Write binary data to a file at the current position 
  338.  
  339.     Return 0 for success, -1 for hardware failure, 1 for disk full
  340. */
  341. int
  342. fiad_tifile_write_binary_image(fiad_tifile *tf, u8 *data, u16 towrite, u16 *written);
  343.  
  344. /*    Get info (read FDR) about a closed file in tf,
  345.     return 1 for success
  346. */
  347. int
  348. fiad_tifile_get_info(fiad_tifile *tf);
  349.  
  350.  
  351. /*    A struct for catalog info */
  352.  
  353. typedef struct fiad_catalog
  354. {
  355.     OSPathSpec    path;
  356.     u32            total_sectors;            // for 'disk' (256-byte)
  357.     u32            free_sectors;            // for 'disk' (256-byte)
  358.     u32            entries, entries_max;
  359.     int               *index;                    // index for filenames[] and fdrs[]
  360.     char        **filenames;            // in host format
  361.     fdrrec        *fdrs;
  362. } fiad_catalog;
  363.  
  364. /*    Free a catalog */
  365. void
  366. fiad_catalog_free_catalog(fiad_catalog *cat);
  367.  
  368. /*    Read a catalog from a directory */
  369. OSError
  370. fiad_catalog_read_catalog(fiad_catalog *cat, const char *wildcard);
  371.  
  372. /*    Sort a catalog */
  373.  
  374. enum
  375. {
  376.     FIAD_CATALOG_SORT_BY_DISK,    // i.e., disk directory order (none!)
  377.     FIAD_CATALOG_SORT_BY_NAME,
  378.     FIAD_CATALOG_SORT_BY_SIZE,
  379.     FIAD_CATALOG_SORT_BY_TYPE
  380. };
  381. void
  382. fiad_catalog_sort_catalog(fiad_catalog *cat, int sort_by, bool ascending);
  383.  
  384. /*    Get file information from catalog:  index is the sorted index */
  385. int
  386. fiad_catalog_get_file_info(fiad_catalog *cat, int index, fdrrec *fdr);
  387.  
  388. /*    Useful utilities */
  389. const char *
  390. fiad_catalog_get_file_type_string(fdrrec *fdr);
  391.  
  392. #include "cexit.h"
  393.  
  394. #endif
  395.