os: minor fixes
							parent
							
								
									edabd57e8a
								
							
						
					
					
						commit
						8bf290acf0
					
				
							
								
								
									
										110
									
								
								vlib/os/os.v
								
								
								
								
							
							
						
						
									
										110
									
								
								vlib/os/os.v
								
								
								
								
							|  | @ -77,6 +77,58 @@ pub fn (f File) is_opened() bool { | ||||||
| 	return f.opened | 	return f.opened | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | /***************************** Write ops ****************************/ | ||||||
|  | 
 | ||||||
|  | pub fn (mut f File) write(s string) { | ||||||
|  | 	if !f.opened { | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  | 	/* | ||||||
|  | 	$if linux { | ||||||
|  | 		$if !android { | ||||||
|  | 			C.syscall(sys_write, f.fd, s.str, s.len) | ||||||
|  | 			return | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	*/ | ||||||
|  | 	C.fputs(s.str, f.cfile) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | pub fn (mut f File) writeln(s string) { | ||||||
|  | 	if !f.opened { | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  | 	/* | ||||||
|  | 	$if linux { | ||||||
|  | 		$if !android { | ||||||
|  | 			snl := s + '\n' | ||||||
|  | 			C.syscall(sys_write, f.fd, snl.str, snl.len) | ||||||
|  | 			return | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | 	*/ | ||||||
|  | 	// TODO perf
 | ||||||
|  | 	C.fputs(s.str, f.cfile) | ||||||
|  | 	C.fputs('\n', f.cfile) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | pub fn (mut f File) write_bytes(data voidptr, size int) { | ||||||
|  | 	C.fwrite(data, 1, size, f.cfile) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | pub fn (mut f File) write_bytes_at(data voidptr, size, pos int) { | ||||||
|  | 	//$if linux {
 | ||||||
|  | 	//}
 | ||||||
|  | 	//$else {
 | ||||||
|  | 	C.fseek(f.cfile, pos, C.SEEK_SET) | ||||||
|  | 	C.fwrite(data, 1, size, f.cfile) | ||||||
|  | 	C.fseek(f.cfile, 0, C.SEEK_END) | ||||||
|  | 	//}
 | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | /***************************** Read ops  ****************************/ | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| // read_bytes reads an amount of bytes from the beginning of the file
 | // read_bytes reads an amount of bytes from the beginning of the file
 | ||||||
| pub fn (f &File) read_bytes(size int) []byte { | pub fn (f &File) read_bytes(size int) []byte { | ||||||
| 	return f.read_bytes_at(size, 0) | 	return f.read_bytes_at(size, 0) | ||||||
|  | @ -106,6 +158,7 @@ pub fn read_bytes(path string) ?[]byte { | ||||||
| 	return res[0..nr_read_elements * fsize] | 	return res[0..nr_read_elements * fsize] | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
| // read_file reads the file in `path` and returns the contents.
 | // read_file reads the file in `path` and returns the contents.
 | ||||||
| pub fn read_file(path string) ?string { | pub fn read_file(path string) ?string { | ||||||
| 	mode := 'rb' | 	mode := 'rb' | ||||||
|  | @ -125,6 +178,15 @@ pub fn read_file(path string) ?string { | ||||||
| 	return string(str,fsize) | 	return string(str,fsize) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | /***************************** Utility  ops ************************/ | ||||||
|  | pub fn (mut f File) flush() { | ||||||
|  | 	if !f.opened { | ||||||
|  | 		return | ||||||
|  | 	} | ||||||
|  | 	C.fflush(f.cfile) | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | /***************************** OS ops ************************/ | ||||||
| // file_size returns the size of the file located in `path`.
 | // file_size returns the size of the file located in `path`.
 | ||||||
| pub fn file_size(path string) int { | pub fn file_size(path string) int { | ||||||
| 	mut s := C.stat{} | 	mut s := C.stat{} | ||||||
|  | @ -337,22 +399,7 @@ pub fn open_file(path string, mode string, options ...int) ?File { | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| pub fn (mut f File) write_bytes_at(data voidptr, size, pos int) { |  | ||||||
| 	//$if linux {
 |  | ||||||
| 	//}
 |  | ||||||
| 	//$else {
 |  | ||||||
| 	C.fseek(f.cfile, pos, C.SEEK_SET) |  | ||||||
| 	C.fwrite(data, 1, size, f.cfile) |  | ||||||
| 	C.fseek(f.cfile, 0, C.SEEK_END) |  | ||||||
| 	//}
 |  | ||||||
| } |  | ||||||
| 
 | 
 | ||||||
| pub fn (mut f File) flush() { |  | ||||||
| 	if !f.opened { |  | ||||||
| 		return |  | ||||||
| 	} |  | ||||||
| 	C.fflush(f.cfile) |  | ||||||
| } |  | ||||||
| 
 | 
 | ||||||
| // system starts the specified command, waits for it to complete, and returns its code.
 | // system starts the specified command, waits for it to complete, and returns its code.
 | ||||||
| fn vpopen(path string) voidptr { | fn vpopen(path string) voidptr { | ||||||
|  | @ -1324,36 +1371,3 @@ pub fn create(path string) ?File { | ||||||
| 		opened: true | 		opened: true | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 |  | ||||||
| pub fn (mut f File) write(s string) { |  | ||||||
| 	if !f.opened { |  | ||||||
| 		return |  | ||||||
| 	} |  | ||||||
|   /* |  | ||||||
| 	$if linux { |  | ||||||
| 		$if !android { |  | ||||||
| 			C.syscall(sys_write, f.fd, s.str, s.len) |  | ||||||
| 			return |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
|   */ |  | ||||||
| 	C.fputs(s.str, f.cfile) |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| pub fn (mut f File) writeln(s string) { |  | ||||||
| 	if !f.opened { |  | ||||||
| 		return |  | ||||||
| 	} |  | ||||||
|   /* |  | ||||||
| 	$if linux { |  | ||||||
| 		$if !android { |  | ||||||
| 			snl := s + '\n' |  | ||||||
| 			C.syscall(sys_write, f.fd, snl.str, snl.len) |  | ||||||
| 			return |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
|   */ |  | ||||||
| 	// TODO perf
 |  | ||||||
| 	C.fputs(s.str, f.cfile) |  | ||||||
| 	C.fputs('\n', f.cfile) |  | ||||||
| } |  | ||||||
|  |  | ||||||
|  | @ -137,21 +137,6 @@ pub fn get_error_msg(code int) string { | ||||||
| 	return posix_get_error_msg(code) | 	return posix_get_error_msg(code) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // convert any value to []byte (LittleEndian) and write it
 |  | ||||||
| // for example if we have write(7, 4), "07 00 00 00" gets written
 |  | ||||||
| // write(0x1234, 2) => "34 12"
 |  | ||||||
| pub fn (mut f File) write_bytes(data voidptr, size int) { |  | ||||||
| /* |  | ||||||
| 	$if linux { |  | ||||||
| 		$if !android { |  | ||||||
| 			C.syscall(sys_write, f.fd, data, 1) |  | ||||||
| 			return |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| */ |  | ||||||
| 	C.fwrite(data, 1, size, f.cfile) |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| pub fn (mut f File) close() { | pub fn (mut f File) close() { | ||||||
| 	if !f.opened { | 	if !f.opened { | ||||||
| 		return | 		return | ||||||
|  |  | ||||||
|  | @ -302,10 +302,6 @@ pub fn symlink(origin, target string) ?bool { | ||||||
| 	return error(get_error_msg(int(C.GetLastError()))) | 	return error(get_error_msg(int(C.GetLastError()))) | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| pub fn (mut f File) write_bytes(data voidptr, size int) { |  | ||||||
| 	C.fwrite(data, 1, size, f.cfile) |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| pub fn (mut f File) close() { | pub fn (mut f File) close() { | ||||||
| 	if !f.opened { | 	if !f.opened { | ||||||
| 		return | 		return | ||||||
|  |  | ||||||
|  | @ -1,7 +1,8 @@ | ||||||
| fn test_pointer_arithmetic() { | fn test_pointer_arithmetic() { | ||||||
| 	arr := [1, 2, 3, 4] | 	arr := [1, 2, 3, 4] | ||||||
| 	unsafe { | 	unsafe { | ||||||
| 		mut parr := *int(arr.data) | 		mut parr := &int(arr.data) | ||||||
|  | 		assert 1 == *parr | ||||||
| 		parr++ | 		parr++ | ||||||
| 		assert 2 == *parr | 		assert 2 == *parr | ||||||
| 		parr++ | 		parr++ | ||||||
|  | @ -10,6 +11,7 @@ fn test_pointer_arithmetic() { | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | /* | ||||||
| fn test_multi_level_pointer_dereferencing() { | fn test_multi_level_pointer_dereferencing() { | ||||||
| 	n := 100 | 	n := 100 | ||||||
| 	pn := &n | 	pn := &n | ||||||
|  | @ -22,3 +24,4 @@ fn test_multi_level_pointer_dereferencing() { | ||||||
| 	} | 	} | ||||||
| 	assert n == 300	// updated by the unsafe pointer manipulation
 | 	assert n == 300	// updated by the unsafe pointer manipulation
 | ||||||
| } | } | ||||||
|  | */ | ||||||
		Loading…
	
		Reference in New Issue