Fix REPOSITION-FILE, HISTORY, locked file handle and other problems.
[pforth] / csrc / pf_save.c
index 128b474..f3f9ac0 100644 (file)
@@ -343,6 +343,33 @@ error:
        return -1;\r
 }\r
 \r
        return -1;\r
 }\r
 \r
+/* Convert dictionary info chunk between native and on-disk (big-endian). */\r
+static void\r
+convertDictionaryInfoWrite (DictionaryInfoChunk *sd)\r
+{\r
+/* Convert all fields in DictionaryInfoChunk from Native to BigEndian. \r
+ * This assumes they are all 32-bit integers.\r
+ */\r
+       int   i;\r
+       uint32_t *p = (uint32_t *) sd;\r
+       for (i=0; i<((int)(sizeof(*sd)/sizeof(uint32_t))); i++)\r
+       {\r
+               Write32BigEndian( (uint8_t *)&p[i], p[i] );\r
+       }\r
+}\r
+\r
+static void\r
+convertDictionaryInfoRead (DictionaryInfoChunk *sd)\r
+{\r
+/* Convert all fields in structure from BigEndian to Native. */\r
+       int   i;\r
+       uint32_t *p = (uint32_t *) sd;\r
+       for (i=0; i<((int)(sizeof(*sd)/sizeof(uint32_t))); i++)\r
+       {\r
+               p[i] = Read32BigEndian( (uint8_t *)&p[i] );\r
+       }\r
+}\r
+\r
 /****************************************************************\r
 ** Save Dictionary in File.\r
 ** If EntryPoint is NULL, save as development environment.\r
 /****************************************************************\r
 ** Save Dictionary in File.\r
 ** If EntryPoint is NULL, save as development environment.\r
@@ -356,7 +383,6 @@ cell_t ffSaveForth( const char *FileName, ExecToken EntryPoint, cell_t NameSize,
        uint32_t NameChunkSize = 0;\r
        uint32_t CodeChunkSize;\r
        uint32_t relativeCodePtr;\r
        uint32_t NameChunkSize = 0;\r
        uint32_t CodeChunkSize;\r
        uint32_t relativeCodePtr;\r
-       int   i;\r
 \r
        fid = sdOpenFile( FileName, "wb" );\r
        if( fid == NULL )\r
 \r
        fid = sdOpenFile( FileName, "wb" );\r
        if( fid == NULL )\r
@@ -445,16 +471,7 @@ cell_t ffSaveForth( const char *FileName, ExecToken EntryPoint, cell_t NameSize,
        SD.sd_CodeSize = CodeSize;\r
 \r
        \r
        SD.sd_CodeSize = CodeSize;\r
 \r
        \r
-/* Convert all fields in DictionaryInfoChunk from Native to BigEndian. \r
- * This assumes they are all 32-bit integers.\r
- */\r
-       {\r
-               uint32_t *p = (uint32_t *) &SD;\r
-               for( i=0; i<((int)(sizeof(SD)/sizeof(uint32_t))); i++ )\r
-               {\r
-                       Write32BigEndian( (uint8_t *)&p[i], p[i] );\r
-               }\r
-       }\r
+       convertDictionaryInfoWrite (&SD);\r
 \r
        if( WriteChunkToFile( fid, ID_P4DI, (char *) &SD, sizeof(DictionaryInfoChunk) ) < 0 ) goto error;\r
 \r
 \r
        if( WriteChunkToFile( fid, ID_P4DI, (char *) &SD, sizeof(DictionaryInfoChunk) ) < 0 ) goto error;\r
 \r
@@ -517,7 +534,6 @@ PForthDictionary pfLoadDictionary( const char *FileName, ExecToken *EntryPointPt
        uint32_t FormSize;\r
        uint32_t BytesLeft;\r
        uint32_t numr;\r
        uint32_t FormSize;\r
        uint32_t BytesLeft;\r
        uint32_t numr;\r
-       int   i;\r
        int   isDicBigEndian;\r
 \r
 DBUG(("pfLoadDictionary( %s )\n", FileName ));\r
        int   isDicBigEndian;\r
 \r
 DBUG(("pfLoadDictionary( %s )\n", FileName ));\r
@@ -568,15 +584,8 @@ DBUG(("pfLoadDictionary( %s )\n", FileName ));
                        if( numr != ChunkSize ) goto read_error;\r
                        BytesLeft -= ChunkSize;\r
                        \r
                        if( numr != ChunkSize ) goto read_error;\r
                        BytesLeft -= ChunkSize;\r
                        \r
-/* Convert all fields in structure from BigEndian to Native. */\r
-                       {\r
-                               uint32_t *p = (uint32_t *) sd;\r
-                               for( i=0; i<((int)(sizeof(*sd)/sizeof(uint32_t))); i++ )\r
-                               {\r
-                                       p[i] = Read32BigEndian( (uint8_t *)&p[i] );\r
-                               }\r
-                       }\r
-                               \r
+                       convertDictionaryInfoRead (sd);\r
+\r
                        isDicBigEndian = sd->sd_Flags & SD_F_BIG_ENDIAN_DIC;\r
 \r
                        if( !gVarQuiet )\r
                        isDicBigEndian = sd->sd_Flags & SD_F_BIG_ENDIAN_DIC;\r
 \r
                        if( !gVarQuiet )\r