]> cloudbase.mooo.com Git - z180-stamp.git/blobdiff - fatfs/doc/res/app3.c
Import fatfs R0.12b
[z180-stamp.git] / fatfs / doc / res / app3.c
similarity index 71%
rename from fatfs/doc/img/app3.c
rename to fatfs/doc/res/app3.c
index 662c403f6d8e795be1bc2e608acefb50cfb34ec0..c4cfcf5f050c3aa664009d9769380a312aa2c59d 100644 (file)
@@ -4,11 +4,11 @@
 / This function checks if the file is contiguous with desired size.\r
 / If not, a block of contiguous sectors is allocated to the file.\r
 / If the file has been opened without FA_WRITE flag, it only checks if\r
-/ the file is contiguous and returns the resulut. */\r
-\r
-#if _FATFS != 80367 /* Check if R0.10c */\r
-#error This function may not be compatible with this revision of FatFs module.\r
-#endif\r
+/ the file is contiguous and returns the resulut.\r
+/-----------------------------------------------------------------------/\r
+/ This function can work with FatFs R0.09 - R0.11a.\r
+/ It is incompatible with R0.12+. Use f_expand function instead.\r
+/----------------------------------------------------------------------*/\r
 \r
 /* Declarations of FatFs internal functions accessible from applications.\r
 /  This is intended to be used for disk checking/fixing or dirty hacks :-) */\r
@@ -37,16 +37,20 @@ DWORD allocate_contiguous_clusters (    /* Returns the first sector in LBA (0:er
         do {\r
             cl = get_fat(fp->fs, ccl);  /* Get the cluster status */\r
             if (cl + 1 < 3) return 0;   /* Hard error? */\r
-            if (cl != ccl + 1 &&; cl < fp->fs->n_fatent) break;  /* Not contiguous? */\r
+            if (cl != ccl + 1 && cl < fp->fs->n_fatent) break;  /* Not contiguous? */\r
             ccl = cl;\r
         } while (++ncl < tcl);\r
         if (ncl == tcl)             /* Is the file contiguous? */\r
-            return clust2sect(fp->fs, fp->sclust);  /* Return file start sector */\r
+            return clust2sect(fp->fs, fp->sclust);  /* File is contiguous. Return the start sector */\r
     }\r
+\r
+    /* File is not contiguous */\r
 #if _FS_READONLY\r
-    return 0;\r
+    return 0;                                                          /* Exit if in read-only cfg. */\r
 #else\r
-    if (f_truncate(fp)) return 0;   /* Remove the existing chain */\r
+    if (!(fp->flag & FA_WRITE)) return 0;   /* Exit if the file object is for read-only */\r
+\r
+    if (f_truncate(fp)) return 0;           /* Remove the non-contiguous chain */\r
 \r
     /* Find a free contiguous area */\r
     ccl = cl = 2; ncl = 0;\r
@@ -80,24 +84,23 @@ int main (void)
     DWORD org;\r
 \r
 \r
-    /* Open or create a file */\r
+    /* Open or create a file to write */\r
     f_mount(&fs, "", 0);\r
-    fr = f_open(&fil, "swapfile.sys", FA_READ | FA_WRITE | FA_OPEN_ALWAYS);\r
+    fr = f_open(&fil, "fastrec.log", FA_READ | FA_WRITE | FA_OPEN_ALWAYS);\r
     if (fr) return 1;\r
 \r
-    /* Check if the file is 64MB in size and occupies a contiguous area.\r
+    /* Check if the file is 256MB in size and occupies a contiguous area.\r
     /  If not, a contiguous area will be re-allocated to the file. */\r
-    org = allocate_contiguous_clusters(&fil, 0x4000000);\r
+    org = allocate_contiguous_clusters(&fil, 0x10000000);\r
     if (!org) {\r
         printf("Function failed due to any error or insufficient contiguous area.\n");\r
         f_close(&fil);\r
         return 1;\r
     }\r
 \r
-    /* Now you can read/write the file with disk functions bypassing the file system layer. */\r
-\r
+    /* Now you can read/write the file without file system layer. */\r
+    ...\r
     dr = disk_write(fil.fs->drv, Buff, org, 1024);   /* Write 512KiB from top of the file */\r
-\r
     ...\r
 \r
     f_close(&fil);\r