il linguaggio Lua: settima parte

segue dalla sesta parte Iteratori e lua funzionale Cos’è un iteratore? Informaticamente parlando, è un costrutto che ci permette di scorrere strutture dati come liste, array, elenchi. In pratica, dato un elemento della struttura il compito dell’iteratore è farci avere il prossimo su cui operare. Non ci stupirà apprendere che in Lua gli iteratori sono funzioni. Vediamo un semplice esempio: function reverse_iter(t) local i=#t+1 return function() i=i-1 if i>=0 then return t[i] end end end...

March 3, 2018 · Andrea Manzini

a simple PNG decoder in Go

while working with image files, I needed a simple way to analyze content of a picture; so I wrote this tool that “walks” inside a PNG file and reports all the chunks seen; this is intended to be expanded with more features in a future. package main import ( "encoding/binary" "fmt" "io" "os" ) type chunk struct { Length uint32 ChunkType [4]byte } func main() { if len(os.Args) != 2 { fmt.Printf("Usage: %s filename.png\n", os.Args[0]) os.Exit(1) } f, err := os.Open(os.Args[1]) if err != nil { panic(err) } defer f.Close() header := make([]byte, 8) _, err = f.Read(header) fmt.Printf("header: %v\n", header) if err != nil { panic(err) } var data chunk var offset int64 offset = 8 for { err = binary.Read(f, binary.BigEndian, &data) if err != nil { if err == io.EOF { break } panic(err) } fmt.Printf("Offset: %d chunk len=%d, type: %s\n", offset, data.Length, string(data.ChunkType[:4])) f.Seek(int64(data.Length+4), io.SeekCurrent) offset += int64(data.Length) + 4 } }...

January 28, 2018 · Andrea Manzini

il linguaggio Lua: sesta parte

segue dalla quinta parte Quando il saggio indica la luna, lo sciocco guarda il dito Nello scorse puntate abbiamo appreso le basi di un linguaggio minimalista, il cui motto è “doing more with less”, che occupa meno byte della vostra foto su Facebook e che i benchmark dichiarano il più veloce tra i linguaggi di scripting. Nato da menti brasiliane, l’hanno chiamato Lua, che vuol dire Luna in portoghese. Lua viene usato come linguaggio di scripting in Angry Birds, World of Warcraft e decine di altri videogame e software: nello scorso post abbiamo visto come creare un semplice plugin per VLC. In questo invece approfondiremo le peculiarità del linguaggio e cominceremo a guardare oltre, presentando l’ecosistema di librerie; infine nel prossimo sperimenteremo l’integrazione tra Lua e C. ...

December 22, 2017 · Andrea Manzini

linux: how to access DHCP options from client

As you may know, you can configure any DHCP server to send many options to the clients; for example to setup dns domains, http proxy (WPAD) and so on. If you need to access these options from a linux client, you must configure the client to ASK the server for the new options, by editing /etc/dhcp/dhclient.conf, and add an entry like: option WPAD code 252 = string; also request WPAD;...

November 6, 2017 · Andrea Manzini