]> git.sur5r.net Git - cc65/commitdiff
CBM directory routines by Josef Soucek
authorcuz <cuz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Sun, 2 Feb 2003 13:18:40 +0000 (13:18 +0000)
committercuz <cuz@b7a2c559-68d2-44c3-8de9-860c34a00d81>
Sun, 2 Feb 2003 13:18:40 +0000 (13:18 +0000)
git-svn-id: svn://svn.cc65.org/cc65/trunk@1920 b7a2c559-68d2-44c3-8de9-860c34a00d81

include/cbm.h
libsrc/cbm/.cvsignore
libsrc/cbm/Makefile
libsrc/cbm/cbm_dir.c [new file with mode: 0644]

index 1bfaa7600c1dc8ecb9359f447468778bb827a5a2..440b969353a206a948a56834a2a7434f327219fd 100644 (file)
@@ -110,6 +110,36 @@ extern unsigned char _filetype;         /* Default 'u' */
 
 
 
+/*****************************************************************************/
+/*                Definitions for directory reading functions                */
+/*****************************************************************************/
+
+
+
+/* CBM FILE TYPES */
+#define CBM_T_DEL   0
+#define CBM_T_SEQ   1
+#define CBM_T_PRG   2
+#define CBM_T_USR   3
+#define CBM_T_REL   4
+#define CBM_T_CBM   5           /* 1581 sub-partition */
+#define CBM_T_DIR   6           /* IDE64 and CMD sub-directory */
+#define CBM_T_VRP   8           /* Vorpal fast-loadable format */
+#define CBM_T_OTHER 5           /* Other file-types not yet defined */
+
+/* CBM FILE ACCESS */
+#define CBM_A_RO    1           /* Read only   */
+#define CBM_A_RW    3           /* Read, Write */
+
+struct cbm_dirent {     
+    char          name[17];     /* File name in PETSCII, limited to 16 chars */
+    unsigned int  size;         /* Size in 256B blocks */
+    unsigned char type;
+    unsigned char access;
+};
+
+
+
 /*****************************************************************************/
 /*                               Machine info                                */
 /*****************************************************************************/
@@ -219,9 +249,23 @@ int __fastcall__ cbm_read (unsigned char lfn, void* buffer, unsigned int size);
 int __fastcall__ cbm_write (unsigned char lfn, void* buffer, unsigned int size);
 /* Writes up to "size" bytes from "buffer" to a file.
  * Returns the number of actually written bytes or -1 in case of an error.
- * _oserror contains an errorcode then (see table below).
+ * _oserror contains an errorcode then (see above table).
+ */
+
+unsigned char __fastcall__ cbm_opendir (unsigned char lfn, unsigned char device);
+/* Opens directory listing.
+ * Returns 0 if opening directory was successful,
+ * othervise errorcode corresponding to cbm_open()
+ */
+
+unsigned char __fastcall__ cbm_readdir (unsigned char lfn, struct cbm_dirent* l_dirent);
+/* Reads one directory line into cbm_dirent structure.
+ * Returns 0 if reading directory line was successful.
+ * Returns 'true' if reading directory failed or no more files to read.
  */
 
+void __fastcall__ cbm_closedir (unsigned char lfn);
+/* Closes directory by cbm_close (unsigned char lfn) */
 
 
 
index 4b273aef66324bbabbb6887a792718250f7420f9..944f0ed3b7dcd69ac587889a7fe2483185ed2be3 100644 (file)
@@ -1,2 +1,3 @@
+cbm_dir.s
 cbm_load.s
 cbm_save.s
index d3095640b5496d7fc175b8d0f92b245cc06201d4..8abdb81043d6a12886383f716104dd071cc1c66f 100644 (file)
@@ -11,7 +11,8 @@
 %.o:   %.s
        @$(AS) -g -o $@ $(AFLAGS) $<
 
-C_OBJS =       cbm_load.o      \
+C_OBJS =       cbm_dir.o       \
+                cbm_load.o     \
                cbm_save.o
 
 S_OBJS =       c_acptr.o       \
