[colobot] 343/390: Changed CBot file handling, potentially solved problems with 64-bit executables

Didier Raboud odyx at moszumanska.debian.org
Fri Jun 12 14:22:02 UTC 2015


This is an automated email from the git hooks/post-receive script.

odyx pushed a commit to branch upstream/latest
in repository colobot.

commit 1db9d8bdca98989b38fd91efffa00c44daf269dc
Author: Tomasz Kapuściński <tomaszkax86 at gmail.com>
Date:   Wed May 6 20:39:09 2015 +0200

    Changed CBot file handling, potentially solved problems with 64-bit executables
---
 src/script/scriptfunc.cpp | 49 ++++++++++++++++++++++++++++++++++-------------
 src/script/scriptfunc.h   |  4 ++++
 2 files changed, 40 insertions(+), 13 deletions(-)

diff --git a/src/script/scriptfunc.cpp b/src/script/scriptfunc.cpp
index 281a216..cd05061 100644
--- a/src/script/scriptfunc.cpp
+++ b/src/script/scriptfunc.cpp
@@ -56,7 +56,6 @@
 #endif
 
 
-
 // Compiling a procedure without any parameters.
 
 CBotTypResult CScriptFunctions::cNull(CBotVar* &var, void* user)
@@ -3249,8 +3248,10 @@ bool CScriptFunctions::rCameraFocus(CBotVar* var, CBotVar* result, int& exceptio
 
 // Static variables
 
-int          CScriptFunctions::m_CompteurFileOpen = 0;
-std::string  CScriptFunctions::m_filesDir;
+int                                 CScriptFunctions::m_CompteurFileOpen = 0;
+std::string                         CScriptFunctions::m_filesDir;
+std::unordered_map<int, FILE*>      CScriptFunctions::m_files;
+int                                 CScriptFunctions::m_nextFile = 1;
 
 
 
@@ -3320,10 +3321,14 @@ bool CScriptFunctions::rfconstruct (CBotVar* pThis, CBotVar* pVar, CBotVar* pRes
         if ( pFile == NULL ) { Exception = CBotErrFileOpen; return false; }
         
         m_CompteurFileOpen ++;
+
+        int fileHandle = m_nextFile++;
+
+        m_files[fileHandle] = pFile;
         
         // save the file handle
         pVar = pThis->GetItem("handle");
-        pVar->SetValInt(reinterpret_cast<long>(pFile));
+        pVar->SetValInt(fileHandle);
     }
     
     return true;
@@ -3365,12 +3370,16 @@ bool CScriptFunctions::rfdestruct (CBotVar* pThis, CBotVar* pVar, CBotVar* pResu
     
     // don't open? no problem :)
     if ( pVar->GetInit() != IS_DEF) return true;
+
+    int fileHandle = pVar->GetValInt();
     
-    FILE* pFile= reinterpret_cast<FILE*>(pVar->GetValInt());
+    FILE* pFile = m_files[fileHandle];
     fclose(pFile);
-    m_CompteurFileOpen --;
+    m_CompteurFileOpen--;
     
     pVar->SetInit(IS_NAN);
+
+    m_files.erase(fileHandle);
     
     return true;
 }
@@ -3432,8 +3441,12 @@ bool CScriptFunctions::rfopen (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult,
     m_CompteurFileOpen ++;
     
     // save file handle
+    int fileHandle = m_nextFile++;
+
+    m_files[fileHandle] = pFile;
+
     pVar = pThis->GetItem("handle");
-    pVar->SetValInt(reinterpret_cast<long>(pFile));
+    pVar->SetValInt(fileHandle);
     
     pResult->SetValInt(true);
     return true;
@@ -3472,18 +3485,22 @@ CBotTypResult CScriptFunctions::cfopen (CBotVar* pThis, CBotVar* &pVar)
 bool CScriptFunctions::rfclose (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, int& Exception)
 {
     // it shouldn't be any parameters
-    if ( pVar != NULL ) return CBotErrOverParam;
+    if (pVar != NULL) { Exception = CBotErrOverParam; return false; }
     
     // retrieve the item "handle"
     pVar = pThis->GetItem("handle");
     
     if ( pVar->GetInit() != IS_DEF) { Exception = CBotErrNotOpen; return false; }
     
-    FILE* pFile= reinterpret_cast<FILE*>(pVar->GetValInt());
+    int fileHandle = pVar->GetValInt();
+
+    FILE* pFile = m_files[fileHandle];
     fclose(pFile);
-    m_CompteurFileOpen --;
+    m_CompteurFileOpen--;
     
     pVar->SetInit(IS_NAN);
+
+    m_files.erase(fileHandle);
     
     return true;
 }
@@ -3516,7 +3533,9 @@ bool CScriptFunctions::rfwrite (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult,
     
     if ( pVar->GetInit() != IS_DEF) { Exception = CBotErrNotOpen; return false; }
     
-    FILE* pFile= reinterpret_cast<FILE*>(pVar->GetValInt());
+    int fileHandle = pVar->GetValInt();
+
+    FILE* pFile = m_files[fileHandle];
     
     int res = fputs(param+CBotString("\n"), pFile);
     
@@ -3555,7 +3574,9 @@ bool CScriptFunctions::rfread (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult,
     
     if ( pVar->GetInit() != IS_DEF) { Exception = CBotErrNotOpen; return false; }
     
-    FILE* pFile= reinterpret_cast<FILE*>(pVar->GetValInt());
+    int fileHandle = pVar->GetValInt();
+
+    FILE* pFile = m_files[fileHandle];
     
     char    chaine[2000];
     int     i;
@@ -3596,7 +3617,9 @@ bool CScriptFunctions::rfeof (CBotVar* pThis, CBotVar* pVar, CBotVar* pResult, i
     
     if ( pVar->GetInit() != IS_DEF) { Exception = CBotErrNotOpen; return false; }
     
-    FILE* pFile= reinterpret_cast<FILE*>(pVar->GetValInt());
+    int fileHandle = pVar->GetValInt();
+
+    FILE* pFile = m_files[fileHandle];
     
     pResult->SetValInt( feof( pFile ) );
     
diff --git a/src/script/scriptfunc.h b/src/script/scriptfunc.h
index 365e5e7..08c2be2 100644
--- a/src/script/scriptfunc.h
+++ b/src/script/scriptfunc.h
@@ -29,6 +29,7 @@
 #include "CBot/CBotDll.h"
 
 #include <string>
+#include <unordered_map>
  
 class CObject;
 class CScript;
@@ -178,4 +179,7 @@ private:
     static bool     Process(CScript* script, CBotVar* result, int &exception);
     static bool     ShouldProcessStop(Error err, int errMode);
     static CObject* SearchInfo(CScript* script, CObject* object, float power);
+
+    static std::unordered_map<int, FILE*> m_files;
+    static int m_nextFile;
 };
\ No newline at end of file

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-games/colobot.git



More information about the Pkg-games-commits mailing list