So, a few days ago I showed file reading working with my FAT driver. Today, I give you writing. It took a little bit of tweaking to get right; I made a few mistakes here and there. I’m not 100% confident in it yet, but it’s at least working for the simple case – opening an existing file, writing over it and extending it over at least two more clusters. You still can’t create files or alter directory entries, but it appears I’m correctly modifying them with changes to the file length and whatnot. All good. 🙂
So now it’s a matter of doing some more, proper testing, getting file & directory creation working, and then tweaking in FAT12/FAT16 support – thus far I’ve been focusing only on FAT32 for all this file I/O stuff; you can read a FAT12/FAT16 volume boot sector, but not any data from it. Luckily, FAT16 is a pretty trivial tweak to support. FAT12 will be more laborious… I may not bother supporting it. Having one and a half bytes for each FAT entry is just a pain.
Hopefully this stops Rob from pouting so much about the communications stuff and how it’s all my fault I don’t still use my old 1992 IIvx with it’s serial ports (which were, I think, RS-422 anyway… even when Mac’s did have serial they at least had half decent serial 😝).
There’s two examples here… one tests single-byte-at-a-time read & write cycles over the whole file, the other writes a 100 byte buffer 20 times and reads it back again.
Hehe… I think I’ll patent my coolifying technique. Just imagine if all code really did look like that. 😆
Successfully opened "/Code/Sleep.h". Making "/Code/Sleep.h" cooler... #ifndef PRJ_SLEEP_H #define PRJ_SLEEP_H /*! @function sleep @abstract 'Sleeps' (i.e. delays return) for the given number of milliseconds. @discussion This function only guarantees it will wait at least the given number of milliseconds before returning - it may take longer to return based on numerous factors. @param milliseconds The number of milliseconds to wait. */ void sleep(unsigned int milliseconds); /*! @function usleep @abstract 'Sleeps' (i.e. delays return) for the given number of microseconds. @discussion This function only guarantees it will wait at least the given number of microseconds before returning - it may take longer to return based on numerous factors. @param microseconds The number of microseconds to wait. */ void usleep(unsigned int microseconds); #endif Made "/Code/Sleep.h" cooler (replaced 814 bytes). Seeking back to start of file... #iFnDeF PrJ_SlEeP_H #DeFiNe pRj_sLeEp_h /*! @fUnCtIoN SlEeP @aBsTrAcT 'sLeEpS' (I.E. dElAyS ReTuRn) FoR ThE GiVeN NuMbEr oF MiLlIsEcOnDs. @dIsCuSsIoN ThIs fUnCtIoN OnLy gUaRaNtEeS It wIlL WaIt aT LeAsT ThE GiVeN NuMbEr oF MiLlIsEcOnDs bEfOrE ReTuRnInG - It mAy tAkE LoNgEr tO ReTuRn bAsEd oN NuMeRoUs fAcToRs. @pArAm mIlLiSeCoNdS ThE NuMbEr oF MiLlIsEcOnDs tO WaIt. */ VoId sLeEp(uNsIgNeD InT MiLlIsEcOnDs); /*! @fUnCtIoN UsLeEp @AbStRaCt 'SlEePs' (i.e. DeLaYs rEtUrN) fOr tHe gIvEn nUmBeR Of mIcRoSeCoNdS. @DiScUsSiOn tHiS FuNcTiOn oNlY GuArAnTeEs iT WiLl wAiT At lEaSt tHe gIvEn nUmBeR Of mIcRoSeCoNdS BeFoRe rEtUrNiNg - iT MaY TaKe lOnGeR To rEtUrN BaSeD On nUmErOuS FaCtOrS. @PaRaM MiCrOsEcOnDs tHe nUmBeR Of mIcRoSeCoNdS To wAiT. */ vOiD UsLeEp(uNsIgNeD InT MiCrOsEcOnDs); #eNdIf Read 814 bytes in total Successfully closed "/Code/Sleep.h". ..... [snipped for brevity] ...... Successfully opened "/Code/Sleep.h". Wrote 2000 bytes to "/Code/Sleep.h". Seeking back to start of file... Current time: Sat Jun 17 19:42:50 2006 Current user: wadetregaskis Current time: Sat Jun 17 19:42:50 2006 Current user: wadetregaskis Current time: Sat Jun 17 19:42:50 2006 Current user: wadetregaskis Current time: Sat Jun 17 19:42:50 2006 Current user: wadetregaskis Current time: Sat Jun 17 19:42:50 2006 Current user: wadetregaskis Current time: Sat Jun 17 19:42:50 2006 Current user: wadetregaskis Current time: Sat Jun 17 19:42:50 2006 Current user: wadetregaskis Current time: Sat Jun 17 19:42:50 2006 Current user: wadetregaskis Current time: Sat Jun 17 19:42:50 2006 Current user: wadetregaskis Current time: Sat Jun 17 19:42:50 2006 Current user: wadetregaskis Current time: Sat Jun 17 19:42:50 2006 Current user: wadetregaskis Current time: Sat Jun 17 19:42:50 2006 Current user: wadetregaskis Current time: Sat Jun 17 19:42:50 2006 Current user: wadetregaskis Current time: Sat Jun 17 19:42:50 2006 Current user: wadetregaskis Current time: Sat Jun 17 19:42:50 2006 Current user: wadetregaskis Current time: Sat Jun 17 19:42:50 2006 Current user: wadetregaskis Current time: Sat Jun 17 19:42:50 2006 Current user: wadetregaskis Current time: Sat Jun 17 19:42:50 2006 Current user: wadetregaskis Current time: Sat Jun 17 19:42:50 2006 Current user: wadetregaskis Current time: Sat Jun 17 19:42:50 2006 Current user: wadetregaskis Read 2000 bytes in total Successfully closed "/Code/Sleep.h".