mirror of
				https://github.com/janet-lang/janet
				synced 2025-10-30 23:23:07 +00:00 
			
		
		
		
	Add file/tell
This commit is contained in:
		| @@ -348,11 +348,24 @@ JANET_CORE_FN(cfun_io_fseek, | |||||||
|     return argv[0]; |     return argv[0]; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | JANET_CORE_FN(cfun_io_ftell, | ||||||
|  |               "(file/tell f)", | ||||||
|  |               "Get the current value of the file position for file `f`.") { | ||||||
|  |     janet_fixarity(argc, 1); | ||||||
|  |     JanetFile *iof = janet_getabstract(argv, 0, &janet_file_type); | ||||||
|  |     if (iof->flags & JANET_FILE_CLOSED) | ||||||
|  |         janet_panic("file is closed"); | ||||||
|  |     long pos = ftell(iof->file); | ||||||
|  |     if (pos == -1) janet_panic("error getting position in file"); | ||||||
|  |     return janet_wrap_number((double)pos); | ||||||
|  | } | ||||||
|  |  | ||||||
| static JanetMethod io_file_methods[] = { | static JanetMethod io_file_methods[] = { | ||||||
|     {"close", cfun_io_fclose}, |     {"close", cfun_io_fclose}, | ||||||
|     {"flush", cfun_io_fflush}, |     {"flush", cfun_io_fflush}, | ||||||
|     {"read", cfun_io_fread}, |     {"read", cfun_io_fread}, | ||||||
|     {"seek", cfun_io_fseek}, |     {"seek", cfun_io_fseek}, | ||||||
|  |     {"tell", cfun_io_ftell}, | ||||||
|     {"write", cfun_io_fwrite}, |     {"write", cfun_io_fwrite}, | ||||||
|     {NULL, NULL} |     {NULL, NULL} | ||||||
| }; | }; | ||||||
| @@ -783,6 +796,7 @@ void janet_lib_io(JanetTable *env) { | |||||||
|         JANET_CORE_REG("file/write", cfun_io_fwrite), |         JANET_CORE_REG("file/write", cfun_io_fwrite), | ||||||
|         JANET_CORE_REG("file/flush", cfun_io_fflush), |         JANET_CORE_REG("file/flush", cfun_io_fflush), | ||||||
|         JANET_CORE_REG("file/seek", cfun_io_fseek), |         JANET_CORE_REG("file/seek", cfun_io_fseek), | ||||||
|  |         JANET_CORE_REG("file/tell", cfun_io_ftell), | ||||||
|         JANET_REG_END |         JANET_REG_END | ||||||
|     }; |     }; | ||||||
|     janet_core_cfuns_ext(env, NULL, io_cfuns); |     janet_core_cfuns_ext(env, NULL, io_cfuns); | ||||||
|   | |||||||
| @@ -294,9 +294,12 @@ | |||||||
| (assert-error "comptime issue" (eval '(comptime (error "oops")))) | (assert-error "comptime issue" (eval '(comptime (error "oops")))) | ||||||
|  |  | ||||||
| (with [f (file/temp)] | (with [f (file/temp)] | ||||||
|  |   (assert (= 0 (file/tell f)) "start of file") | ||||||
|   (file/write f "foo\n") |   (file/write f "foo\n") | ||||||
|  |   (assert (= 4 (file/tell f)) "after written string") | ||||||
|   (file/flush f) |   (file/flush f) | ||||||
|   (file/seek f :set 0) |   (file/seek f :set 0) | ||||||
|  |   (assert (= 0 (file/tell f)) "start of file again") | ||||||
|   (assert (= (string (file/read f :all)) "foo\n") "temp files work")) |   (assert (= (string (file/read f :all)) "foo\n") "temp files work")) | ||||||
|  |  | ||||||
| (var counter 0) | (var counter 0) | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 sogaiu
					sogaiu