diff --git a/libsrc/cbm/cbm_dir.c b/libsrc/cbm/cbm_dir.c
new file mode 100644 (file)
index 0000000..bedbdaf
--- /dev/null
@@ -0,0 +1,146 @@
+/* This is very simplified version of POSIX opendir(), readdir() and closedir() */
+/* for Commodore computers.                                                     */
+/* Created by Josef Soucek, 2003    E-mail:josef.soucek@ct.cz                   */
+
+/* Version 0.1 - 21.1.2003 */
+/* Tested with floppy drive and IDE64 devices                                   */
+/* Not tested with messed (buggy) directory listing                             */
+/* Limits filenames to 16 chars (VICE supports more in directory listing)       */
+
+
+
+#include <cbm.h>
+
+
+
+unsigned char __fastcall__ cbm_opendir (unsigned char lfn, unsigned char device)
+{
+    unsigned char status;
+    if ((status = cbm_open (lfn, device, CBM_READ, "$")) == 0) {
+        if (cbm_k_chkin (lfn) == 0) {
+            if (cbm_k_basin () == 0x01) {         /* Start address */
+                if (cbm_k_basin () == 0x04) {
+                    cbm_k_clrch ();
+                    return 0;
+                }
+            } else {
+                cbm_close (lfn);
+                cbm_k_clrch ();
+                return 2;
+            }
+        }
+    }
+    return status;
+}
+
+
+
+unsigned char __fastcall__ cbm_readdir (unsigned char lfn, register struct cbm_dirent* l_dirent)
+{
+    unsigned char byte, i;
+
+    if (cbm_k_chkin (lfn) == 0) {
+        if (cbm_k_readst () == 0) {
+            cbm_k_basin ();                    /* 0x01, 0x01, next basic line */
+            cbm_k_basin ();
+
+            byte = cbm_k_basin();             /* File-size */
+            l_dirent->size = byte | ((cbm_k_basin()) << 8);
+
+            byte = cbm_k_basin();
+
+            if (byte == 'b') {                 /* "B" BLOCKS FREE. */
+                while (cbm_k_readst () == 0) { /* Read until end */
+                    cbm_k_basin ();
+                }
+                cbm_k_clrch ();
+                return 2;                       /* END */
+            }
+
+            if (byte != '\"') {
+                while (cbm_k_basin() != '\"') {
+                    if (cbm_k_readst () != 0) {   /* ### Included to prevent */
+                        cbm_k_clrch ();           /* ### Endless loop */
+                        return 3;                 /* ### Should be probably removed */
+                    }                             /* ### */
+                }
+            }
+
+            i = 0;
+            while ((byte = cbm_k_basin ()) != '\"') {
+                if (cbm_k_readst () != 0) {     /* ### Included to prevent */
+                    cbm_k_clrch ();             /* ### Endless loop */
+                    return 4;                   /* ### Should be probably removed */
+                }                               /* ### */
+
+                if (i < sizeof (l_dirent->name) - 1) {
+                    l_dirent->name[i] = byte;
+                    ++i;
+                }
+            }
+            l_dirent->name[i] = '\0';
+
+            while ((byte=cbm_k_basin ()) == ' ') {
+                if (cbm_k_readst ()) {          /* ### Included to prevent */
+                    cbm_k_clrch ();             /* ### Endless loop */
+                    return 5;                   /* ### Should be probably removed */
+                }                               /* ### */
+            }
+
+            switch (byte) {
+                case 's':
+                    l_dirent->type = CBM_T_SEQ;
+                    break;
+                case 'p':
+                    l_dirent->type = CBM_T_PRG;
+                    break;
+                case 'u':
+                    l_dirent->type = CBM_T_USR;
+                    break;
+                case 'r':
+                    l_dirent->type = CBM_T_REL;
+                    break;
+                case 'c':
+                    l_dirent->type = CBM_T_CBM;
+                    break;
+                case 'd':
+                    l_dirent->type = CBM_T_DIR;
+                    break;
+                case 'v':
+                    l_dirent->type = CBM_T_VRP;
+                    break;
+                default:
+                    l_dirent->type = CBM_T_OTHER;
+            }
+
+            cbm_k_basin ();
+            cbm_k_basin ();
+
+            byte = cbm_k_basin ();
+
+            l_dirent->access = (byte == 0x3C)? CBM_A_RO : CBM_A_RW;
+
+            if (byte != 0) {
+                while (cbm_k_basin() != 0) {
+                    if (cbm_k_readst () != 0) { /* ### Included to prevent */
+                        cbm_k_clrch ();         /* ### Endless loop */
+                        return 6;               /* ### Should be probably removed */
+                    }                           /* ### */
+                }
+            }
+
+            cbm_k_clrch ();
+            return 0;                         /* Line read successfuly */
+        }
+    }
+    cbm_k_clrch ();
+    return 1;
+}
+
+
+void __fastcall__ cbm_closedir( unsigned char lfn)
+{
+    cbm_close(lfn);
+}
+
+