rev 628 - in trunk: include/prothon pr/test src
SVN User <[email protected]> Wed, 16 Jun 2004 01:19:23 -0400
| Newsgroups | gmane.comp.lang.prothon.cvs |
|---|---|
| Message-ID | <[email protected]> |
Author: mark
Date: 2004-06-16 01:19:21 -0400 (Wed, 16 Jun 2004)
New Revision: 628
Modified:
trunk/include/prothon/prothon.h
trunk/pr/test/test.pr
trunk/src/builtins-file.c
trunk/src/builtins-time.c
trunk/src/object.c
Log:
added TempFile
Modified: trunk/include/prothon/prothon.h
===================================================================
--- trunk/include/prothon/prothon.h 2004-06-16 02:31:01 UTC (rev 627)
+++ trunk/include/prothon/prothon.h 2004-06-16 05:19:21 UTC (rev 628)
@@ -305,30 +305,31 @@
FLOAT_PROTO, // 5 Float
IMAG_PROTO, // 6 Imaginary
SYMBOL_PROTO, // 7 Symbol
- SEQ_PROTO, // 8 Sequence
- BYTES_PROTO, // 9 Bytes
- STRING_PROTO, // 10 String
- TUPLE_PROTO, // 11 Tuple
- LIST_PROTO, // 12 List
- DICT_PROTO, // 13 Dict
- FUNC_PROTO, // 14 Func
- METHOD_PROTO, // 15 Method
- HASH_PROTO, // 16 Hash
- SLICE_PROTO, // 17 Slice
- LOCAL_PROTO, // 18
- OUTER_PROTO, // 19
- SUPER_PROTO, // 20
- GEN_PROTO, // 21
- THREAD_PROTO, // 22 Thread
- MUTEX_PROTO, // 23 Mutex
+ TIME_PROTO, // 8 Time
+ SEQ_PROTO, // 9 Sequence
+ BYTES_PROTO, // 10 Bytes
+ STRING_PROTO, // 11 String
+ TUPLE_PROTO, // 12 Tuple
+ LIST_PROTO, // 13 List
+ DICT_PROTO, // 14 Dict
+ FUNC_PROTO, // 15 Func
+ METHOD_PROTO, // 16 Method
+ HASH_PROTO, // 17 Hash
+ SLICE_PROTO, // 18 Slice
+ LOCAL_PROTO, // 19
+ OUTER_PROTO, // 20
+ SUPER_PROTO, // 21
+ GEN_PROTO, // 22
+ THREAD_PROTO, // 23 Thread
+ MUTEX_PROTO, // 24 Mutex
// Exceptions
- EXCEPTION, // 24 Exception
- INTERNAL_EXC, // 25 InternalError
- PARSEERROR_EXC, // 26
- INTERPRETER_EXC, // 27
- ASSERTION_EXC, // 28 AssertionError
- NAME_EXC, // 29 NameError
+ EXCEPTION, // 25 Exception
+ INTERNAL_EXC, // 26 InternalError
+ PARSEERROR_EXC, // 27
+ INTERPRETER_EXC, // 28
+ ASSERTION_EXC, // 29 AssertionError
+ NAME_EXC, // 30 NameError
INDEX_EXC, // IndexError
TYPE_EXC, // TypeError
VALUE_EXC, // ValueError
@@ -702,6 +703,10 @@
( ((str_obj)->data_type == DATA_TYPE_IMMDATA) ? (str_obj->data.str) : \
(((str_obj)->data_type == DATA_TYPE_DATAPTR) ? (((str_p)(str_obj->data.ptr))->str) : NULL ) )
+// NEW_TIME_OBJ: Create a new time object with a given initial time
+obj_p new_time_obj(isp ist, i64_t usecs);
+#define NEW_TIME(usecs) new_time_obj(ist, usecs)
+
// NEW_LIST_OBJ: Create a new list object with a given initial size
obj_p new_list_obj(isp ist, size_t initial_size);
#define NEW_LIST(initial_size) new_list_obj(ist, initial_size)
Modified: trunk/pr/test/test.pr
===================================================================
--- trunk/pr/test/test.pr 2004-06-16 02:31:01 UTC (rev 627)
+++ trunk/pr/test/test.pr 2004-06-16 05:19:21 UTC (rev 628)
@@ -17,16 +17,33 @@
files[2].open('r')
print files[2].read()
files[2].close()
-*/
File("abc/test").delete()
f = File("abc/test.txt")
-print f, f.exists?(), f.closed?()
+print f, f.exists?(), f.open?()
f.open('w')
-print f, f.exists?(), f.closed?()
+print f, f.exists?(), f.open?()
f.write(x"303132")
-print f, f.exists?(), f.closed?()
+print f, f.exists?(), f.open?()
f.close()
-print f, f.exists?(), f.closed?()
+print f, f.exists?(), f.open?()
+d = Dir()
+print d, d.modTime()
+print d, ' len:', len(d), d.path, d.name
+files = Dir().files()
+f = files[0]
+print f, f.modTime(), f.size()
+f.setModTime(Time(1080000000000000))
+print f, f.modTime(), f.size()
+f.setModTime(Time(1090000000000000))
+print f, f.modTime(), f.size()
+*/
+
+t = TempFile(delOnClose=False)
+print t, t.open?(), t.exists?()
+t.close()
+print t, t.open?(), t.exists?()
+t.delete()
+print t, t.open?(), t.exists?()
\ No newline at end of file
Modified: trunk/src/builtins-file.c
===================================================================
--- trunk/src/builtins-file.c 2004-06-16 02:31:01 UTC (rev 627)
+++ trunk/src/builtins-file.c 2004-06-16 05:19:21 UTC (rev 628)
@@ -484,21 +484,39 @@
return self;
}
+DEF(Dir, setModTime, FPARM1(time, OBJ(NONE))) {
+ apr_status_t aprerr;
+ char* path;
+ obj_p time_obj;
+ file_p filep;
+ BIN_CONTENT_CHK(Dir);
+ filep = self->data.ptr;
+ time_obj = parms[1];
+ if (time_obj == OBJ(NONE)) time_obj = new_time_obj(ist, apr_time_now());
+ CHECK_TYPE_EXC(time_obj, OBJ(TIME_PROTO), time);
+ path = pr2c_strptr(ist, get_attr(ist, self, sym(ist, "path")));
+ path[strlen(path)-1] = 0;
+ aprerr = apr_file_mtime_set(path, time_obj->data.i64, filep->pool);
+ IF_APR_ERR("setting directory modification time") return NULL;
+ return self;
+}
-/*
- * Set the mtime of the specified file.
- * @param fname The full path to the file (using / on all systems)
- * @param mtime The mtime to apply to the file.
- * @param pool The pool to use.
- * @warning Platforms which do not implement this feature will return
- * APR_ENOTIMPL.
+DEF(Dir, modTime, NULL) {
+ apr_finfo_t info;
+ char* path;
+ file_p filep;
+ BIN_CONTENT_CHK(Dir);
+ filep = self->data.ptr;
+ path = pr2c_strptr(ist, get_attr(ist, self, sym(ist, "path")));
+ apr_stat(&info, path, APR_FINFO_MTIME, filep->pool);
+ if ((info.valid & APR_FINFO_MTIME) == 0) {
+ raise_exception(ist, OBJ(IO_EXC), "unable to get directory modification time");
+ return NULL;
+ }
+ return new_time_obj(ist, info.mtime);
+}
-APR_DECLARE(apr_status_t) apr_file_mtime_set(const char *fname,
- apr_time_t mtime,
- apr_pool_t *pool);
- */
-
DEF(Dir, objList_, FORM_RPARAM) {
return parms[1];
}
@@ -724,6 +742,52 @@
return self;
}
+DEF(File, setModTime, FPARM1(time, OBJ(NONE))) {
+ apr_status_t aprerr;
+ char* path;
+ obj_p time_obj;
+ file_p filep;
+ BIN_CONTENT_CHK(File);
+ filep = self->data.ptr;
+ time_obj = parms[1];
+ if (time_obj == OBJ(NONE)) time_obj = new_time_obj(ist, apr_time_now());
+ CHECK_TYPE_EXC(time_obj, OBJ(TIME_PROTO), time);
+ path = pr2c_strptr(ist, get_attr(ist, self, sym(ist, "path")));
+ aprerr = apr_file_mtime_set(path, time_obj->data.i64, filep->pool);
+ IF_APR_ERR("setting file modification time") return NULL;
+ return self;
+}
+
+DEF(File, modTime, NULL) {
+ apr_finfo_t info;
+ char* path;
+ file_p filep;
+ BIN_CONTENT_CHK(File);
+ filep = self->data.ptr;
+ path = pr2c_strptr(ist, get_attr(ist, self, sym(ist, "path")));
+ apr_stat(&info, path, APR_FINFO_MTIME, filep->pool);
+ if ((info.valid & APR_FINFO_MTIME) == 0) {
+ raise_exception(ist, OBJ(IO_EXC), "unable to get file modification time");
+ return NULL;
+ }
+ return NEW_TIME(info.mtime);
+}
+
+DEF(File, size, NULL) {
+ apr_finfo_t info;
+ char* path;
+ file_p filep;
+ BIN_CONTENT_CHK(File);
+ filep = self->data.ptr;
+ path = pr2c_strptr(ist, get_attr(ist, self, sym(ist, "path")));
+ apr_stat(&info, path, APR_FINFO_SIZE, filep->pool);
+ if ((info.valid & APR_FINFO_SIZE) == 0) {
+ raise_exception(ist, OBJ(IO_EXC), "unable to get file size");
+ return NULL;
+ }
+ return NEW_INT(info.size);
+}
+
DEF(File, open, FPARM2( mode, NEW_STRING(""),
bufsize, NEW_INT(-1) ) ) {
apr_file_t *f;
@@ -799,6 +863,16 @@
return OBJ(NONE);
}
+DEF(File, open_QUES, NULL) {
+ BIN_CONTENT_CHK(File);
+ if (!has_proto(ist, self, File_OBJ)) {
+ raise_exception(ist, OBJ(TYPE_EXC), "closed can only be called on a File object");
+ return NULL;
+ }
+ if (OPEN(self)) return OBJ(PR_TRUE);
+ else return OBJ(PR_FALSE);
+}
+
DEF(File, closed_QUES, NULL) {
BIN_CONTENT_CHK(File);
if (!has_proto(ist, self, File_OBJ)) {
@@ -1069,6 +1143,63 @@
return parms[1];
}
+
+//*************************** MODULE TempFile *************************************
+
+MODULE_START(TempFile)
+{
+ TempFile_OBJ = NEW_OBJ(File_OBJ);
+ set_obj_doc(TempFile_OBJ, "TempFile object prototype");
+
+ MODULE_ADD_TO_BASE(TempFile);
+ TempFile_OBJ->unclonable = TRUE;
+}
+
+DEF(TempFile, init_, FPARM2( bufsize, NEW_INT(-1), delOnClose, OBJ(PR_TRUE))) {
+ apr_status_t aprerr;
+ apr_file_t *fp;
+ apr_pool_t *subpool;
+ char *temp_dir, *fullpath;
+ int bufsize, delOnClose = TRUE, flags = 0;
+
+ BIN_EMPTY_CHK();
+ INT_32_PARAM(1, bufsize);
+ if (parms[3] == OBJ(PR_FALSE)) delOnClose = FALSE;
+
+ aprerr = apr_pool_create(&subpool, get_pr_head_pool());
+ IF_APR_ERR("getting memory for TempFile") return NULL;
+
+ aprerr = apr_temp_dir_get(&temp_dir, subpool );
+ IF_APR_ERR("getting temp dir") return NULL;
+ aprerr = apr_filepath_merge( &fullpath, temp_dir, "prothon-temp-file-XXXXXX",
+ APR_FILEPATH_TRUENAME, subpool );
+ IF_APR_ERR("merging filepath") return NULL;
+
+ if (!delOnClose) flags = (APR_CREATE | APR_READ | APR_WRITE | APR_EXCL);
+ aprerr = apr_file_mktemp(&fp, fullpath, flags, subpool);
+ IF_APR_ERR("opening temporary file") return NULL;
+
+ read_unlock(ist, self);
+ SET_TYPE_IF_EXC(TempFile_OBJ, self, DATA_TYPE_DATAPTR) return NULL;
+ set_file_obj_data(ist, self, NEW_STRING(fullpath), NULL, fp, bufsize, flags);
+ read_lock(ist, self);
+ self->unclonable = TRUE;
+ return OBJ(NONE);
+}
+
+DEF(TempFile, str_, NULL) {
+ char buf[300], *path;
+ BIN_STR_CHK(TempFile);
+ path = pr2c_strptr(ist, get_attr(ist, self, sym(ist, "path")));
+ apr_snprintf(buf, sizeof(buf), "<TempFile:'%s'>", path);
+ pr_free(path);
+ return NEW_STRING(buf);
+}
+
+DEF(TempFile, move, FPARM1(dir, NULL)) {
+ raise_exception(ist, OBJ(IO_EXC), "attempt to move a temporary file");
+ return NULL;
+}
//*************************** MODULE TextFile *************************************
MODULE_START(TextFile)
@@ -1507,6 +1638,8 @@
MODULE_ADD_SYM(Dir, eq__QUES);
MODULE_ADD_SYM(Dir, setUPerms);
MODULE_ADD_SYM(Dir, setAttrs);
+ MODULE_ADD_SYM(Dir, setModTime);
+ MODULE_ADD_SYM(Dir, modTime);
MODULE_ADD_SYM(Dir, objList_);
MODULE_SUB_INIT(File);
@@ -1520,6 +1653,9 @@
MODULE_ADD_SYM(File, eq__QUES);
MODULE_ADD_SYM(File, setUPerms);
MODULE_ADD_SYM(File, setAttrs);
+ MODULE_ADD_SYM(File, setModTime);
+ MODULE_ADD_SYM(File, modTime);
+ MODULE_ADD_SYM(File, size);
MODULE_ADD_SYM(File, read);
MODULE_ADD_SYM(File, write);
MODULE_ADD_SYM(File, flush);
@@ -1527,10 +1663,16 @@
MODULE_ADD_SYM(File, truncate);
MODULE_ADD_SYM(File, open);
MODULE_ADD_SYM(File, close);
+ MODULE_ADD_SYM(File, open_QUES);
MODULE_ADD_SYM(File, closed_QUES);
MODULE_ADD_SYM(File, setStdOut);
MODULE_ADD_SYM(File, objList_);
+ MODULE_SUB_INIT(TempFile);
+ MODULE_ADD_SYM(TempFile, init_);
+ MODULE_ADD_SYM(TempFile, str_);
+ MODULE_ADD_SYM(TempFile, move);
+
MODULE_SUB_INIT(TextFile);
MODULE_ADD_SYM(TextFile, init_);
MODULE_ADD_SYM(TextFile, str_);
Modified: trunk/src/builtins-time.c
===================================================================
--- trunk/src/builtins-time.c 2004-06-16 02:31:01 UTC (rev 627)
+++ trunk/src/builtins-time.c 2004-06-16 05:19:21 UTC (rev 628)
@@ -77,7 +77,7 @@
MODULE_START(Time)
{
- Time_OBJ = NEW_OBJ(NULL);
+ Time_OBJ = OBJ(TIME_PROTO);
set_obj_doc(Time_OBJ, "Time object prototype");
set_obj_id(Time_OBJ, *, Time);
Modified: trunk/src/object.c
===================================================================
--- trunk/src/object.c 2004-06-16 02:31:01 UTC (rev 627)
+++ trunk/src/object.c 2004-06-16 05:19:21 UTC (rev 628)
@@ -215,6 +215,7 @@
OBJ(TUPLE_PROTO) = NEW_OBJ(OBJ(SEQ_PROTO));
OBJ(LIST_PROTO) = NEW_OBJ(OBJ(TUPLE_PROTO));
OBJ(SYMBOL_PROTO) = NEW_OBJ(NULL);
+ OBJ(TIME_PROTO) = NEW_OBJ(NULL);
OBJ(HASH_PROTO) = NEW_OBJ(NULL);
OBJ(SLICE_PROTO) = NEW_OBJ(NULL);