Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
authorWuzzy <Wuzzy2@mail.ru>
Sat, 11 May 2019 20:31:51 +0200
changeset 14913 68e1783762bc
parent 14912 3a53309e684b
child 14914 9ab78e08a34c
Pas2C: Support FileSize and Delete; add dummy implementation of FormatDateTime
hedgewars/pas2cRedo.pas
project_files/hwc/rtl/SysUtils.h
project_files/hwc/rtl/fileio.c
project_files/hwc/rtl/fileio.h
project_files/hwc/rtl/sysutils.c
--- a/hedgewars/pas2cRedo.pas	Sat May 11 04:58:11 2019 +0200
+++ b/hedgewars/pas2cRedo.pas	Sat May 11 20:31:51 2019 +0200
@@ -48,6 +48,8 @@
 
     Handle = integer;
 
+    TDateTime = double;
+
 var
     write, writeLn, read, readLn, flush, CreateDir: procedure;
 
@@ -84,7 +86,8 @@
 
      min, max:function:integer;
     assign, rewrite, rewrite_2, reset, reset_2, flush, BlockWrite, BlockRead, close : procedure;
-    FileExists, DirectoryExists, eof : function : boolean;
+    FileSize : function: Int64;
+    FileExists, DirectoryExists, eof, DeleteFile : function : boolean;
 
     ParamCount : function : integer;
     ParamStr : function : string;
--- a/project_files/hwc/rtl/SysUtils.h	Sat May 11 04:58:11 2019 +0200
+++ b/project_files/hwc/rtl/SysUtils.h	Sat May 11 20:31:51 2019 +0200
@@ -6,8 +6,12 @@
 // EFFECTS: return the current date time in pascal notation
 //          http://www.merlyn.demon.co.uk/del-prgg.htm#TDT
 TDateTime   fpcrtl_now();
-#define     now                     fpcrtl_now
-#define     Now                     fpcrtl_now
+#define     fpcrtl_Now              fpcrtl_now
+#define     now                     fpcrtl_Now
+#define     Now                     fpcrtl_Now
+
+string255   fpcrtl_formatDateTime(string255 FormatStr, TDateTime DateTime);
+#define     fpcrtl_FormatDateTime   fpcrtl_formatDateTime
 
 // EFFECTS: return the current time
 //          http://www.merlyn.demon.co.uk/del-prgg.htm#TDT
--- a/project_files/hwc/rtl/fileio.c	Sat May 11 04:58:11 2019 +0200
+++ b/project_files/hwc/rtl/fileio.c	Sat May 11 20:31:51 2019 +0200
@@ -241,3 +241,35 @@
     fflush(f);
 }
 
+Int64 fpcrtl_fileSize(File f)
+{
+    assert(f->record_len > 0);
+
+    IOResult = IO_NO_ERROR;
+    int i = fseek(f->fp, 0, SEEK_END);
+    if (i == -1) {
+        IOResult = IO_ERROR_DUMMY;
+        return -1;
+    }
+    long size = ftell(f->fp);
+    if (size == -1) {
+        IOResult = IO_ERROR_DUMMY;
+        return -1;
+    }
+    return size / f->record_len;
+}
+
+bool fpcrtl_deleteFile(string255 filename)
+{
+    FIX_STRING(filename);
+
+    int ret = remove(filename.str);
+    if(ret == 0) {
+       IOResult = IO_NO_ERROR;
+       return true;
+    } else {
+       IOResult = IO_ERROR_DUMMY;
+       return false;
+    }
+}
+
--- a/project_files/hwc/rtl/fileio.h	Sat May 11 04:58:11 2019 +0200
+++ b/project_files/hwc/rtl/fileio.h	Sat May 11 20:31:51 2019 +0200
@@ -78,4 +78,10 @@
 char *      fpcrtl_getCurrentDir(void);
 #define     fpcrtl_GetCurrentDir                        fpcrtl_getCurrentDir
 
+Int64       fpcrtl_fileSize(File f);
+#define     fpcrtl_FileSize                             fpcrtl_fileSize
+
+bool        fpcrtl_deleteFile(string255 filename);
+#define     fpcrtl_DeleteFile                           fpcrtl_deleteFile
+
 #endif /* FILEIO_H_ */
--- a/project_files/hwc/rtl/sysutils.c	Sat May 11 04:58:11 2019 +0200
+++ b/project_files/hwc/rtl/sysutils.c	Sat May 11 20:31:51 2019 +0200
@@ -80,13 +80,24 @@
     return fpcrtl_date() + fpcrtl_time();
 }
 
-/*
- * XXX: dummy
- */
+// Semi-dummy implementation of FormatDateTime
 string255 fpcrtl_formatDateTime(string255 FormatStr, TDateTime DateTime)
 {
-    string255 result = STRINIT("2012 01-01");
-    return result;
+    string255 buffer = STRINIT(FormatStr.str);
+    time_t rawtime;
+    struct tm* my_tm;
+
+    // DateTime is ignored, always uses current time.
+    // TODO: Use DateTime argument properly.
+    time(&rawtime);
+    my_tm = localtime(&rawtime);
+
+    // Currently uses a hardcoded format string, FormatStr is ignored.
+    // The format strings for C and Pascal differ!
+    // TODO: Use FormatStr argument properly.
+    size_t len = strftime(buffer.str, sizeof(buffer.str), "%Y-%m-%d-%H-%M-%S-0", my_tm);
+    buffer.len = len;
+    return buffer;
 }
 
 string255 fpcrtl_trim(string255 s)