os.File clean up + socket.v (dial())
parent
386367c3d5
commit
61b51f6149
|
@ -68,7 +68,7 @@ fn (g mut CGen) gen(s string) {
|
|||
|
||||
fn (g mut CGen) save() {
|
||||
s := g.lines.join('\n')
|
||||
g.out.appendln(s)
|
||||
g.out.writeln(s)
|
||||
g.out.close()
|
||||
}
|
||||
|
||||
|
|
|
@ -823,7 +823,6 @@ fn new_v(args []string) *V {
|
|||
files << f
|
||||
}
|
||||
}
|
||||
obfuscate := args.contains('-obf')
|
||||
return &V {
|
||||
os: _os
|
||||
out_name: out_name
|
||||
|
@ -839,7 +838,7 @@ fn new_v(args []string) *V {
|
|||
is_play: args.contains('play')
|
||||
is_prod: args.contains('-prod')
|
||||
is_verbose: args.contains('-verbose')
|
||||
obfuscate: obfuscate
|
||||
obfuscate: args.contains('-obf')
|
||||
is_prof: args.contains('-prof')
|
||||
is_live: args.contains('-live')
|
||||
sanitize: args.contains('-sanitize')
|
||||
|
|
|
@ -236,7 +236,7 @@ fn (p mut Parser) parse() {
|
|||
}
|
||||
if true && !p.first_run() && p.fileis('test') {
|
||||
out := os.create('/var/tmp/fmt.v')
|
||||
out.appendln(p.scanner.fmt_out.str())
|
||||
out.writeln(p.scanner.fmt_out.str())
|
||||
out.close()
|
||||
}
|
||||
return
|
||||
|
|
|
@ -0,0 +1,78 @@
|
|||
module net
|
||||
|
||||
#include <netdb.h>
|
||||
#include <sys/types.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/socket.h>
|
||||
#include <errno.h>
|
||||
|
||||
struct Conn {
|
||||
pub:
|
||||
sockfd int
|
||||
}
|
||||
|
||||
struct Listener {
|
||||
listener int
|
||||
their_addr voidptr
|
||||
}
|
||||
|
||||
import const (
|
||||
AF_UNSPEC
|
||||
SOCK_STREAM
|
||||
AI_PASSIVE
|
||||
)
|
||||
|
||||
struct C.addrinfo {
|
||||
mut:
|
||||
ai_family int
|
||||
ai_socktype int
|
||||
ai_flags int
|
||||
ai_protocol int
|
||||
ai_addrlen int
|
||||
ai_next voidptr
|
||||
ai_addr voidptr
|
||||
}
|
||||
|
||||
fn dial(addr string, port int) Conn {
|
||||
println('net.dial("$addr":$port)')
|
||||
mut hints := C.addrinfo{}
|
||||
servinfo := &C.addrinfo{!}
|
||||
mut rp := &C.addrinfo{!}
|
||||
mut sockfd := -1
|
||||
// allow IPv4 or IPv6
|
||||
hints.ai_family = AF_UNSPEC
|
||||
// avoid name lookup for port
|
||||
// hints.ai_flags = AI_NUMERICSERV
|
||||
hints.ai_socktype = SOCK_STREAM
|
||||
hints.ai_flags = AI_PASSIVE
|
||||
strport := '$port'
|
||||
connbad := Conn{}
|
||||
c_addr := addr.cstr()
|
||||
rv := C.getaddrinfo(c_addr, strport.cstr(), &hints, &servinfo)
|
||||
if rv != 0 {
|
||||
println('Getaddrinfo failed ')
|
||||
return connbad
|
||||
}
|
||||
// Loop through all the results and connect to the first we can
|
||||
for rp = servinfo; !isnil(rp); rp = rp.ai_next {
|
||||
sockfd = C.socket(rp.ai_family, rp.ai_socktype, rp.ai_protocol)
|
||||
if sockfd == -1 {
|
||||
continue
|
||||
}
|
||||
if C.connect(sockfd, rp.ai_addr, rp.ai_addrlen) {
|
||||
C.close(sockfd)
|
||||
continue
|
||||
}
|
||||
break
|
||||
}
|
||||
if sockfd == -1 {
|
||||
println('socket: Cannot connect to host')
|
||||
return connbad
|
||||
}
|
||||
C.freeaddrinfo(servinfo)
|
||||
conn := Conn {
|
||||
sockfd: sockfd,
|
||||
}
|
||||
return conn
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
import net
|
||||
|
||||
fn test_dial() {
|
||||
|
||||
conn := net.dial('irc.freenode.org', 6667)
|
||||
println(conn.sockfd)
|
||||
|
||||
}
|
10
vlib/os/os.v
10
vlib/os/os.v
|
@ -188,7 +188,7 @@ fn create_file2(file, mode string) File {
|
|||
return res
|
||||
}
|
||||
|
||||
fn (f File) append(s string) {
|
||||
pub fn (f File) write(s string) {
|
||||
ss := s.clone()
|
||||
C.fputs(ss.cstr(), f.cfile)
|
||||
// ss.free()
|
||||
|
@ -208,7 +208,7 @@ fn (f File) write_bytes_at(data voidptr, size, pos int) {
|
|||
C.fseek(f.cfile, 0, SEEK_END)
|
||||
}
|
||||
|
||||
pub fn (f File) appendln(s string) {
|
||||
pub fn (f File) writeln(s string) {
|
||||
// C.fwrite(s.str, 1, s.len, f.cfile)
|
||||
// ss := s.clone()
|
||||
// TODO perf
|
||||
|
@ -446,7 +446,7 @@ pub fn home_dir() string {
|
|||
// write_file writes text data to a file in `path`.
|
||||
pub fn write_file(path, text string) {
|
||||
f := os.create(path)
|
||||
f.appendln(text)
|
||||
f.writeln(text)
|
||||
f.close()
|
||||
}
|
||||
|
||||
|
@ -505,7 +505,7 @@ pub fn is_dir(path string) bool {
|
|||
}
|
||||
}
|
||||
|
||||
fn chdir(path string) {
|
||||
pub fn chdir(path string) {
|
||||
$if windows {
|
||||
C._chdir(path.cstr())
|
||||
}
|
||||
|
@ -555,7 +555,7 @@ pub fn ls(path string) []string {
|
|||
fn log(s string) {
|
||||
}
|
||||
|
||||
fn print_backtrace() {
|
||||
pub fn print_backtrace() {
|
||||
/*
|
||||
# void *buffer[100];
|
||||
nptrs := 0
|
||||
|
|
Loading…
Reference in New Issue