File modification time: Difference between revisions
Content added Content deleted
(Changed code in order to use the functions of standard module “os”.) |
(Added Wren) |
||
Line 1,444: | Line 1,444: | ||
Dim accessTime = file.LastAccessTime |
Dim accessTime = file.LastAccessTime |
||
file.LastAccessTime = accessTime.AddHours(1)</lang> |
file.LastAccessTime = accessTime.AddHours(1)</lang> |
||
=={{header|Wren}}== |
|||
{{trans|C}} |
|||
{{libheader|POSIX}} |
|||
{{libheader|Wren-date}} |
|||
As there is currently no way for Wren-cli to get or set file modification times, we instead use an embedded script so the C host can do this for us. |
|||
<lang ecmascript>/* file_mod_time_wren */ |
|||
import "./date" for Date |
|||
foreign class Stat { |
|||
construct new(fileName) {} |
|||
foreign atime |
|||
foreign mtime |
|||
} |
|||
foreign class Utimbuf { |
|||
construct new(actime, modtime) {} |
|||
foreign utime(fileName) |
|||
} |
|||
// gets a Date object from a Unix time in seconds |
|||
var UT2Date = Fn.new { |ut| Date.unixEpoch.addSeconds(ut) } |
|||
Date.default = "yyyy|-|mm|-|dd| |hh|:|MM|:|ss" // default format for printing |
|||
var fileName = "temp.txt" |
|||
var st = Stat.new(fileName) |
|||
System.print("'%(fileName)' was last modified on %(UT2Date.call(st.mtime)).") |
|||
var utb = Utimbuf.new(st.atime, 0) // atime unchanged, mtime = current time |
|||
if (utb.utime(fileName) < 0) { |
|||
System.print("There was an error changing the file modification time.") |
|||
return |
|||
} |
|||
st = Stat.new(fileName) // update info |
|||
System.print("File modification time changed to %(UT2Date.call(st.mtime)).")</lang> |
|||
<br> |
|||
We now embed this in the following C program, build and run it: |
|||
<lang c>/* gcc file_mod_time.c -o file_mod_time -lwren -lm */ |
|||
#include <sys/stat.h> |
|||
#include <stdio.h> |
|||
#include <stdlib.h> |
|||
#include <string.h> |
|||
#include <time.h> |
|||
#include <utime.h> |
|||
#include "wren.h" |
|||
/* Stat functions */ |
|||
void Stat_allocate(WrenVM* vm) { |
|||
struct stat *pStat = (struct stat*)wrenSetSlotNewForeign(vm, 0, 0, sizeof(struct stat)); |
|||
const char *filename = wrenGetSlotString(vm, 1); |
|||
if (stat(filename, pStat) < 0) { |
|||
perror(filename); |
|||
exit(1); |
|||
} |
|||
} |
|||
void Stat_atime(WrenVM* vm) { |
|||
struct stat *pStat = (struct stat*)wrenGetSlotForeign(vm, 0); |
|||
wrenSetSlotDouble(vm, 0, (double)pStat->st_atime); |
|||
} |
|||
void Stat_mtime(WrenVM* vm) { |
|||
struct stat *pStat = (struct stat*)wrenGetSlotForeign(vm, 0); |
|||
wrenSetSlotDouble(vm, 0, (double)pStat->st_mtime); |
|||
} |
|||
/* Utimbuf functions */ |
|||
void Utimbuf_allocate(WrenVM* vm) { |
|||
struct utimbuf *pUtimbuf = (struct utimbuf*)wrenSetSlotNewForeign(vm, 0, 0, sizeof(struct utimbuf)); |
|||
time_t actime = (time_t)wrenGetSlotDouble(vm, 1); |
|||
if (!actime) actime = time(NULL); |
|||
pUtimbuf->actime = actime; |
|||
time_t modtime = (time_t)wrenGetSlotDouble(vm, 2); |
|||
if (!modtime) modtime = time(NULL); |
|||
pUtimbuf->modtime = modtime; |
|||
} |
|||
void Utimbuf_utime(WrenVM* vm) { |
|||
const struct utimbuf *pUtimbuf = (const struct utimbuf*)wrenGetSlotForeign(vm, 0); |
|||
const char *filename = wrenGetSlotString(vm, 1); |
|||
int res = utime(filename, pUtimbuf); |
|||
wrenSetSlotDouble(vm, 0, (double)res); |
|||
} |
|||
WrenForeignClassMethods bindForeignClass(WrenVM* vm, const char* module, const char* className) { |
|||
WrenForeignClassMethods methods; |
|||
methods.allocate = NULL; |
|||
methods.finalize = NULL; |
|||
if (strcmp(module, "main") == 0) { |
|||
if (strcmp(className, "Stat") == 0) { |
|||
methods.allocate = Stat_allocate; |
|||
} else if (strcmp(className, "Utimbuf") == 0) { |
|||
methods.allocate = Utimbuf_allocate; |
|||
} |
|||
} |
|||
return methods; |
|||
} |
|||
WrenForeignMethodFn bindForeignMethod( |
|||
WrenVM* vm, |
|||
const char* module, |
|||
const char* className, |
|||
bool isStatic, |
|||
const char* signature) { |
|||
if (strcmp(module, "main") == 0) { |
|||
if (strcmp(className, "Stat") == 0) { |
|||
if(!isStatic && strcmp(signature, "atime") == 0) return Stat_atime; |
|||
if(!isStatic && strcmp(signature, "mtime") == 0) return Stat_mtime; |
|||
} else if (strcmp(className, "Utimbuf") == 0) { |
|||
if(!isStatic && strcmp(signature, "utime(_)") == 0) return Utimbuf_utime; |
|||
} |
|||
} |
|||
return NULL; |
|||
} |
|||
static void writeFn(WrenVM* vm, const char* text) { |
|||
printf("%s", text); |
|||
} |
|||
void errorFn(WrenVM* vm, WrenErrorType errorType, const char* module, const int line, const char* msg) { |
|||
switch (errorType) { |
|||
case WREN_ERROR_COMPILE: |
|||
printf("[%s line %d] [Error] %s\n", module, line, msg); |
|||
break; |
|||
case WREN_ERROR_STACK_TRACE: |
|||
printf("[%s line %d] in %s\n", module, line, msg); |
|||
break; |
|||
case WREN_ERROR_RUNTIME: |
|||
printf("[Runtime Error] %s\n", msg); |
|||
break; |
|||
} |
|||
} |
|||
char *readFile(const char *fileName) { |
|||
FILE *f = fopen(fileName, "r"); |
|||
fseek(f, 0, SEEK_END); |
|||
long fsize = ftell(f); |
|||
rewind(f); |
|||
char *script = malloc(fsize + 1); |
|||
size_t ret = fread(script, 1, fsize, f); |
|||
if (ret != fsize) printf("Error reading %s\n", fileName); |
|||
fclose(f); |
|||
script[fsize] = 0; |
|||
return script; |
|||
} |
|||
static void loadModuleComplete(WrenVM* vm, const char* module, WrenLoadModuleResult result) { |
|||
if( result.source) free((void*)result.source); |
|||
} |
|||
WrenLoadModuleResult loadModule(WrenVM* vm, const char* name) { |
|||
WrenLoadModuleResult result = {0}; |
|||
if (strcmp(name, "random") != 0 && strcmp(name, "meta") != 0) { |
|||
result.onComplete = loadModuleComplete; |
|||
char fullName[strlen(name) + 6]; |
|||
strcpy(fullName, name); |
|||
strcat(fullName, ".wren"); |
|||
result.source = readFile(fullName); |
|||
} |
|||
return result; |
|||
} |
|||
int main(int argc, char **argv) { |
|||
WrenConfiguration config; |
|||
wrenInitConfiguration(&config); |
|||
config.writeFn = &writeFn; |
|||
config.errorFn = &errorFn; |
|||
config.bindForeignClassFn = &bindForeignClass; |
|||
config.bindForeignMethodFn = &bindForeignMethod; |
|||
config.loadModuleFn = &loadModule; |
|||
WrenVM* vm = wrenNewVM(&config); |
|||
const char* module = "main"; |
|||
const char* fileName = "file_mod_time.wren"; |
|||
char *script = readFile(fileName); |
|||
WrenInterpretResult result = wrenInterpret(vm, module, script); |
|||
switch (result) { |
|||
case WREN_RESULT_COMPILE_ERROR: |
|||
printf("Compile Error!\n"); |
|||
break; |
|||
case WREN_RESULT_RUNTIME_ERROR: |
|||
printf("Runtime Error!\n"); |
|||
break; |
|||
case WREN_RESULT_SUCCESS: |
|||
break; |
|||
} |
|||
wrenFreeVM(vm); |
|||
free(script); |
|||
return 0; |
|||
}</lang> |
|||
{{out}} |
|||
Sample output: |
|||
<pre> |
|||
'temp.txt' was last modified on 2021-12-05 14:14:30. |
|||
File modification time changed to 2021-12-05 14:23:42. |
|||
</pre> |
|||
=={{header|zkl}}== |
=={{header|zkl}}== |