<commit_before>package gost

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/tls"
	"crypto/x509"
	"crypto/x509/pkix"
	"encoding/pem"
	"math/big"
	"time"

	"github.com/go-log/log"
)

// Version is the gost version.
const Version = "2.4-rc2"

// Debug is a flag that enables the debug log.
var Debug bool

var (
	tinyBufferSize   = 128
	smallBufferSize  = 1 * 1024  // 1KB small buffer
	mediumBufferSize = 8 * 1024  // 8KB medium buffer
	largeBufferSize  = 32 * 1024 // 32KB large buffer
)

var (
	// KeepAliveTime is the keep alive time period for TCP connection.
	KeepAliveTime = 180 * time.Second
	// DialTimeout is the timeout of dial.
	DialTimeout = 30 * time.Second
	// ReadTimeout is the timeout for reading.
	ReadTimeout = 30 * time.Second
	// WriteTimeout is the timeout for writing.
	WriteTimeout = 60 * time.Second
	// PingTimeout is the timeout for pinging.
	PingTimeout = 30 * time.Second
	// PingRetries is the reties of ping.
	PingRetries = 3
	// default udp node TTL in second for udp port forwarding.
	defaultTTL = 60 * time.Second
)

var (
	// DefaultTLSConfig is a default TLS config for internal use
	DefaultTLSConfig *tls.Config

	// DefaultUserAgent is the default HTTP User-Agent header used by HTTP and websocket
	DefaultUserAgent = "Chrome/60.0.3112.90"
)

func init() {
	rawCert, rawKey, err := generateKeyPair()
	if err != nil {
		panic(err)
	}
	cert, err := tls.X509KeyPair(rawCert, rawKey)
	if err != nil {
		panic(err)
	}
	DefaultTLSConfig = &tls.Config{
		Certificates: []tls.Certificate{cert},
	}

	// log.DefaultLogger = &LogLogger{}
}

// SetLogger sets a new logger for internal log system
func SetLogger(logger log.Logger) {
	log.DefaultLogger = logger
}

func generateKeyPair() (rawCert, rawKey []byte, err error) {
	// Create private key and self-signed certificate
	// Adapted from https://golang.org/src/crypto/tls/generate_cert.go

	priv, err := rsa.GenerateKey(rand.Reader, 2048)
	if err != nil {
		return
	}
	validFor := time.Hour * 24 * 365 * 10 // ten years
	notBefore := time.Now()
	notAfter := notBefore.Add(validFor)
	serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
	serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
	template := x509.Certificate{
		SerialNumber: serialNumber,
		Subject: pkix.Name{
			Organization: []string{"gost"},
		},
		NotBefore: notBefore,
		NotAfter:  notAfter,

		KeyUsage:              x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
		ExtKeyUsage:           []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
		BasicConstraintsValid: true,
	}
	derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
	if err != nil {
		return
	}

	rawCert = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
	rawKey = pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)})

	return
}
<commit_msg>version 2.5-dev<commit_after>package gost

import (
	"crypto/rand"
	"crypto/rsa"
	"crypto/tls"
	"crypto/x509"
	"crypto/x509/pkix"
	"encoding/pem"
	"math/big"
	"time"

	"github.com/go-log/log"
)

// Version is the gost version.
const Version = "2.5-dev"

// Debug is a flag that enables the debug log.
var Debug bool

var (
	tinyBufferSize   = 128
	smallBufferSize  = 1 * 1024  // 1KB small buffer
	mediumBufferSize = 8 * 1024  // 8KB medium buffer
	largeBufferSize  = 32 * 1024 // 32KB large buffer
)

var (
	// KeepAliveTime is the keep alive time period for TCP connection.
	KeepAliveTime = 180 * time.Second
	// DialTimeout is the timeout of dial.
	DialTimeout = 30 * time.Second
	// ReadTimeout is the timeout for reading.
	ReadTimeout = 30 * time.Second
	// WriteTimeout is the timeout for writing.
	WriteTimeout = 60 * time.Second
	// PingTimeout is the timeout for pinging.
	PingTimeout = 30 * time.Second
	// PingRetries is the reties of ping.
	PingRetries = 3
	// default udp node TTL in second for udp port forwarding.
	defaultTTL = 60 * time.Second
)

var (
	// DefaultTLSConfig is a default TLS config for internal use
	DefaultTLSConfig *tls.Config

	// DefaultUserAgent is the default HTTP User-Agent header used by HTTP and websocket
	DefaultUserAgent = "Chrome/60.0.3112.90"
)

func init() {
	rawCert, rawKey, err := generateKeyPair()
	if err != nil {
		panic(err)
	}
	cert, err := tls.X509KeyPair(rawCert, rawKey)
	if err != nil {
		panic(err)
	}
	DefaultTLSConfig = &tls.Config{
		Certificates: []tls.Certificate{cert},
	}

	// log.DefaultLogger = &LogLogger{}
}

// SetLogger sets a new logger for internal log system
func SetLogger(logger log.Logger) {
	log.DefaultLogger = logger
}

func generateKeyPair() (rawCert, rawKey []byte, err error) {
	// Create private key and self-signed certificate
	// Adapted from https://golang.org/src/crypto/tls/generate_cert.go

	priv, err := rsa.GenerateKey(rand.Reader, 2048)
	if err != nil {
		return
	}
	validFor := time.Hour * 24 * 365 * 10 // ten years
	notBefore := time.Now()
	notAfter := notBefore.Add(validFor)
	serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128)
	serialNumber, err := rand.Int(rand.Reader, serialNumberLimit)
	template := x509.Certificate{
		SerialNumber: serialNumber,
		Subject: pkix.Name{
			Organization: []string{"gost"},
		},
		NotBefore: notBefore,
		NotAfter:  notAfter,

		KeyUsage:              x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
		ExtKeyUsage:           []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
		BasicConstraintsValid: true,
	}
	derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &priv.PublicKey, priv)
	if err != nil {
		return
	}

	rawCert = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes})
	rawKey = pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(priv)})

	return
}
<|endoftext|>


<commit_before>package main

import (
	"github.com/bcicen/ctop/config"
	"github.com/bcicen/ctop/cwidgets/single"
	ui "github.com/gizak/termui"
)

func ShowConnError(err error) (exit bool) {
	ui.Clear()
	ui.DefaultEvtStream.ResetHandlers()
	defer ui.DefaultEvtStream.ResetHandlers()

	setErr := func(err error) {
		errView.Append(err.Error())
		errView.Append("attempting to reconnect...")
		ui.Render(errView)
	}

	HandleKeys("exit", func() {
		exit = true
		ui.StopLoop()
	})

	ui.Handle("/timer/2s", func(ui.Event) {
		_, err := cursor.RefreshContainers()
		if err == nil {
			ui.StopLoop()
			return
		}
		setErr(err)
	})

	ui.Handle("/sys/wnd/resize", func(e ui.Event) {
		errView.Resize()
		ui.Clear()
		ui.Render(errView)
		log.Infof("RESIZE")
	})

	errView.Resize()
	setErr(err)
	ui.Loop()
	return exit
}

func RedrawRows(clr bool) {
	// reinit body rows
	cGrid.Clear()

	// build layout
	y := 1
	if config.GetSwitchVal("enableHeader") {
		header.SetCount(cursor.Len())
		header.SetFilter(config.GetVal("filterStr"))
		y += header.Height()
	}

	cGrid.SetY(y)

	for _, c := range cursor.filtered {
		cGrid.AddRows(c.Widgets)
	}

	if clr {
		ui.Clear()
		log.Debugf("screen cleared")
	}
	if config.GetSwitchVal("enableHeader") {
		ui.Render(header)
	}
	cGrid.Align()
	ui.Render(cGrid)
}

func SingleView() MenuFn {
	c := cursor.Selected()
	if c == nil {
		return nil
	}

	ui.Clear()
	ui.DefaultEvtStream.ResetHandlers()
	defer ui.DefaultEvtStream.ResetHandlers()

	ex := single.NewSingle(c.Id)
	c.SetUpdater(ex)

	ex.Align()
	ui.Render(ex)

	HandleKeys("up", ex.Up)
	HandleKeys("down", ex.Down)
	ui.Handle("/sys/kbd/", func(ui.Event) { ui.StopLoop() })

	ui.Handle("/timer/1s", func(ui.Event) { ui.Render(ex) })
	ui.Handle("/sys/wnd/resize", func(e ui.Event) {
		ex.SetWidth(ui.TermWidth())
		ex.Align()
		log.Infof("resize: width=%v max-rows=%v", ex.Width, cGrid.MaxRows())
	})

	ui.Loop()
	c.SetUpdater(c.Widgets)
	return nil
}

func RefreshDisplay() error {
	// skip display refresh during scroll
	if !cursor.isScrolling {
		needsClear, err := cursor.RefreshContainers()
		if err != nil {
			return err
		}
		RedrawRows(needsClear)
	}
	return nil
}

func Display() bool {
	var menu MenuFn
	var connErr error

	cGrid.SetWidth(ui.TermWidth())
	ui.DefaultEvtStream.Hook(logEvent)

	// initial draw
	header.Align()
	status.Align()
	cursor.RefreshContainers()
	RedrawRows(true)

	HandleKeys("up", cursor.Up)
	HandleKeys("down", cursor.Down)

	HandleKeys("pgup", cursor.PgUp)
	HandleKeys("pgdown", cursor.PgDown)

	HandleKeys("exit", ui.StopLoop)
	HandleKeys("help", func() {
		menu = HelpMenu
		ui.StopLoop()
	})

	ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
		menu = ContainerMenu
		ui.StopLoop()
	})
	ui.Handle("/sys/kbd/<left>", func(ui.Event) {
		menu = LogMenu
		ui.StopLoop()
	})
	ui.Handle("/sys/kbd/<right>", func(ui.Event) {
		menu = SingleView
		ui.StopLoop()
	})
	ui.Handle("/sys/kbd/l", func(ui.Event) {
		menu = LogMenu
		ui.StopLoop()
	})
	ui.Handle("/sys/kbd/e", func(ui.Event) {
		menu = ExecShell
		ui.StopLoop()
	})
	ui.Handle("/sys/kbd/o", func(ui.Event) {
		menu = SingleView
		ui.StopLoop()
	})
	ui.Handle("/sys/kbd/a", func(ui.Event) {
		config.Toggle("allContainers")
		connErr = RefreshDisplay()
		if connErr != nil {
			ui.StopLoop()
		}
	})
	ui.Handle("/sys/kbd/D", func(ui.Event) {
		dumpContainer(cursor.Selected())
	})
	ui.Handle("/sys/kbd/f", func(ui.Event) {
		menu = FilterMenu
		ui.StopLoop()
	})
	ui.Handle("/sys/kbd/H", func(ui.Event) {
		config.Toggle("enableHeader")
		RedrawRows(true)
	})
	ui.Handle("/sys/kbd/r", func(e ui.Event) {
		config.Toggle("sortReversed")
	})
	ui.Handle("/sys/kbd/s", func(ui.Event) {
		menu = SortMenu
		ui.StopLoop()
	})
	ui.Handle("/sys/kbd/S", func(ui.Event) {
		path, err := config.Write()
		if err == nil {
			log.Statusf("wrote config to %s", path)
		} else {
			log.StatusErr(err)
		}
		ui.StopLoop()
	})

	ui.Handle("/timer/1s", func(e ui.Event) {
		if log.StatusQueued() {
			ui.StopLoop()
		}
		connErr = RefreshDisplay()
		if connErr != nil {
			ui.StopLoop()
		}
	})

	ui.Handle("/sys/wnd/resize", func(e ui.Event) {
		header.Align()
		status.Align()
		cursor.ScrollPage()
		cGrid.SetWidth(ui.TermWidth())
		log.Infof("resize: width=%v max-rows=%v", cGrid.Width, cGrid.MaxRows())
		RedrawRows(true)
	})

	ui.Loop()

	if connErr != nil {
		return ShowConnError(connErr)
	}

	if log.StatusQueued() {
		for sm := range log.FlushStatus() {
			if sm.IsError {
				status.ShowErr(sm.Text)
			} else {
				status.Show(sm.Text)
			}
		}
		return false
	}

	if menu != nil {
		for menu != nil {
			menu = menu()
		}
		return false
	}

	return true
}
<commit_msg>fix timer<commit_after>package main

import (
	"github.com/bcicen/ctop/config"
	"github.com/bcicen/ctop/cwidgets/single"
	ui "github.com/gizak/termui"
)

func ShowConnError(err error) (exit bool) {
	ui.Clear()
	ui.DefaultEvtStream.ResetHandlers()
	defer ui.DefaultEvtStream.ResetHandlers()

	setErr := func(err error) {
		errView.Append(err.Error())
		errView.Append("attempting to reconnect...")
		ui.Render(errView)
	}

	HandleKeys("exit", func() {
		exit = true
		ui.StopLoop()
	})

	ui.Handle("/timer/1s", func(ui.Event) {
		_, err := cursor.RefreshContainers()
		if err == nil {
			ui.StopLoop()
			return
		}
		setErr(err)
	})

	ui.Handle("/sys/wnd/resize", func(e ui.Event) {
		errView.Resize()
		ui.Clear()
		ui.Render(errView)
		log.Infof("RESIZE")
	})

	errView.Resize()
	setErr(err)
	ui.Loop()
	return exit
}

func RedrawRows(clr bool) {
	// reinit body rows
	cGrid.Clear()

	// build layout
	y := 1
	if config.GetSwitchVal("enableHeader") {
		header.SetCount(cursor.Len())
		header.SetFilter(config.GetVal("filterStr"))
		y += header.Height()
	}

	cGrid.SetY(y)

	for _, c := range cursor.filtered {
		cGrid.AddRows(c.Widgets)
	}

	if clr {
		ui.Clear()
		log.Debugf("screen cleared")
	}
	if config.GetSwitchVal("enableHeader") {
		ui.Render(header)
	}
	cGrid.Align()
	ui.Render(cGrid)
}

func SingleView() MenuFn {
	c := cursor.Selected()
	if c == nil {
		return nil
	}

	ui.Clear()
	ui.DefaultEvtStream.ResetHandlers()
	defer ui.DefaultEvtStream.ResetHandlers()

	ex := single.NewSingle(c.Id)
	c.SetUpdater(ex)

	ex.Align()
	ui.Render(ex)

	HandleKeys("up", ex.Up)
	HandleKeys("down", ex.Down)
	ui.Handle("/sys/kbd/", func(ui.Event) { ui.StopLoop() })

	ui.Handle("/timer/1s", func(ui.Event) { ui.Render(ex) })
	ui.Handle("/sys/wnd/resize", func(e ui.Event) {
		ex.SetWidth(ui.TermWidth())
		ex.Align()
		log.Infof("resize: width=%v max-rows=%v", ex.Width, cGrid.MaxRows())
	})

	ui.Loop()
	c.SetUpdater(c.Widgets)
	return nil
}

func RefreshDisplay() error {
	// skip display refresh during scroll
	if !cursor.isScrolling {
		needsClear, err := cursor.RefreshContainers()
		if err != nil {
			return err
		}
		RedrawRows(needsClear)
	}
	return nil
}

func Display() bool {
	var menu MenuFn
	var connErr error

	cGrid.SetWidth(ui.TermWidth())
	ui.DefaultEvtStream.Hook(logEvent)

	// initial draw
	header.Align()
	status.Align()
	cursor.RefreshContainers()
	RedrawRows(true)

	HandleKeys("up", cursor.Up)
	HandleKeys("down", cursor.Down)

	HandleKeys("pgup", cursor.PgUp)
	HandleKeys("pgdown", cursor.PgDown)

	HandleKeys("exit", ui.StopLoop)
	HandleKeys("help", func() {
		menu = HelpMenu
		ui.StopLoop()
	})

	ui.Handle("/sys/kbd/<enter>", func(ui.Event) {
		menu = ContainerMenu
		ui.StopLoop()
	})
	ui.Handle("/sys/kbd/<left>", func(ui.Event) {
		menu = LogMenu
		ui.StopLoop()
	})
	ui.Handle("/sys/kbd/<right>", func(ui.Event) {
		menu = SingleView
		ui.StopLoop()
	})
	ui.Handle("/sys/kbd/l", func(ui.Event) {
		menu = LogMenu
		ui.StopLoop()
	})
	ui.Handle("/sys/kbd/e", func(ui.Event) {
		menu = ExecShell
		ui.StopLoop()
	})
	ui.Handle("/sys/kbd/o", func(ui.Event) {
		menu = SingleView
		ui.StopLoop()
	})
	ui.Handle("/sys/kbd/a", func(ui.Event) {
		config.Toggle("allContainers")
		connErr = RefreshDisplay()
		if connErr != nil {
			ui.StopLoop()
		}
	})
	ui.Handle("/sys/kbd/D", func(ui.Event) {
		dumpContainer(cursor.Selected())
	})
	ui.Handle("/sys/kbd/f", func(ui.Event) {
		menu = FilterMenu
		ui.StopLoop()
	})
	ui.Handle("/sys/kbd/H", func(ui.Event) {
		config.Toggle("enableHeader")
		RedrawRows(true)
	})
	ui.Handle("/sys/kbd/r", func(e ui.Event) {
		config.Toggle("sortReversed")
	})
	ui.Handle("/sys/kbd/s", func(ui.Event) {
		menu = SortMenu
		ui.StopLoop()
	})
	ui.Handle("/sys/kbd/S", func(ui.Event) {
		path, err := config.Write()
		if err == nil {
			log.Statusf("wrote config to %s", path)
		} else {
			log.StatusErr(err)
		}
		ui.StopLoop()
	})

	ui.Handle("/timer/1s", func(e ui.Event) {
		if log.StatusQueued() {
			ui.StopLoop()
		}
		connErr = RefreshDisplay()
		if connErr != nil {
			ui.StopLoop()
		}
	})

	ui.Handle("/sys/wnd/resize", func(e ui.Event) {
		header.Align()
		status.Align()
		cursor.ScrollPage()
		cGrid.SetWidth(ui.TermWidth())
		log.Infof("resize: width=%v max-rows=%v", cGrid.Width, cGrid.MaxRows())
		RedrawRows(true)
	})

	ui.Loop()

	if connErr != nil {
		return ShowConnError(connErr)
	}

	if log.StatusQueued() {
		for sm := range log.FlushStatus() {
			if sm.IsError {
				status.ShowErr(sm.Text)
			} else {
				status.Show(sm.Text)
			}
		}
		return false
	}

	if menu != nil {
		for menu != nil {
			menu = menu()
		}
		return false
	}

	return true
}
<|endoftext|>


<commit_before>package main

import (
	"fmt"
	"github.com/qedus/osmpbf"
	"io"
	"log"
	"math"
	"os"
	"runtime"
	"strconv"
	"strings"
)

func main() {
	debug := false // switch to true for a bit of extra information

	// grok the command line args
	filename, dist, lat, lon, pattern := grok_args(os.Args)

	if debug {
		fmt.Printf("# d,lat,lon,'pattern': %.3f, %f,%f,'%s'\n", dist, lat, lon, pattern)
	}

	// start reading the file
	f, err := os.Open(filename)
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	d := osmpbf.NewDecoder(f)
	err = d.Start(runtime.GOMAXPROCS(-1))
	if err != nil {
		log.Fatal(err)
	}

	var nc, wc, rc uint64
	for {
		if v, err := d.Decode(); err == io.EOF {
			break
		} else if err != nil {
			log.Fatal(err)
		} else {
			switch v := v.(type) {
			case *osmpbf.Node:
				handleNode(*v, dist, lat, lon, pattern)
				//handleNode(*v, lat, lon, dist)
				nc++
			case *osmpbf.Way:
				// Process Way v.
				wc++
			case *osmpbf.Relation:
				// Process Relation v.
				rc++
			default:
				log.Fatalf("unknown type %T\n", v)
			}
		}
	}
	if debug {
		fmt.Printf("# Nodes: %d, Ways: %d, Relations: %d\n", nc, wc, rc)
	}
}

func grok_args(args []string) (filename string, dist float64, lat float64, lon float64, pattern string) {

	var err error

	// default values
	filename = ""
	lat = 0.0
	lon = 0.0
	dist = -1.0 // negative distance means: no within-distance checking
	pattern = ""

	// show help if not enough args
	if len(os.Args) < 2 {
		fmt.Fprintf(os.Stderr, `Usage:

%s osm-file [-d max-dist lat lon] [pattern]

    eg. %s england-latest.osm mexican
    eg. %s central-america-latest.osm -d 10 12.1166 -68.9333 > willemstad10k.csv

The unit for maximum distance is km.
`, os.Args[0], os.Args[0], os.Args[0])
		os.Exit(1)
	}

	// first arg is filename
	filename = os.Args[1]
	if !fileExists(filename) {
		fmt.Fprintf(os.Stderr, "File does not exist: %s\n", os.Args[1])
		os.Exit(1)
	}

	// distance,lat,lon args
	pattern_idx := 2
	if args[2] == "-d" {
		dist, err = strconv.ParseFloat(args[3], 64)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Illegal value for max-distance: %s\n", os.Args[3])
			os.Exit(1)
		}

		lat, err = strconv.ParseFloat(args[4], 64)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Illegal value for latitude: %s\n", os.Args[4])
			os.Exit(1)
		}

		lon, err = strconv.ParseFloat(args[5], 64)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Illegal value for longitude: %s\n", os.Args[5])
			os.Exit(1)
		}
		pattern_idx = 6
	}

	// pattern arg
	if len(args) >= (pattern_idx + 1) {
		pattern = strings.ToLower(args[pattern_idx]) // SEARCH IN LOWER CASE!
	}
	return
}

/* approximately calculate the distance between 2 points
   from: http://www.movable-type.co.uk/scripts/latlong.html
   note: φ=lat λ=lon  in RADIANS!
   var x = (λ2-λ1) * Math.cos((φ1+φ2)/2);
   var y = (φ2-φ1);
   var d = Math.sqrt(x*x + y*y) * R;
*/
func rough_distance(lat1, lon1, lat2, lon2 float64) float64 {

	// convert to radians
	lat1 = lat1 * math.Pi / 180.0
	lon1 = lon1 * math.Pi / 180.0
	lat2 = lat2 * math.Pi / 180.0
	lon2 = lon2 * math.Pi / 180.0

	r := 6371.0 // km
	x := (lon2 - lon1) * math.Cos((lat1+lat2)/2)
	y := (lat2 - lat1)
	d := math.Sqrt(x*x+y*y) * r
	return d
}

func fileExists(name string) bool {
	if _, err := os.Stat(name); err != nil {
		if os.IsNotExist(err) {
			return false
		}
	}
	return true
}

func handleNode(nd osmpbf.Node, dist float64, lat float64, lon float64, contains_pattern string) {
	estim_distance := 0.0
	bingo_distance := true
	if dist >= 0.0 { // do we need to examine the distance?
		estim_distance = rough_distance(lat, lon, nd.Lat, nd.Lon)
		bingo_distance = (estim_distance < dist)
	}

	bingo_pattern := true
	if len(contains_pattern) > 0 { // do we need to examine the pattern?
		bingo_pattern = false
		for k, v := range nd.Tags {
			bingo_pattern = strings.Contains(strings.ToLower(k), contains_pattern) ||
				strings.Contains(strings.ToLower(v), contains_pattern)
			if bingo_pattern {
				break // out of the loop
			}
		}
	}

	if bingo_distance && bingo_pattern {
		// turn the Tags map into a k:v string
		tgs := ""
		for k, v := range nd.Tags {
			tgs = tgs + " " + k + ":" + v
		}

		if dist >= 0.0 {
			fmt.Printf("%f, %f, %s #,%.2f\n", nd.Lat, nd.Lon, tgs, estim_distance)
		} else {
			fmt.Printf("%f, %f, %s\n", nd.Lat, nd.Lon, tgs)
		}
	}
}
<commit_msg>fix in help message<commit_after>package main

import (
	"fmt"
	"github.com/qedus/osmpbf"
	"io"
	"log"
	"math"
	"os"
	"runtime"
	"strconv"
	"strings"
)

func main() {
	debug := false // switch to true for a bit of extra information

	// grok the command line args
	filename, dist, lat, lon, pattern := grok_args(os.Args)

	if debug {
		fmt.Printf("# d,lat,lon,'pattern': %.3f, %f,%f,'%s'\n", dist, lat, lon, pattern)
	}

	// start reading the file
	f, err := os.Open(filename)
	if err != nil {
		log.Fatal(err)
	}
	defer f.Close()

	d := osmpbf.NewDecoder(f)
	err = d.Start(runtime.GOMAXPROCS(-1))
	if err != nil {
		log.Fatal(err)
	}

	var nc, wc, rc uint64
	for {
		if v, err := d.Decode(); err == io.EOF {
			break
		} else if err != nil {
			log.Fatal(err)
		} else {
			switch v := v.(type) {
			case *osmpbf.Node:
				handleNode(*v, dist, lat, lon, pattern)
				//handleNode(*v, lat, lon, dist)
				nc++
			case *osmpbf.Way:
				// Process Way v.
				wc++
			case *osmpbf.Relation:
				// Process Relation v.
				rc++
			default:
				log.Fatalf("unknown type %T\n", v)
			}
		}
	}
	if debug {
		fmt.Printf("# Nodes: %d, Ways: %d, Relations: %d\n", nc, wc, rc)
	}
}

func grok_args(args []string) (filename string, dist float64, lat float64, lon float64, pattern string) {

	var err error

	// default values
	filename = ""
	lat = 0.0
	lon = 0.0
	dist = -1.0 // negative distance means: no within-distance checking
	pattern = ""

	// show help if not enough args
	if len(os.Args) < 2 {
		fmt.Fprintf(os.Stderr, `Usage:

%s osm-file [-d max-dist lat lon] [pattern]

    eg. %s england-latest.osm.pbf mexican
    eg. %s central-america-latest.osm.pbf -d 10 12.1166 -68.9333 > willemstad10k.csv

The unit for maximum distance is km.
`, os.Args[0], os.Args[0], os.Args[0])
		os.Exit(1)
	}

	// first arg is filename
	filename = os.Args[1]
	if !fileExists(filename) {
		fmt.Fprintf(os.Stderr, "File does not exist: %s\n", os.Args[1])
		os.Exit(1)
	}

	// distance,lat,lon args
	pattern_idx := 2
	if args[2] == "-d" {
		dist, err = strconv.ParseFloat(args[3], 64)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Illegal value for max-distance: %s\n", os.Args[3])
			os.Exit(1)
		}

		lat, err = strconv.ParseFloat(args[4], 64)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Illegal value for latitude: %s\n", os.Args[4])
			os.Exit(1)
		}

		lon, err = strconv.ParseFloat(args[5], 64)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Illegal value for longitude: %s\n", os.Args[5])
			os.Exit(1)
		}
		pattern_idx = 6
	}

	// pattern arg
	if len(args) >= (pattern_idx + 1) {
		pattern = strings.ToLower(args[pattern_idx]) // SEARCH IN LOWER CASE!
	}
	return
}

/* approximately calculate the distance between 2 points
   from: http://www.movable-type.co.uk/scripts/latlong.html
   note: φ=lat λ=lon  in RADIANS!
   var x = (λ2-λ1) * Math.cos((φ1+φ2)/2);
   var y = (φ2-φ1);
   var d = Math.sqrt(x*x + y*y) * R;
*/
func rough_distance(lat1, lon1, lat2, lon2 float64) float64 {

	// convert to radians
	lat1 = lat1 * math.Pi / 180.0
	lon1 = lon1 * math.Pi / 180.0
	lat2 = lat2 * math.Pi / 180.0
	lon2 = lon2 * math.Pi / 180.0

	r := 6371.0 // km
	x := (lon2 - lon1) * math.Cos((lat1+lat2)/2)
	y := (lat2 - lat1)
	d := math.Sqrt(x*x+y*y) * r
	return d
}

func fileExists(name string) bool {
	if _, err := os.Stat(name); err != nil {
		if os.IsNotExist(err) {
			return false
		}
	}
	return true
}

func handleNode(nd osmpbf.Node, dist float64, lat float64, lon float64, contains_pattern string) {
	estim_distance := 0.0
	bingo_distance := true
	if dist >= 0.0 { // do we need to examine the distance?
		estim_distance = rough_distance(lat, lon, nd.Lat, nd.Lon)
		bingo_distance = (estim_distance < dist)
	}

	bingo_pattern := true
	if len(contains_pattern) > 0 { // do we need to examine the pattern?
		bingo_pattern = false
		for k, v := range nd.Tags {
			bingo_pattern = strings.Contains(strings.ToLower(k), contains_pattern) ||
				strings.Contains(strings.ToLower(v), contains_pattern)
			if bingo_pattern {
				break // out of the loop
			}
		}
	}

	if bingo_distance && bingo_pattern {
		// turn the Tags map into a k:v string
		tgs := ""
		for k, v := range nd.Tags {
			tgs = tgs + " " + k + ":" + v
		}

		if dist >= 0.0 {
			fmt.Printf("%f, %f, %s #,%.2f\n", nd.Lat, nd.Lon, tgs, estim_distance)
		} else {
			fmt.Printf("%f, %f, %s\n", nd.Lat, nd.Lon, tgs)
		}
	}
}
<|endoftext|>


<commit_before>package main

import (
	"errors"
	"flag"
	"fmt"
	"github.com/c-14/grue/config"
	"os"
)

const version = "0.2.0-alpha"

func usage() string {
	return `usage: grue [--help] {add|fetch|import|init_cfg} ...

Subcommands:
	add <name> <url>
	fetch [-init]
	import <config>
	init_cfg`
}

func add(args []string, conf *config.GrueConfig) error {
	if len(args) != 2 {
		return errors.New("usage: grue add <name> <url>")
	}
	var name string = args[0]
	var uri string = args[1]
	return conf.AddAccount(name, uri)
}

func main() {
	if len(os.Args) < 2 {
		fmt.Fprintln(os.Stderr, usage())
		os.Exit(EX_USAGE)
	}
	conf, err := config.ReadConfig()
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(EX_TEMPFAIL)
	}
	defer conf.Unlock()
	switch cmd := os.Args[1]; cmd {
	case "add":
		err = add(os.Args[2:], conf)
	case "fetch":
		var fetchCmd = flag.NewFlagSet("fetch", flag.ExitOnError)
		var initFlag = fetchCmd.Bool("init", false, "Don't send emails, only initialize database of read entries")
		err = fetchCmd.Parse(os.Args[2:])
		if err == nil {
			err = fetchFeeds(conf, *initFlag)
		}
	case "import":
		err = config.ImportCfg(os.Args[2:])
	case "init_cfg":
		break
	case "-v":
		fallthrough
	case "--version":
		fmt.Println(version)
	case "-h":
		fallthrough
	case "--help":
		fmt.Println(usage())
	default:
		fmt.Fprintln(os.Stderr, usage())
		conf.Unlock()
		os.Exit(EX_USAGE)
	}
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
	}
}
<commit_msg>Switch to ContinueOnError when parsing fetchCmd.<commit_after>package main

import (
	"errors"
	"flag"
	"fmt"
	"github.com/c-14/grue/config"
	"os"
)

const version = "0.2.0-alpha"

func usage() string {
	return `usage: grue [--help] {add|fetch|import|init_cfg} ...

Subcommands:
	add <name> <url>
	fetch [-init]
	import <config>
	init_cfg`
}

func add(args []string, conf *config.GrueConfig) error {
	if len(args) != 2 {
		return errors.New("usage: grue add <name> <url>")
	}
	var name string = args[0]
	var uri string = args[1]
	return conf.AddAccount(name, uri)
}

func main() {
	if len(os.Args) < 2 {
		fmt.Fprintln(os.Stderr, usage())
		os.Exit(EX_USAGE)
	}
	conf, err := config.ReadConfig()
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(EX_TEMPFAIL)
	}
	defer conf.Unlock()
	switch cmd := os.Args[1]; cmd {
	case "add":
		err = add(os.Args[2:], conf)
	case "fetch":
		var fetchCmd = flag.NewFlagSet("fetch", flag.ContinueOnError)
		var initFlag = fetchCmd.Bool("init", false, "Don't send emails, only initialize database of read entries")
		err = fetchCmd.Parse(os.Args[2:])
		if err == nil {
			err = fetchFeeds(conf, *initFlag)
		}
	case "import":
		err = config.ImportCfg(os.Args[2:])
	case "init_cfg":
		break
	case "-v":
		fallthrough
	case "--version":
		fmt.Println(version)
	case "-h":
		fallthrough
	case "--help":
		fmt.Println(usage())
	default:
		fmt.Fprintln(os.Stderr, usage())
		conf.Unlock()
		os.Exit(EX_USAGE)
	}
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
	}
}
<|endoftext|>


<commit_before>package gtra

import (
	"errors"
	"github.com/tidwall/gjson"
	"github.com/jinzhongmin/gtra/lang"
	"io"
	"log"
	"net/http"
	"net/url"
	"regexp"
	"strconv"
	"strings"
)

type Translate struct {
	str string

	sl string
	tl string
	dt []string
}

func NewTra(str string) *Translate {
	t := new(Translate)
	t.str = str

	t.sl = lang.AUTO
	t.tl = lang.ZHCN
	t.dt = []string{"at", "bd", "ex", "ld", "md", "qca", "rw", "rm", "ss", "t"}

	return t

}
func (t *Translate) tk() string {
	tkki, tkkf := gettkk()
	tki, tkf := gettk(int32(tkki), int32(tkkf), t.str)

	return (strconv.Itoa(int(tki)) + "." + strconv.Itoa(int(tkf)))
}
func (t *Translate) url(tl string) string {
	urls := "https://translate.google.cn/translate_a/single?"

	urls = urls + "&client=gtx"
	urls = urls + "&sl=" + t.sl
	urls = urls + "&tl=" + tl
	urls = urls + "&hl=" + tl
	for i := range t.dt {
		urls = urls + "&dt=" + t.dt[i]
	}
	//urls = urls + "&ie=UTF-8"
	//urls = urls + "&oe=UTF-8"
	//urls = urls + "&otf=1"
	//urls = urls + "&ssel=0"
	//urls = urls + "&tsel=0"
	//urls = urls + "&kc=7"
	urls = urls + "&q=" + url.QueryEscape(t.str)
	urls = urls + "&tk=" + t.tk()

	return urls
}
func (t *Translate) To(tl string) (string, error) {
	r := get(t.url(tl))

	var e error
	if r[0] != '[' {
		log.Panicln("server deny!")
		e = errors.New("DENY")
	}

	j, _ := toJson(r, 0)

	j = strings.Replace(strings.Replace(j, "]", "}", -1), "[", "{", -1)
	p := gjson.Parse(j)

	return p.Get("0.0.0").String(), e
}
func (t *Translate) SetSl(sl string) *Translate {
	t.sl = sl

	return t
}

/*func Translate(str string, from string, to string, dt []string) string {
	toJson(gtra(str, from, to, dt))
}*/

/**************************\
 * Base for get remote src*
\**************************/
func get(url string) string {

	c := &http.Client{}
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		log.Panicln(err)
	}
	req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36")

	ret, err := c.Do(req)
	if err != nil {
		log.Panicln(err)
	}

	return string(dataLoad(ret.Body))
}
func dataLoad(body io.ReadCloser) []byte {

	data := make([]byte, 0)

	l := 0
	_, err := body.Read(data)

	if err == io.EOF {
		data = data[:l]
	} else if err != nil {
		log.Panicln(err)
	} else {
		for err == nil {

			_data := make([]byte, 100)
			l, err = body.Read(_data)

			tmp := make([]byte, len(data)+l)
			tmp = append(data, _data...)
			data = tmp
		}
	}

	return data
}

/**************************\
 *       translate        *
\**************************/
/*func gtra(tra string, from string, to string, dt []string) string {

	tkka, tkkb := gettkk()
	tka, tkb := gettk(int32(tkka), int32(tkkb), tra)

	_url := "https://translate.google.cn/translate_a/single?client=t&ie=UTF-8&oe=UTF-8&otf=1&ssel=0&tsel=0&kc=7"
	_url += ("&q=" + url.QueryEscape(tra))
	_url += ("&sl=" + from + "&tl=" + to + "&hl=" + to)

	for i := range dt {
		_url = _url + "&dt=" + dt[i]
	}

	_url += ("&tk=" + strconv.FormatInt(int64(tka), 10) + "." + strconv.FormatInt(int64(tkb), 10))

	return get(_url)
}*/

/**************************\
 * 	   calculate tk       *
\**************************/
func gettkk() (int64, int64) {
	s := get("https://translate.google.cn/")
	reg := regexp.MustCompile(`TKK=.*\)\(\)\)'\)`)
	tkkc := reg.FindAllString(s, -1)

	reg = regexp.MustCompile(`\-*\d{3,}`)
	tkkv := reg.FindAllString(tkkc[0], -1)

	a, _ := strconv.ParseInt(tkkv[0], 10, 64)
	b, _ := strconv.ParseInt(tkkv[1], 10, 64)
	c, _ := strconv.ParseInt(tkkv[2], 10, 64)

	return c, a + b
}
func gettk(tkka int32, tkkb int32, str string) (int32, int32) {
	b := make([]byte, len(str))
	b = []byte(str)
	a := tkka
	for i := 0; i < len(b); i++ {
		a += int32((b[i]))
		a = xr(a, "+-a^+6")
	}
	a = xr(a, "+-3^+b+-f")
	a ^= tkkb
	if a < 0 {
		a = -a
	}
	a %= 1E6

	return a, a ^ tkka
}
func xr(a int32, b string) int32 {
	for c := 0; c < len(b)-2; c = c + 3 {
		var d int32
		d = int32(b[c+2])

		if d >= int32('a') {
			d = d - 87
		} else {
			d = d - 48
		}

		if int32('+') == int32(b[c+1]) {
			if a >= 0 {
				d = a >> uint32(d)
			} else {
				t := 4294967295 + int(a)
				t = t + 1
				d = int32(t >> uint(d))
			}
		} else {
			d = a << uint32(d)
		}

		if int32('+') == int32(b[c]) {
			a = a + d
		} else {
			a = a ^ d
		}
	}
	return a
}

/**************************\
 * 	        parse         *
\**************************/
func toJson(str string, i int) (string, int) {
	_str := ""

	if str[i] == '[' {
		i++
	}
	n := 0
	l := i

	for ; i < len(str); i++ {
		if str[i] == ',' {
			_str = _str + "\"" + strconv.Itoa(n) + "\":" + str[l:i+1]
			n++
			l = i + 1
		} else if str[i] == ']' {
			_str = _str + "\"" + strconv.Itoa(n) + "\":" + str[l:i+1] + ","
			break
		} else if str[i] == '[' {
			_s, _i := toJson(str, i)

			i = _i + 1
			l = i + 1

			_str = _str + "\"" + strconv.Itoa(n) + "\":" + _s
			n++
		}
	}
	_str = "[" + _str

	return _str, i
}

/*func toJson(list string) string {
	var l string
	for i := 0; i < len(list); i++ {
		if list[i] == '[' {
			l = list[0:i+1] + "\"0\":" + list[i+1:]
			list = l
		}
	}
	n := 0
	for i := 0; i < len(list); i++ {

		if list[i] == ',' {
			n++
			l = list[0:i+1] + "\"" + strconv.Itoa(n) + "\":" + list[i+1:]
			list = l
		}

		if i < len(list)-1 && list[i+1] == '[' {
			n = 0
		}
	}
	return strings.Replace(strings.Replace(l, "]", "}", -1), "[", "{", -1)

}*/
<commit_msg>优化并添加新功能<commit_after>package gtra

import (
	"errors"
	"github.com/jinzhongmin/gtra/lang"
	"github.com/tidwall/gjson"
	"io/ioutil"
	"log"
	"net/http"
	"net/url"
	"regexp"
	"strconv"
	"strings"
)

type Translate struct {
	str string

	sl string
	tl string
}

func NewTra(str string) *Translate {
	t := new(Translate)
	t.str = str

	t.sl = lang.AUTO
	t.tl = lang.ZHCN

	return t

}
func (t *Translate) tk() string {
	tkki, tkkf := gettkk()
	tki, tkf := gettk(int32(tkki), int32(tkkf), t.str)

	return (strconv.Itoa(int(tki)) + "." + strconv.Itoa(int(tkf)))
}
func (t *Translate) url(tl string, dt []string) string {
	urls := "https://translate.google.cn/translate_a/single?"

	urls = urls + "&client=gtx"
	urls = urls + "&sl=" + t.sl
	urls = urls + "&tl=" + tl
	urls = urls + "&hl=" + tl
	for i := range dt {
		urls = urls + "&dt=" + dt[i]
	}
	//urls = urls + "&ie=UTF-8"
	//urls = urls + "&oe=UTF-8"
	//urls = urls + "&otf=1"
	//urls = urls + "&ssel=0"
	//urls = urls + "&tsel=0"
	//urls = urls + "&kc=7"
	urls = urls + "&q=" + url.QueryEscape(t.str)
	urls = urls + "&tk=" + t.tk()

	return urls
}
func (t *Translate) To(tl string) (string, error) {
	r := get(t.url(tl, []string{"t"}))

	if r[0] != '[' {
		log.Panicln("server deny!")
		e := errors.New("DENY")
		return "", e
	}

	/*	j, _ := toJson(r, 0)

		j = strings.Replace(strings.Replace(j, "]", "}", -1), "[", "{", -1)*/
	p := gjson.Parse(r)

	return p.Get("0.0.0").String(), nil
}
func (t *Translate) SetSl(sl string) *Translate {
	t.sl = sl

	return t
}
func (t *Translate) GetSynonyms(tl string) ([]string, error) {

	r := get(t.url(tl, []string{"t", "at"}))

	if r[0] != '[' {
		log.Panicln("server deny!")
		e := errors.New("DENY")
		return []string{""}, e
	}
	/*	j, _ := toJson(r, 0)

		j = strings.Replace(strings.Replace(j, "]", "}", -1), "[", "{", -1)*/
	p := gjson.Parse(r)
	var ats []string

	p.Get("5.0.2").ForEach(func(key gjson.Result, value gjson.Result) bool {
		ats = append(ats, value.Get("0").String())

		return true
	})
	return ats, nil
}

func (t *Translate) GetExamples(tl string) ([]string, error) {

	r := get(t.url(tl, []string{"t", "ex"}))

	if r[0] != '[' {
		log.Panicln("server deny!")
		e := errors.New("DENY")
		return []string{""}, e
	}

	p := gjson.Parse(r)

	var exs []string

	p.Get("13.0").ForEach(func(key gjson.Result, value gjson.Result) bool {
		_s := strings.Replace(value.Get("0").String(), "<b>", "[", -1)
		s := strings.Replace(_s, "</b>", "]", -1)
		exs = append(exs, s)

		return true
	})

	return exs, nil
}

func (t *Translate) GetAcceptation(tl string) (map[string][]string, error) {

	r := get(t.url(tl, []string{"t", "md"}))

	if r[0] != '[' {
		log.Panicln("server deny!")
		e := errors.New("DENY")
		return nil, e
	}
	/*	j, _ := toJson(r, 0)

		j = strings.Replace(strings.Replace(j, "]", "}", -1), "[", "{", -1)*/
	p := gjson.Parse(r)

	mds := make(map[string][]string)

	p.Get("12").ForEach(func(key gjson.Result, value gjson.Result) bool {
		value.Get("1").ForEach(func(_key gjson.Result, _value gjson.Result) bool {
			mds[value.Get("0").String()] = append(mds[value.Get("0").String()], _value.Get("0").String())
			return true
		})

		return true
	})
	return mds, nil
}

/*func Translate(str string, from string, to string, dt []string) string {
	toJson(gtra(str, from, to, dt))
}*/

/**************************\
 * Base for get remote src*
\**************************/
func get(url string) string {

	c := &http.Client{}
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		log.Panicln(err)
	}
	req.Header.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36")

	ret, err := c.Do(req)
	if err != nil {
		log.Panicln(err)
	}

	d, _ := ioutil.ReadAll(ret.Body)
	return string(d)
}

/*
func dataLoad(body io.ReadCloser) []byte {

	data := make([]byte, 0)

	l := 0
	_, err := body.Read(data)

	if err == io.EOF {
		data = data[:l]
	} else if err != nil {
		log.Panicln(err)
	} else {
		for err == nil {

			_data := make([]byte, 100)
			l, err = body.Read(_data)

			tmp := make([]byte, len(data)+l)
			tmp = append(data, _data...)
			data = tmp
		}
	}

	return data
}
*/

/**************************\
 *       translate        *
\**************************/
/*func gtra(tra string, from string, to string, dt []string) string {

	tkka, tkkb := gettkk()
	tka, tkb := gettk(int32(tkka), int32(tkkb), tra)

	_url := "https://translate.google.cn/translate_a/single?client=t&ie=UTF-8&oe=UTF-8&otf=1&ssel=0&tsel=0&kc=7"
	_url += ("&q=" + url.QueryEscape(tra))
	_url += ("&sl=" + from + "&tl=" + to + "&hl=" + to)

	for i := range dt {
		_url = _url + "&dt=" + dt[i]
	}

	_url += ("&tk=" + strconv.FormatInt(int64(tka), 10) + "." + strconv.FormatInt(int64(tkb), 10))

	return get(_url)
}*/

/**************************\
 * 	   calculate tk       *
\**************************/
func gettkk() (int64, int64) {
	s := get("https://translate.google.cn/")
	reg := regexp.MustCompile(`TKK=.*\)\(\)\)'\)`)
	tkkc := reg.FindAllString(s, -1)

	reg = regexp.MustCompile(`\-*\d{3,}`)
	tkkv := reg.FindAllString(tkkc[0], -1)

	a, _ := strconv.ParseInt(tkkv[0], 10, 64)
	b, _ := strconv.ParseInt(tkkv[1], 10, 64)
	c, _ := strconv.ParseInt(tkkv[2], 10, 64)

	return c, a + b
}
func gettk(tkka int32, tkkb int32, str string) (int32, int32) {
	b := make([]byte, len(str))
	b = []byte(str)
	a := tkka
	for i := 0; i < len(b); i++ {
		a += int32((b[i]))
		a = xr(a, "+-a^+6")
	}
	a = xr(a, "+-3^+b+-f")
	a ^= tkkb
	if a < 0 {
		a = -a
	}
	a %= 1E6

	return a, a ^ tkka
}
func xr(a int32, b string) int32 {
	for c := 0; c < len(b)-2; c = c + 3 {
		var d int32
		d = int32(b[c+2])

		if d >= int32('a') {
			d = d - 87
		} else {
			d = d - 48
		}

		if int32('+') == int32(b[c+1]) {
			if a >= 0 {
				d = a >> uint32(d)
			} else {
				t := 4294967295 + int(a)
				t = t + 1
				d = int32(t >> uint(d))
			}
		} else {
			d = a << uint32(d)
		}

		if int32('+') == int32(b[c]) {
			a = a + d
		} else {
			a = a ^ d
		}
	}
	return a
}

/**************************\
 * 	        parse         *
\**************************/
/*func toJson(str string, i int) (string, int) {
	_str := ""

	if str[i] == '[' {
		i++
	}
	n := 0
	l := i

	for ; i < len(str); i++ {
		if str[i] == ',' {
			_str = _str + "\"" + strconv.Itoa(n) + "\":" + str[l:i+1]
			n++
			l = i + 1
		} else if str[i] == ']' {
			_str = _str + "\"" + strconv.Itoa(n) + "\":" + str[l:i+1] + ","
			break
		} else if str[i] == '[' {
			_s, _i := toJson(str, i)

			i = _i + 1
			l = i + 1

			_str = _str + "\"" + strconv.Itoa(n) + "\":" + _s
			n++
		}
	}
	_str = "[" + _str + "]"

	return _str, i
}*/

/*func toJson(list string) string {
	var l string
	for i := 0; i < len(list); i++ {
		if list[i] == '[' {
			l = list[0:i+1] + "\"0\":" + list[i+1:]
			list = l
		}
	}
	n := 0
	for i := 0; i < len(list); i++ {

		if list[i] == ',' {
			n++
			l = list[0:i+1] + "\"" + strconv.Itoa(n) + "\":" + list[i+1:]
			list = l
		}

		if i < len(list)-1 && list[i+1] == '[' {
			n = 0
		}
	}
	return strings.Replace(strings.Replace(l, "]", "}", -1), "[", "{", -1)

}*/
<|endoftext|>


<commit_before>package hdfs

import (
	"errors"
	//"fmt"
	"io/ioutil"
	//"log"
	"github.com/eaciit/colony-core/v0"
	"os"
	"path/filepath"
	"strconv"
	"strings"
	"sync"
)

func (h *WebHdfs) GetToLocal(path string, destination string, permission string) error {
	d, err := h.Get(path)
	if err != nil {
		return err
	}
	if permission == "" {
		permission = "755"
	}
	iperm, _ := strconv.Atoi(permission)
	err = ioutil.WriteFile(destination, d, os.FileMode(iperm))
	if err != nil {
		return err
	}
	return nil
}

func (h *WebHdfs) Get(path string) ([]byte, error) {
	r, err := h.call("GET", path, OP_OPEN, nil)
	if err != nil {
		return nil, err
	}
	if r.StatusCode != 307 {
		return nil, errors.New("Invalid Response Header on OP_OPEN: " + r.Status)
	}

	location := r.Header["Location"][0]
	r, err = h.call("GET", location, OP_OPEN, nil)
	if err != nil {
		return nil, err
	}
	if r.StatusCode != 200 {
		return nil, errors.New(r.Status)
	}
	d, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return nil, err
	}
	defer r.Body.Close()
	return d, nil
}

func mergeMapString(source map[string]string, adds map[string]string) map[string]string {
	if source == nil {
		source = make(map[string]string)
	}
	if adds != nil {
		for k, v := range adds {
			source[k] = v
		}
	}
	return source
}

func (h *WebHdfs) Put(localfile string, destination string, permission string, parms map[string]string, server *colonycore.Server) error {
	if permission == "" {
		permission = "755"
	}
	parms = mergeMapString(parms, map[string]string{"permission": permission})
	r, err := h.call("PUT", destination, OP_CREATE, parms)
	if err != nil {
		return err
	}
	if r.StatusCode != 307 {
		return errors.New("Invalid Response Header on OP_CREATE: " + r.Status)
	}

	location := r.Header["Location"][0]
	if server != nil {
		for _, alias := range server.HostAlias {
			if strings.Contains(strings.Split(location, ":")[1], alias.HostName) {
				location = strings.Replace(location, alias.HostName, alias.IP, 1)
				break
			}
		}
	}

	r, err = h.callPayload("PUT", location, OP_CREATE, localfile, nil)
	if err != nil {
		return err
	}
	if r.StatusCode != 201 {
		return errors.New(r.Status)
	}
	return nil
}

func (h *WebHdfs) Puts(paths []string, destinationFolder string, permission string, parms map[string]string) map[string]error {
	var es map[string]error
	if permission == "" {
		permission = "755"
	}

	fileCount := len(paths)

	//parms = mergeMapString(parms, map[string]string{"permission": strconv.Itoa(permission)})
	ipool := 0
	iprocessing := 0
	iread := 0
	files := []string{}
	for _, path := range paths {
		ipool = ipool + 1
		iread = iread + 1
		files = append(files, path)
		if ipool == h.Config.PoolSize || iread == fileCount {
			wg := sync.WaitGroup{}
			wg.Add(ipool)

			for _, f := range files {
				go func(path string, swg *sync.WaitGroup) {
					defer swg.Done()
					iprocessing = iprocessing + 1
					_, filename := filepath.Split(path)
					newfilename := filepath.Join(destinationFolder, filename)
					e := h.Put(path, newfilename, permission, parms)
					//var e error
					if e != nil {
						if es == nil {
							es = make(map[string]error)
							es[path] = e
						}
						//fmt.Println(path, "=> ", newfilename, " ... FAIL => ", e.Error(), " | Processing ", iprocessing, " of ", fileCount)
					} else {
						//fmt.Println(path, "=> ", newfilename, " ... SUCCESS | Processing ", iprocessing, " of ", fileCount)
					}
				}(f, &wg)
			}

			wg.Wait()
			ipool = 0
			files = []string{}
		}
	}

	return es
}

func (h *WebHdfs) Append(localfile string, destination string) error {
	r, err := h.call("POST", destination, OP_APPEND, nil)
	if err != nil {
		return err
	}
	if r.StatusCode != 307 {
		return errors.New("Invalid Response Header on OP_APPEND: " + r.Status)
	}

	location := r.Header["Location"][0]

	r, err = h.callPayload("POST", location, OP_APPEND, localfile, nil)
	if err != nil {
		return err
	}
	if r.StatusCode != 201 {
		return errors.New(r.Status)
	}
	return nil
}

func (h *WebHdfs) SetOwner(path string, owner string, group string) error {
	ownerInfo := map[string]string{}
	if owner != "" {
		ownerInfo["owner"] = owner
	}
	if group != "" {
		ownerInfo["group"] = group
	}
	r, e := h.call("PUT", path, OP_SETOWNER, ownerInfo)
	if e != nil {
		return e
	}
	if r.StatusCode != 200 {
		return errors.New("Invalid Response Header on OP_SETOWNER: " + r.Status)
	}
	return nil
}

func (h *WebHdfs) SetPermission(path string, permission string) error {
	if permission == "" {
		permission = "755"
	}

	parms := map[string]string{}
	parms["permission"] = permission

	r, e := h.call("PUT", path, OP_SETPERMISSION, parms)
	if e != nil {
		return e
	}
	if r.StatusCode != 200 {
		return errors.New("Invalid Response Header on OP_SETPERMISSION: " + r.Status)
	}
	return nil
}

/*
func (h *WebHdfs) CreateNewFile(path, filename, permission string) error {
	if permission == "" {
		permission = "755"
	}

	parms := map[string]string{}
	parms["permission"] = permission

	var fullpath string

	if string(path[len(path)-1]) == "/" {
		fullpath = path + filename
	} else {
		fullpath = path + "/" + filename
	}

	log.Println(fullpath)

	r, e := h.call("PUT", fullpath, OP_CREATE, parms)
	if e != nil {
		return e
	}
	if r.StatusCode != 200 {
		return errors.New("Invalid Response Header on OP_CREATE: " + r.Status)
	}
	return nil
}
*/
<commit_msg>add server as parameter on GetToLocal and Puts<commit_after>package hdfs

import (
	"errors"
	//"fmt"
	"io/ioutil"
	//"log"
	"github.com/eaciit/colony-core/v0"
	"os"
	"path/filepath"
	"strconv"
	"strings"
	"sync"
)

func (h *WebHdfs) GetToLocal(path string, destination string, permission string, server *colonycore.Server) error {
	d, err := h.Get(path)
	if err != nil {
		return err
	}
	if permission == "" {
		permission = "755"
	}

	if server != nil {
		for _, alias := range server.HostAlias {
			if strings.Contains(strings.Split(destination, ":")[1], alias.HostName) {
				destination = strings.Replace(destination, alias.HostName, alias.IP, 1)
				break
			}
		}
	}

	iperm, _ := strconv.Atoi(permission)
	err = ioutil.WriteFile(destination, d, os.FileMode(iperm))
	if err != nil {
		return err
	}
	return nil
}

func (h *WebHdfs) Get(path string) ([]byte, error) {
	r, err := h.call("GET", path, OP_OPEN, nil)
	if err != nil {
		return nil, err
	}
	if r.StatusCode != 307 {
		return nil, errors.New("Invalid Response Header on OP_OPEN: " + r.Status)
	}

	location := r.Header["Location"][0]
	r, err = h.call("GET", location, OP_OPEN, nil)
	if err != nil {
		return nil, err
	}
	if r.StatusCode != 200 {
		return nil, errors.New(r.Status)
	}
	d, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return nil, err
	}
	defer r.Body.Close()
	return d, nil
}

func mergeMapString(source map[string]string, adds map[string]string) map[string]string {
	if source == nil {
		source = make(map[string]string)
	}
	if adds != nil {
		for k, v := range adds {
			source[k] = v
		}
	}
	return source
}

func (h *WebHdfs) Put(localfile string, destination string, permission string, parms map[string]string, server *colonycore.Server) error {
	if permission == "" {
		permission = "755"
	}
	parms = mergeMapString(parms, map[string]string{"permission": permission})
	r, err := h.call("PUT", destination, OP_CREATE, parms)
	if err != nil {
		return err
	}
	if r.StatusCode != 307 {
		return errors.New("Invalid Response Header on OP_CREATE: " + r.Status)
	}

	location := r.Header["Location"][0]
	if server != nil {
		for _, alias := range server.HostAlias {
			if strings.Contains(strings.Split(location, ":")[1], alias.HostName) {
				location = strings.Replace(location, alias.HostName, alias.IP, 1)
				break
			}
		}
	}

	r, err = h.callPayload("PUT", location, OP_CREATE, localfile, nil)
	if err != nil {
		return err
	}
	if r.StatusCode != 201 {
		return errors.New(r.Status)
	}
	return nil
}

func (h *WebHdfs) Puts(paths []string, destinationFolder string, permission string, parms map[string]string, server *colonycore.Server) map[string]error {
	var es map[string]error
	if permission == "" {
		permission = "755"
	}

	fileCount := len(paths)

	//parms = mergeMapString(parms, map[string]string{"permission": strconv.Itoa(permission)})
	ipool := 0
	iprocessing := 0
	iread := 0
	files := []string{}
	for _, path := range paths {
		ipool = ipool + 1
		iread = iread + 1
		files = append(files, path)
		if ipool == h.Config.PoolSize || iread == fileCount {
			wg := sync.WaitGroup{}
			wg.Add(ipool)

			for _, f := range files {
				go func(path string, swg *sync.WaitGroup) {
					defer swg.Done()
					iprocessing = iprocessing + 1
					_, filename := filepath.Split(path)
					newfilename := filepath.Join(destinationFolder, filename)
					e := h.Put(path, newfilename, permission, parms, server)
					//var e error
					if e != nil {
						if es == nil {
							es = make(map[string]error)
							es[path] = e
						}
						//fmt.Println(path, "=> ", newfilename, " ... FAIL => ", e.Error(), " | Processing ", iprocessing, " of ", fileCount)
					} else {
						//fmt.Println(path, "=> ", newfilename, " ... SUCCESS | Processing ", iprocessing, " of ", fileCount)
					}
				}(f, &wg)
			}

			wg.Wait()
			ipool = 0
			files = []string{}
		}
	}

	return es
}

func (h *WebHdfs) Append(localfile string, destination string) error {
	r, err := h.call("POST", destination, OP_APPEND, nil)
	if err != nil {
		return err
	}
	if r.StatusCode != 307 {
		return errors.New("Invalid Response Header on OP_APPEND: " + r.Status)
	}

	location := r.Header["Location"][0]

	r, err = h.callPayload("POST", location, OP_APPEND, localfile, nil)
	if err != nil {
		return err
	}
	if r.StatusCode != 201 {
		return errors.New(r.Status)
	}
	return nil
}

func (h *WebHdfs) SetOwner(path string, owner string, group string) error {
	ownerInfo := map[string]string{}
	if owner != "" {
		ownerInfo["owner"] = owner
	}
	if group != "" {
		ownerInfo["group"] = group
	}
	r, e := h.call("PUT", path, OP_SETOWNER, ownerInfo)
	if e != nil {
		return e
	}
	if r.StatusCode != 200 {
		return errors.New("Invalid Response Header on OP_SETOWNER: " + r.Status)
	}
	return nil
}

func (h *WebHdfs) SetPermission(path string, permission string) error {
	if permission == "" {
		permission = "755"
	}

	parms := map[string]string{}
	parms["permission"] = permission

	r, e := h.call("PUT", path, OP_SETPERMISSION, parms)
	if e != nil {
		return e
	}
	if r.StatusCode != 200 {
		return errors.New("Invalid Response Header on OP_SETPERMISSION: " + r.Status)
	}
	return nil
}

/*
func (h *WebHdfs) CreateNewFile(path, filename, permission string) error {
	if permission == "" {
		permission = "755"
	}

	parms := map[string]string{}
	parms["permission"] = permission

	var fullpath string

	if string(path[len(path)-1]) == "/" {
		fullpath = path + filename
	} else {
		fullpath = path + "/" + filename
	}

	log.Println(fullpath)

	r, e := h.call("PUT", fullpath, OP_CREATE, parms)
	if e != nil {
		return e
	}
	if r.StatusCode != 200 {
		return errors.New("Invalid Response Header on OP_CREATE: " + r.Status)
	}
	return nil
}
*/
<|endoftext|>


<commit_before>// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package pgzip

import (
	"bytes"
	"compress/flate"
	"errors"
	"fmt"
	"github.com/klauspost/crc32"
	"hash"
	"io"
)

const (
	defaultBlockSize = 250000
	tailSize         = 16384
	defaultBlocks    = 16
)

// These constants are copied from the flate package, so that code that imports
// "compress/gzip" does not also have to import "compress/flate".
const (
	NoCompression      = flate.NoCompression
	BestSpeed          = flate.BestSpeed
	BestCompression    = flate.BestCompression
	DefaultCompression = flate.DefaultCompression
)

// A Writer is an io.WriteCloser.
// Writes to a Writer are compressed and written to w.
type Writer struct {
	Header
	w             io.Writer
	level         int
	wroteHeader   bool
	blockSize     int
	blocks        int
	currentBuffer []byte
	prevTail      []byte
	digest        hash.Hash32
	size          int
	closed        bool
	buf           [10]byte
	err           error
	pushedErr     chan error
	results       chan result
}

type result struct {
	result        chan []byte
	notifyWritten chan struct{}
}

// Use SetConcurrency to finetune the concurrency level if needed.
//
// With this you can control the approximate size of your blocks,
// as well as how many you want to be processing in parallel.
//
// Default values for this is SetConcurrency(250000, 16),
// meaning blocks are split at 250000 bytes and up to 16 blocks
// can be processing at once before the writer blocks.
func (z *Writer) SetConcurrency(blockSize, blocks int) error {
	if blockSize <= tailSize {
		return fmt.Errorf("gzip: block size cannot be less than or equal to %d", tailSize)
	}
	if blocks <= 0 {
		return errors.New("gzip: blocks cannot be zero or less")
	}
	z.blockSize = blockSize
	z.results = make(chan result, blocks)
	z.blocks = blocks
	return nil
}

// NewWriter returns a new Writer.
// Writes to the returned writer are compressed and written to w.
//
// It is the caller's responsibility to call Close on the WriteCloser when done.
// Writes may be buffered and not flushed until Close.
//
// Callers that wish to set the fields in Writer.Header must do so before
// the first call to Write or Close. The Comment and Name header fields are
// UTF-8 strings in Go, but the underlying format requires NUL-terminated ISO
// 8859-1 (Latin-1). NUL or non-Latin-1 runes in those strings will lead to an
// error on Write.
func NewWriter(w io.Writer) *Writer {
	z, _ := NewWriterLevel(w, DefaultCompression)
	return z
}

// NewWriterLevel is like NewWriter but specifies the compression level instead
// of assuming DefaultCompression.
//
// The compression level can be DefaultCompression, NoCompression, or any
// integer value between BestSpeed and BestCompression inclusive. The error
// returned will be nil if the level is valid.
func NewWriterLevel(w io.Writer, level int) (*Writer, error) {
	if level < DefaultCompression || level > BestCompression {
		return nil, fmt.Errorf("gzip: invalid compression level: %d", level)
	}
	z := new(Writer)
	z.SetConcurrency(defaultBlockSize, defaultBlocks)
	z.init(w, level)
	return z, nil
}

// This function must be used by goroutines to set an
// error condition, since z.err access is restricted
// to the callers goruotine.
func (z Writer) pushError(err error) {
	z.pushedErr <- err
	close(z.pushedErr)
}

func (z *Writer) init(w io.Writer, level int) {
	digest := z.digest
	if digest != nil {
		digest.Reset()
	} else {
		digest = crc32.NewIEEE()
	}

	*z = Writer{
		Header: Header{
			OS: 255, // unknown
		},
		w:         w,
		level:     level,
		digest:    digest,
		pushedErr: make(chan error, 1),
		results:   make(chan result, z.blocks),
		blockSize: z.blockSize,
		blocks:    z.blocks,
	}
}

// Reset discards the Writer z's state and makes it equivalent to the
// result of its original state from NewWriter or NewWriterLevel, but
// writing to w instead. This permits reusing a Writer rather than
// allocating a new one.
func (z *Writer) Reset(w io.Writer) {
	if z.results != nil && !z.closed {
		close(z.results)
	}
	z.SetConcurrency(defaultBlockSize, defaultBlocks)
	z.init(w, z.level)
}

// GZIP (RFC 1952) is little-endian, unlike ZLIB (RFC 1950).
func put2(p []byte, v uint16) {
	p[0] = uint8(v >> 0)
	p[1] = uint8(v >> 8)
}

func put4(p []byte, v uint32) {
	p[0] = uint8(v >> 0)
	p[1] = uint8(v >> 8)
	p[2] = uint8(v >> 16)
	p[3] = uint8(v >> 24)
}

// writeBytes writes a length-prefixed byte slice to z.w.
func (z *Writer) writeBytes(b []byte) error {
	if len(b) > 0xffff {
		return errors.New("gzip.Write: Extra data is too large")
	}
	put2(z.buf[0:2], uint16(len(b)))
	_, err := z.w.Write(z.buf[0:2])
	if err != nil {
		return err
	}
	_, err = z.w.Write(b)
	return err
}

// writeString writes a UTF-8 string s in GZIP's format to z.w.
// GZIP (RFC 1952) specifies that strings are NUL-terminated ISO 8859-1 (Latin-1).
func (z *Writer) writeString(s string) (err error) {
	// GZIP stores Latin-1 strings; error if non-Latin-1; convert if non-ASCII.
	needconv := false
	for _, v := range s {
		if v == 0 || v > 0xff {
			return errors.New("gzip.Write: non-Latin-1 header string")
		}
		if v > 0x7f {
			needconv = true
		}
	}
	if needconv {
		b := make([]byte, 0, len(s))
		for _, v := range s {
			b = append(b, byte(v))
		}
		_, err = z.w.Write(b)
	} else {
		_, err = io.WriteString(z.w, s)
	}
	if err != nil {
		return err
	}
	// GZIP strings are NUL-terminated.
	z.buf[0] = 0
	_, err = z.w.Write(z.buf[0:1])
	return err
}

// compressCurrent will compress the data currently buffered
// This should only be called from the main writer/flush/closer
func (z *Writer) compressCurrent(flush bool) {
	r := result{}
	r.result = make(chan []byte, 1)
	r.notifyWritten = make(chan struct{}, 0)
	z.results <- r

	// If block given is more than twice the block size, split it.
	c := z.currentBuffer
	if len(c) > z.blockSize*2 {
		c = c[:z.blockSize]
		go compressBlock(c, z.prevTail, *z, r)
		z.prevTail = c[len(c)-tailSize:]
		z.currentBuffer = z.currentBuffer[z.blockSize:]
		z.compressCurrent(flush)
		// Last one flushes if needed
		return
	}

	go compressBlock(c, z.prevTail, *z, r)
	if len(c) > tailSize {
		z.prevTail = c[len(c)-tailSize:]
	} else {
		z.prevTail = nil
	}
	z.currentBuffer = make([]byte, 0, z.blockSize+(z.blockSize/4))

	// Wait if flushing
	if flush {
		_ = <-r.notifyWritten
	}
}

// Returns an error if it has been set.
// Cannot be used by functions that are from internal goroutines.
func (z *Writer) checkError() error {
	if z.err != nil {
		return z.err
	}
	select {
	case err := <-z.pushedErr:
		z.err = err
		close(z.pushedErr)
	default:
	}
	return z.err
}

// Write writes a compressed form of p to the underlying io.Writer. The
// compressed bytes are not necessarily flushed to output until
// the Writer is closed or Flush() is called.
//
// The function will return quickly, if there are unused buffers.
// The sent slice (p) is copied, and the caller is free to re-use the buffer
// when the function returns.
//
// Errors that occur during compression will be reported later, and a nil error
// does not signify that the compression succeeded (since it is most likely still running)
// That means that the call that returns an error may not be the call that caused it.
// Only Flush and Close functions are guaranteed to return any errors up to that point.
func (z *Writer) Write(p []byte) (int, error) {
	if z.checkError() != nil {
		return 0, z.err
	}
	// Write the GZIP header lazily.
	if !z.wroteHeader {
		z.wroteHeader = true
		z.buf[0] = gzipID1
		z.buf[1] = gzipID2
		z.buf[2] = gzipDeflate
		z.buf[3] = 0
		if z.Extra != nil {
			z.buf[3] |= 0x04
		}
		if z.Name != "" {
			z.buf[3] |= 0x08
		}
		if z.Comment != "" {
			z.buf[3] |= 0x10
		}
		put4(z.buf[4:8], uint32(z.ModTime.Unix()))
		if z.level == BestCompression {
			z.buf[8] = 2
		} else if z.level == BestSpeed {
			z.buf[8] = 4
		} else {
			z.buf[8] = 0
		}
		z.buf[9] = z.OS
		var n int
		n, z.err = z.w.Write(z.buf[0:10])
		if z.err != nil {
			return n, z.err
		}
		if z.Extra != nil {
			z.err = z.writeBytes(z.Extra)
			if z.err != nil {
				return n, z.err
			}
		}
		if z.Name != "" {
			z.err = z.writeString(z.Name)
			if z.err != nil {
				return n, z.err
			}
		}
		if z.Comment != "" {
			z.err = z.writeString(z.Comment)
			if z.err != nil {
				return n, z.err
			}
		}
		// Start receiving data from compressors
		go func() {
			listen := z.results
			for {
				r, ok := <-listen
				// If closed, we are finished.
				if !ok {
					return
				}
				buf := <-r.result
				n, err := z.w.Write(buf)
				if err != nil {
					z.pushError(err)
					close(r.notifyWritten)
					return
				}
				if n != len(buf) {
					z.pushError(fmt.Errorf("gzip: short write %d should be %d", n, len(buf)))
					close(r.notifyWritten)
					return
				}
				close(r.notifyWritten)
			}
		}()
		z.currentBuffer = make([]byte, 0, z.blockSize+(z.blockSize/4))
	}
	z.size += len(p)
	z.digest.Write(p)
	z.currentBuffer = append(z.currentBuffer, p...)
	if len(z.currentBuffer) >= z.blockSize {
		z.compressCurrent(false)
	}
	return len(p), z.err
}

// Step 1: compresses buffer to buffer
// Step 2: send writer to channel
// Step 3: Close result channel to indicate we are done
func compressBlock(p, prevTail []byte, z Writer, r result) {
	defer close(r.result)
	buf := make([]byte, 0, len(p))
	dest := bytes.NewBuffer(buf)

	var compressor *flate.Writer
	var err error
	if len(prevTail) > 0 {
		compressor, err = flate.NewWriterDict(dest, z.level, prevTail)
	} else {
		compressor, err = flate.NewWriter(dest, z.level)
	}
	if err != nil {
		z.pushError(err)
		return
	}
	compressor.Write(p)

	err = compressor.Flush()
	if err != nil {
		z.pushError(err)
		return
	}
	if z.closed {
		err = compressor.Close()
		if err != nil {
			z.pushError(err)
			return
		}
	}
	// Read back buffer
	buf = dest.Bytes()
	r.result <- buf
}

// Flush flushes any pending compressed data to the underlying writer.
//
// It is useful mainly in compressed network protocols, to ensure that
// a remote reader has enough data to reconstruct a packet. Flush does
// not return until the data has been written. If the underlying
// writer returns an error, Flush returns that error.
//
// In the terminology of the zlib library, Flush is equivalent to Z_SYNC_FLUSH.
func (z *Writer) Flush() error {
	if z.checkError() != nil {
		return z.err
	}
	if z.closed {
		return nil
	}
	if !z.wroteHeader {
		z.Write(nil)
		if z.err != nil {
			return z.err
		}
	}
	// We send current block to compression
	z.compressCurrent(true)
	if z.checkError() != nil {
		return z.err
	}

	return nil
}

// UncompressedSize will return the number of bytes written.
// pgzip only, not a function in the official gzip package.
func (z Writer) UncompressedSize() int {
	return z.size
}

// Close closes the Writer, flushing any unwritten data to the underlying
// io.Writer, but does not close the underlying io.Writer.
func (z *Writer) Close() error {
	if z.checkError() != nil {
		return z.err
	}
	if z.closed {
		return nil
	}

	z.closed = true
	if !z.wroteHeader {
		z.Write(nil)
		if z.err != nil {
			return z.err
		}
	}
	z.compressCurrent(true)
	if z.checkError() != nil {
		return z.err
	}
	close(z.results)
	put4(z.buf[0:4], z.digest.Sum32())
	put4(z.buf[4:8], uint32(z.size))
	_, z.err = z.w.Write(z.buf[0:8])
	return z.err
}
<commit_msg>Use new optimized deflater package.<commit_after>// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package pgzip

import (
	"bytes"
	"errors"
	"fmt"
	"github.com/klauspost/compress/flate"
	"github.com/klauspost/crc32"
	"hash"
	"io"
)

const (
	defaultBlockSize = 250000
	tailSize         = 16384
	defaultBlocks    = 16
)

// These constants are copied from the flate package, so that code that imports
// "compress/gzip" does not also have to import "compress/flate".
const (
	NoCompression      = flate.NoCompression
	BestSpeed          = flate.BestSpeed
	BestCompression    = flate.BestCompression
	DefaultCompression = flate.DefaultCompression
)

// A Writer is an io.WriteCloser.
// Writes to a Writer are compressed and written to w.
type Writer struct {
	Header
	w             io.Writer
	level         int
	wroteHeader   bool
	blockSize     int
	blocks        int
	currentBuffer []byte
	prevTail      []byte
	digest        hash.Hash32
	size          int
	closed        bool
	buf           [10]byte
	err           error
	pushedErr     chan error
	results       chan result
}

type result struct {
	result        chan []byte
	notifyWritten chan struct{}
}

// Use SetConcurrency to finetune the concurrency level if needed.
//
// With this you can control the approximate size of your blocks,
// as well as how many you want to be processing in parallel.
//
// Default values for this is SetConcurrency(250000, 16),
// meaning blocks are split at 250000 bytes and up to 16 blocks
// can be processing at once before the writer blocks.
func (z *Writer) SetConcurrency(blockSize, blocks int) error {
	if blockSize <= tailSize {
		return fmt.Errorf("gzip: block size cannot be less than or equal to %d", tailSize)
	}
	if blocks <= 0 {
		return errors.New("gzip: blocks cannot be zero or less")
	}
	z.blockSize = blockSize
	z.results = make(chan result, blocks)
	z.blocks = blocks
	return nil
}

// NewWriter returns a new Writer.
// Writes to the returned writer are compressed and written to w.
//
// It is the caller's responsibility to call Close on the WriteCloser when done.
// Writes may be buffered and not flushed until Close.
//
// Callers that wish to set the fields in Writer.Header must do so before
// the first call to Write or Close. The Comment and Name header fields are
// UTF-8 strings in Go, but the underlying format requires NUL-terminated ISO
// 8859-1 (Latin-1). NUL or non-Latin-1 runes in those strings will lead to an
// error on Write.
func NewWriter(w io.Writer) *Writer {
	z, _ := NewWriterLevel(w, DefaultCompression)
	return z
}

// NewWriterLevel is like NewWriter but specifies the compression level instead
// of assuming DefaultCompression.
//
// The compression level can be DefaultCompression, NoCompression, or any
// integer value between BestSpeed and BestCompression inclusive. The error
// returned will be nil if the level is valid.
func NewWriterLevel(w io.Writer, level int) (*Writer, error) {
	if level < DefaultCompression || level > BestCompression {
		return nil, fmt.Errorf("gzip: invalid compression level: %d", level)
	}
	z := new(Writer)
	z.SetConcurrency(defaultBlockSize, defaultBlocks)
	z.init(w, level)
	return z, nil
}

// This function must be used by goroutines to set an
// error condition, since z.err access is restricted
// to the callers goruotine.
func (z Writer) pushError(err error) {
	z.pushedErr <- err
	close(z.pushedErr)
}

func (z *Writer) init(w io.Writer, level int) {
	digest := z.digest
	if digest != nil {
		digest.Reset()
	} else {
		digest = crc32.NewIEEE()
	}

	*z = Writer{
		Header: Header{
			OS: 255, // unknown
		},
		w:         w,
		level:     level,
		digest:    digest,
		pushedErr: make(chan error, 1),
		results:   make(chan result, z.blocks),
		blockSize: z.blockSize,
		blocks:    z.blocks,
	}
}

// Reset discards the Writer z's state and makes it equivalent to the
// result of its original state from NewWriter or NewWriterLevel, but
// writing to w instead. This permits reusing a Writer rather than
// allocating a new one.
func (z *Writer) Reset(w io.Writer) {
	if z.results != nil && !z.closed {
		close(z.results)
	}
	z.SetConcurrency(defaultBlockSize, defaultBlocks)
	z.init(w, z.level)
}

// GZIP (RFC 1952) is little-endian, unlike ZLIB (RFC 1950).
func put2(p []byte, v uint16) {
	p[0] = uint8(v >> 0)
	p[1] = uint8(v >> 8)
}

func put4(p []byte, v uint32) {
	p[0] = uint8(v >> 0)
	p[1] = uint8(v >> 8)
	p[2] = uint8(v >> 16)
	p[3] = uint8(v >> 24)
}

// writeBytes writes a length-prefixed byte slice to z.w.
func (z *Writer) writeBytes(b []byte) error {
	if len(b) > 0xffff {
		return errors.New("gzip.Write: Extra data is too large")
	}
	put2(z.buf[0:2], uint16(len(b)))
	_, err := z.w.Write(z.buf[0:2])
	if err != nil {
		return err
	}
	_, err = z.w.Write(b)
	return err
}

// writeString writes a UTF-8 string s in GZIP's format to z.w.
// GZIP (RFC 1952) specifies that strings are NUL-terminated ISO 8859-1 (Latin-1).
func (z *Writer) writeString(s string) (err error) {
	// GZIP stores Latin-1 strings; error if non-Latin-1; convert if non-ASCII.
	needconv := false
	for _, v := range s {
		if v == 0 || v > 0xff {
			return errors.New("gzip.Write: non-Latin-1 header string")
		}
		if v > 0x7f {
			needconv = true
		}
	}
	if needconv {
		b := make([]byte, 0, len(s))
		for _, v := range s {
			b = append(b, byte(v))
		}
		_, err = z.w.Write(b)
	} else {
		_, err = io.WriteString(z.w, s)
	}
	if err != nil {
		return err
	}
	// GZIP strings are NUL-terminated.
	z.buf[0] = 0
	_, err = z.w.Write(z.buf[0:1])
	return err
}

// compressCurrent will compress the data currently buffered
// This should only be called from the main writer/flush/closer
func (z *Writer) compressCurrent(flush bool) {
	r := result{}
	r.result = make(chan []byte, 1)
	r.notifyWritten = make(chan struct{}, 0)
	z.results <- r

	// If block given is more than twice the block size, split it.
	c := z.currentBuffer
	if len(c) > z.blockSize*2 {
		c = c[:z.blockSize]
		go compressBlock(c, z.prevTail, *z, r)
		z.prevTail = c[len(c)-tailSize:]
		z.currentBuffer = z.currentBuffer[z.blockSize:]
		z.compressCurrent(flush)
		// Last one flushes if needed
		return
	}

	go compressBlock(c, z.prevTail, *z, r)
	if len(c) > tailSize {
		z.prevTail = c[len(c)-tailSize:]
	} else {
		z.prevTail = nil
	}
	z.currentBuffer = make([]byte, 0, z.blockSize+(z.blockSize/4))

	// Wait if flushing
	if flush {
		_ = <-r.notifyWritten
	}
}

// Returns an error if it has been set.
// Cannot be used by functions that are from internal goroutines.
func (z *Writer) checkError() error {
	if z.err != nil {
		return z.err
	}
	select {
	case err := <-z.pushedErr:
		z.err = err
		close(z.pushedErr)
	default:
	}
	return z.err
}

// Write writes a compressed form of p to the underlying io.Writer. The
// compressed bytes are not necessarily flushed to output until
// the Writer is closed or Flush() is called.
//
// The function will return quickly, if there are unused buffers.
// The sent slice (p) is copied, and the caller is free to re-use the buffer
// when the function returns.
//
// Errors that occur during compression will be reported later, and a nil error
// does not signify that the compression succeeded (since it is most likely still running)
// That means that the call that returns an error may not be the call that caused it.
// Only Flush and Close functions are guaranteed to return any errors up to that point.
func (z *Writer) Write(p []byte) (int, error) {
	if z.checkError() != nil {
		return 0, z.err
	}
	// Write the GZIP header lazily.
	if !z.wroteHeader {
		z.wroteHeader = true
		z.buf[0] = gzipID1
		z.buf[1] = gzipID2
		z.buf[2] = gzipDeflate
		z.buf[3] = 0
		if z.Extra != nil {
			z.buf[3] |= 0x04
		}
		if z.Name != "" {
			z.buf[3] |= 0x08
		}
		if z.Comment != "" {
			z.buf[3] |= 0x10
		}
		put4(z.buf[4:8], uint32(z.ModTime.Unix()))
		if z.level == BestCompression {
			z.buf[8] = 2
		} else if z.level == BestSpeed {
			z.buf[8] = 4
		} else {
			z.buf[8] = 0
		}
		z.buf[9] = z.OS
		var n int
		n, z.err = z.w.Write(z.buf[0:10])
		if z.err != nil {
			return n, z.err
		}
		if z.Extra != nil {
			z.err = z.writeBytes(z.Extra)
			if z.err != nil {
				return n, z.err
			}
		}
		if z.Name != "" {
			z.err = z.writeString(z.Name)
			if z.err != nil {
				return n, z.err
			}
		}
		if z.Comment != "" {
			z.err = z.writeString(z.Comment)
			if z.err != nil {
				return n, z.err
			}
		}
		// Start receiving data from compressors
		go func() {
			listen := z.results
			for {
				r, ok := <-listen
				// If closed, we are finished.
				if !ok {
					return
				}
				buf := <-r.result
				n, err := z.w.Write(buf)
				if err != nil {
					z.pushError(err)
					close(r.notifyWritten)
					return
				}
				if n != len(buf) {
					z.pushError(fmt.Errorf("gzip: short write %d should be %d", n, len(buf)))
					close(r.notifyWritten)
					return
				}
				close(r.notifyWritten)
			}
		}()
		z.currentBuffer = make([]byte, 0, z.blockSize+(z.blockSize/4))
	}
	z.size += len(p)
	z.digest.Write(p)
	z.currentBuffer = append(z.currentBuffer, p...)
	if len(z.currentBuffer) >= z.blockSize {
		z.compressCurrent(false)
	}
	return len(p), z.err
}

// Step 1: compresses buffer to buffer
// Step 2: send writer to channel
// Step 3: Close result channel to indicate we are done
func compressBlock(p, prevTail []byte, z Writer, r result) {
	defer close(r.result)
	buf := make([]byte, 0, len(p))
	dest := bytes.NewBuffer(buf)

	var compressor *flate.Writer
	var err error
	if len(prevTail) > 0 {
		compressor, err = flate.NewWriterDict(dest, z.level, prevTail)
	} else {
		compressor, err = flate.NewWriter(dest, z.level)
	}
	if err != nil {
		z.pushError(err)
		return
	}
	compressor.Write(p)

	err = compressor.Flush()
	if err != nil {
		z.pushError(err)
		return
	}
	if z.closed {
		err = compressor.Close()
		if err != nil {
			z.pushError(err)
			return
		}
	}
	// Read back buffer
	buf = dest.Bytes()
	r.result <- buf
}

// Flush flushes any pending compressed data to the underlying writer.
//
// It is useful mainly in compressed network protocols, to ensure that
// a remote reader has enough data to reconstruct a packet. Flush does
// not return until the data has been written. If the underlying
// writer returns an error, Flush returns that error.
//
// In the terminology of the zlib library, Flush is equivalent to Z_SYNC_FLUSH.
func (z *Writer) Flush() error {
	if z.checkError() != nil {
		return z.err
	}
	if z.closed {
		return nil
	}
	if !z.wroteHeader {
		z.Write(nil)
		if z.err != nil {
			return z.err
		}
	}
	// We send current block to compression
	z.compressCurrent(true)
	if z.checkError() != nil {
		return z.err
	}

	return nil
}

// UncompressedSize will return the number of bytes written.
// pgzip only, not a function in the official gzip package.
func (z Writer) UncompressedSize() int {
	return z.size
}

// Close closes the Writer, flushing any unwritten data to the underlying
// io.Writer, but does not close the underlying io.Writer.
func (z *Writer) Close() error {
	if z.checkError() != nil {
		return z.err
	}
	if z.closed {
		return nil
	}

	z.closed = true
	if !z.wroteHeader {
		z.Write(nil)
		if z.err != nil {
			return z.err
		}
	}
	z.compressCurrent(true)
	if z.checkError() != nil {
		return z.err
	}
	close(z.results)
	put4(z.buf[0:4], z.digest.Sum32())
	put4(z.buf[4:8], uint32(z.size))
	_, z.err = z.w.Write(z.buf[0:8])
	return z.err
}
<|endoftext|>


<commit_before>package help

import (
	"bytes"
	"text/template"
)

type Resource struct {
	Name     string
	Commands []Argument
}

type Command struct {
	Brief     []string
	Arguments []Argument
}

type Argument struct {
	Name        string
	Description []string
}

var resourceHelpTemplate = `Available {{.Name}} commands:
{{range .Commands}}
	{{.Name}}
{{range .Description}}{{ printf "\t\t" }}{{ . }}{{ printf "\n" }}{{end}}
{{end}}`

var commandHelpTemplate = `{{range .Brief}}{{ . }}{{ printf " " }}{{ end }}

{{if .Arguments}}PARAMETERS:
{{range .Arguments}}
	{{.Name}}
{{range .Description}} {{ printf "\t\t" }} {{ . }} {{ printf "\n" }} {{end}}
{{end}}
{{end}}OPTIONS:

	--help
			Shows general help or help for the given resource or command.
	--user
			Specifies a user name for the account.
	--password
			Specifies the password for the given user.
	--profile
			Specifies a profile to use (one from the config file).
	--output
			Specifies the output format - either 'json', 'text' or 'table'.
			Defaults to 'json'.
	--generate-cli-skeleton
			If specified, the command is not actually executed.
			Instead, all of the specified arguments and options except from this one
			are returned in JSON format.
	--from-file
			Specifies a JSON file to load command arguments and options from. No other
			arguments or options can be specified.
	--query
			Restricts the fields in the result to only those specified with this option.

			Multiple fields can be separated by comma. Nested fields can be queried using dot.
			Multiple nested fields are also separated by comma and must be enclosed in curly
			braces on the deepest level of nesting. Note, that a shell may treat curly braces
			in a special way so put the whole query in quotes to avoid errors.
			Aliases may be set for the nested fields using semicolons inside the braces.

			An example:
				clc server list --query "details.IP-addresses.{I:internal,P:public}"
	--filter
			Filters out the returned entities that do not match the given conditions. Multiple
			conditions are separated via comma. Each condition consists of a field, an
			operation and a value. Supported operations are:

			=	equals, applicable to strings, numbers and booleans
			^= (starts with), $= (ends with), ~= (contains)	these three are only for strings
			<,<=,>,>= 	comparison operators, can be used with numbers and strings
	--trace
			If specified, prints out all the HTTP request/response data.

ENVIRONMENT VARIABLES:

	CLC_USER	Specifies a user name for the account.
	CLC_PASSWORD	Specifies the password for the given user.
	CLC_PROFILE	Specifies a profile to use (one from the config file).
	CLC_TRACE	If specified (any non-empty value fits), prints out all the HTTP request/response data.
`

func ForCommand(cmd Command) string {
	tmpl, err := template.New("command help").Parse(commandHelpTemplate)
	if err != nil {
		panic(err)
	}
	buf := bytes.NewBuffer([]byte{})
	err = tmpl.Execute(buf, cmd)
	if err != nil {
		panic(err)
	}
	return string(buf.Bytes())
}

func ForResource(r Resource) string {
	tmpl, err := template.New("resource help").Parse(resourceHelpTemplate)
	if err != nil {
		panic(err)
	}
	buf := bytes.NewBuffer([]byte{})
	err = tmpl.Execute(buf, r)
	if err != nil {
		panic(err)
	}
	return string(buf.Bytes())
}
<commit_msg>Make the environment variables help conditional<commit_after>package help

import (
	"bytes"
	"text/template"
)

type Resource struct {
	Name     string
	Commands []Argument
}

type Command struct {
	Brief     []string
	Arguments []Argument
	NoEnvVars bool
}

type Argument struct {
	Name        string
	Description []string
}

var resourceHelpTemplate = `Available {{.Name}} commands:
{{range .Commands}}
	{{.Name}}
{{range .Description}}{{ printf "\t\t" }}{{ . }}{{ printf "\n" }}{{end}}
{{end}}`

var commandHelpTemplate = `{{range .Brief}}{{ . }}{{ printf " " }}{{ end }}

{{if .Arguments}}PARAMETERS:
{{range .Arguments}}
	{{.Name}}
{{range .Description}} {{ printf "\t\t" }} {{ . }} {{ printf "\n" }} {{end}}
{{end}}
{{end}}OPTIONS:

	--help
			Shows general help or help for the given resource or command.
	--user
			Specifies a user name for the account.
	--password
			Specifies the password for the given user.
	--profile
			Specifies a profile to use (one from the config file).
	--output
			Specifies the output format - either 'json', 'text' or 'table'.
			Defaults to 'json'.
	--generate-cli-skeleton
			If specified, the command is not actually executed.
			Instead, all of the specified arguments and options except from this one
			are returned in JSON format.
	--from-file
			Specifies a JSON file to load command arguments and options from. No other
			arguments or options can be specified.
	--query
			Restricts the fields in the result to only those specified with this option.

			Multiple fields can be separated by comma. Nested fields can be queried using dot.
			Multiple nested fields are also separated by comma and must be enclosed in curly
			braces on the deepest level of nesting. Note, that a shell may treat curly braces
			in a special way so put the whole query in quotes to avoid errors.
			Aliases may be set for the nested fields using semicolons inside the braces.

			An example:
				clc server list --query "details.IP-addresses.{I:internal,P:public}"
	--filter
			Filters out the returned entities that do not match the given conditions. Multiple
			conditions are separated via comma. Each condition consists of a field, an
			operation and a value. Supported operations are:

			=	equals, applicable to strings, numbers and booleans
			^= (starts with), $= (ends with), ~= (contains)	these three are only for strings
			<,<=,>,>= 	comparison operators, can be used with numbers and strings
	--trace
			If specified, prints out all the HTTP request/response data.
{{if .NoEnvVars}}{{else}}
ENVIRONMENT VARIABLES:

	CLC_USER	Specifies a user name for the account.
	CLC_PASSWORD	Specifies the password for the given user.
	CLC_PROFILE	Specifies a profile to use (one from the config file).
	CLC_TRACE	If specified (any non-empty value fits), prints out all the HTTP request/response data.
{{end}}`

func ForCommand(cmd Command) string {
	tmpl, err := template.New("command help").Parse(commandHelpTemplate)
	if err != nil {
		panic(err)
	}
	buf := bytes.NewBuffer([]byte{})
	err = tmpl.Execute(buf, cmd)
	if err != nil {
		panic(err)
	}
	return string(buf.Bytes())
}

func ForResource(r Resource) string {
	tmpl, err := template.New("resource help").Parse(resourceHelpTemplate)
	if err != nil {
		panic(err)
	}
	buf := bytes.NewBuffer([]byte{})
	err = tmpl.Execute(buf, r)
	if err != nil {
		panic(err)
	}
	return string(buf.Bytes())
}
<|endoftext|>


<commit_before>package main

import (
	"compress/gzip"
	"encoding/base32"
	"encoding/binary"
	"encoding/gob"
	"log"
	"os"
	"path/filepath"
	"sync"
	"time"
)

type Player struct {
	ID uint64
	Hero
	ZoneX, ZoneY int64
	TileX, TileY uint8
	hud          interface {
		Paint(func(int, int, rune, Color))
		Key(int) bool
	}
	repaint chan struct{}

	Joined    time.Time
	LastLogin time.Time
	Admin     bool

	zone *Zone
}

func (p *Player) Move(dx, dy int) {
	if p.Delay > 0 {
		return
	}
	destX := dx + int(p.TileX)
	destY := dy + int(p.TileY)

	zoneChange := destX < 0 || destY < 0 || destX > 255 || destY > 255

	p.lock.Lock()
	z := p.zone
	p.lock.Unlock()
	z.Lock()
	if !zoneChange {
		zoneChange = z.Tile(uint8(destX), uint8(destY)) == nil
	}
	if !zoneChange && z.Blocked(uint8(destX), uint8(destY)) {
		z.Unlock()
		return
	}
	z.Tile(p.TileX, p.TileY).Remove(p)
	z.Unlock()
	if zoneChange {
		p.RepaintZone()
		ReleaseZone(z)
		p.lock.Lock()
		p.Delay = 2
		if destY < 0 {
			p.ZoneY -= 2
			p.TileX = 127
			p.TileY = 255
		} else if destY > 255 {
			p.ZoneY += 2
			p.TileX = 127
			p.TileY = 0
		} else if destX < 128 {
			if p.ZoneY & 1 == 1 {
				p.ZoneX--
			}
			if destY < 128 {
				p.ZoneY--
				p.TileX = 255 - zoneOffset[255-64]
				p.TileY = 255 - 64
			} else {
				p.ZoneY++
				p.TileX = 255 - zoneOffset[64]
				p.TileY = 64
			}
		} else {
			if p.ZoneY & 1 == 1 {
				p.ZoneX++
			}
			if destY < 128 {
				p.ZoneY--
				p.TileX = zoneOffset[255-64]
				p.TileY = 255 - 64
			} else {
				p.ZoneY++
				p.TileX = zoneOffset[64]
				p.TileY = 64
			}
		}
		z = GrabZone(p.ZoneX, p.ZoneY)
		p.zone = z
		p.lock.Unlock()
		p.Save()
		p.hud = nil
	} else {
		p.lock.Lock()
		p.TileX = uint8(destX)
		p.TileY = uint8(destY)
		p.Delay = 2
		p.lock.Unlock()
	}
	z.Lock()
	z.Tile(p.TileX, p.TileY).Add(p)
	z.Unlock()
	p.RepaintZone()
}

func playerFilename(id uint64) string {
	var buf [binary.MaxVarintLen64]byte
	i := binary.PutUvarint(buf[:], id)
	encoded := base32.StdEncoding.EncodeToString(buf[:i])

	l := len(encoded)
	for encoded[l-1] == '=' {
		l--
	}
	return "p" + encoded[:l] + ".gz"
}

func (p *Player) Save() {
	p.lock.Lock()
	defer p.lock.Unlock()

	dir := seedFilename()
	os.MkdirAll(dir, 0755)

	f, err := os.Create(filepath.Join(dir, playerFilename(p.ID)))
	if err != nil {
		log.Printf("[save:%d] %v", p.ID, err)
		return
	}
	defer f.Close()

	g, err := gzip.NewWriterLevel(f, gzip.BestCompression)
	if err != nil {
		log.Printf("[save:%d] %v", p.ID, err)
		return
	}
	defer g.Close()

	err = gob.NewEncoder(g).Encode(p)
	if err != nil {
		log.Printf("[save:%d] %v", p.ID, err)
	}
}

func LoadPlayer(id uint64) (*Player, error) {
	dir := seedFilename()

	f, err := os.Open(filepath.Join(dir, playerFilename(id)))
	if err != nil {
		return nil, err
	}
	defer f.Close()

	g, err := gzip.NewReader(f)
	if err != nil {
		return nil, err
	}
	defer g.Close()

	d := gob.NewDecoder(g)
	var p Player
	err = d.Decode(&p)
	if err != nil {
		return nil, err
	}
	p.repaint = make(chan struct{}, 1)
	return &p, nil
}

func (p *Player) Repaint() {
	select {
	case p.repaint <- struct{}{}:
	default:
	}
}

func (p *Player) RepaintZone() {
	go func() {
		p.lock.Lock()
		p.zone.Repaint()
		p.lock.Unlock()
	}()
}

func (p *Player) Paint() (rune, Color) {
	if p.Admin {
		return '♚', "#fa0"
	}
	return p.Hero.Paint()
}

func (p *Player) Think() {
	p.think(false)
}

type ZoneEntryHUD string

func (h ZoneEntryHUD) Paint(setcell func(int, int, rune, Color)) {
	i := 0
	for _, r := range h {
		setcell(i, 0, r, "#fff")
		i++
	}
}

func (h ZoneEntryHUD) Key(code int) bool {
	return false
}

type Hero struct {
	Name_ *Name

	lock     sync.Mutex
	Delay    uint
	Backpack []Object
}

func (h *Hero) Name() string {
	return h.Name_.String()
}

func (h *Hero) Examine() string {
	return "a hero."
}

func (h *Hero) Blocking() bool {
	return false
}

func (h *Hero) Paint() (rune, Color) {
	return '☻', "#fff"
}

func (h *Hero) Think() {
	h.think(true)
}

func (h *Hero) think(ai bool) {
	h.lock.Lock()
	defer h.lock.Unlock()

	if h.Delay > 0 {
		h.Delay--
		return
	}
}

func (h *Hero) InteractOptions() []string {
	return nil
}

func (h *Hero) GiveItem(o Object) {
	h.Backpack = append(h.Backpack, o)
}
<commit_msg>improve zone changes further<commit_after>package main

import (
	"compress/gzip"
	"encoding/base32"
	"encoding/binary"
	"encoding/gob"
	"log"
	"os"
	"path/filepath"
	"sync"
	"time"
)

type Player struct {
	ID uint64
	Hero
	ZoneX, ZoneY int64
	TileX, TileY uint8
	hud          interface {
		Paint(func(int, int, rune, Color))
		Key(int) bool
	}
	repaint chan struct{}

	Joined    time.Time
	LastLogin time.Time
	Admin     bool

	zone *Zone
}

func (p *Player) Move(dx, dy int) {
	if p.Delay > 0 {
		return
	}
	destX := dx + int(p.TileX)
	destY := dy + int(p.TileY)

	zoneChange := destX < 0 || destY < 0 || destX > 255 || destY > 255

	p.lock.Lock()
	z := p.zone
	p.lock.Unlock()
	z.Lock()
	if !zoneChange {
		zoneChange = z.Tile(uint8(destX), uint8(destY)) == nil
	}
	if !zoneChange && z.Blocked(uint8(destX), uint8(destY)) {
		z.Unlock()
		return
	}
	z.Tile(p.TileX, p.TileY).Remove(p)
	z.Unlock()
	if zoneChange {
		p.RepaintZone()
		ReleaseZone(z)
		p.lock.Lock()
		p.Delay = 2
		if destY < 0 {
			p.ZoneY -= 2
			p.TileY = 255
		} else if destY > 255 {
			p.ZoneY += 2
			p.TileY = 0
		} else if destX < 128 {
			p.TileX = 255 - p.TileX
			p.TileY = 255 - p.TileY
			if p.ZoneY&1 == 0 {
				p.ZoneX--
			}
			if destY < 128 {
				p.ZoneY--
			} else {
				p.ZoneY++
			}
		} else {
			p.TileX = 255 - p.TileX
			p.TileY = 255 - p.TileY
			if p.ZoneY&1 == 1 {
				p.ZoneX++
			}
			if destY < 128 {
				p.ZoneY--
			} else {
				p.ZoneY++
			}
		}
		z = GrabZone(p.ZoneX, p.ZoneY)
		p.zone = z
		p.lock.Unlock()
		p.Save()
		p.hud = nil
	} else {
		p.lock.Lock()
		p.TileX = uint8(destX)
		p.TileY = uint8(destY)
		p.Delay = 2
		p.lock.Unlock()
	}
	z.Lock()
	z.Tile(p.TileX, p.TileY).Add(p)
	z.Unlock()
	p.RepaintZone()
}

func playerFilename(id uint64) string {
	var buf [binary.MaxVarintLen64]byte
	i := binary.PutUvarint(buf[:], id)
	encoded := base32.StdEncoding.EncodeToString(buf[:i])

	l := len(encoded)
	for encoded[l-1] == '=' {
		l--
	}
	return "p" + encoded[:l] + ".gz"
}

func (p *Player) Save() {
	p.lock.Lock()
	defer p.lock.Unlock()

	dir := seedFilename()
	os.MkdirAll(dir, 0755)

	f, err := os.Create(filepath.Join(dir, playerFilename(p.ID)))
	if err != nil {
		log.Printf("[save:%d] %v", p.ID, err)
		return
	}
	defer f.Close()

	g, err := gzip.NewWriterLevel(f, gzip.BestCompression)
	if err != nil {
		log.Printf("[save:%d] %v", p.ID, err)
		return
	}
	defer g.Close()

	err = gob.NewEncoder(g).Encode(p)
	if err != nil {
		log.Printf("[save:%d] %v", p.ID, err)
	}
}

func LoadPlayer(id uint64) (*Player, error) {
	dir := seedFilename()

	f, err := os.Open(filepath.Join(dir, playerFilename(id)))
	if err != nil {
		return nil, err
	}
	defer f.Close()

	g, err := gzip.NewReader(f)
	if err != nil {
		return nil, err
	}
	defer g.Close()

	d := gob.NewDecoder(g)
	var p Player
	err = d.Decode(&p)
	if err != nil {
		return nil, err
	}
	p.repaint = make(chan struct{}, 1)
	return &p, nil
}

func (p *Player) Repaint() {
	select {
	case p.repaint <- struct{}{}:
	default:
	}
}

func (p *Player) RepaintZone() {
	go func() {
		p.lock.Lock()
		p.zone.Repaint()
		p.lock.Unlock()
	}()
}

func (p *Player) Paint() (rune, Color) {
	if p.Admin {
		return '♚', "#fa0"
	}
	return p.Hero.Paint()
}

func (p *Player) Think() {
	p.think(false)
}

type ZoneEntryHUD string

func (h ZoneEntryHUD) Paint(setcell func(int, int, rune, Color)) {
	i := 0
	for _, r := range h {
		setcell(i, 0, r, "#fff")
		i++
	}
}

func (h ZoneEntryHUD) Key(code int) bool {
	return false
}

type Hero struct {
	Name_ *Name

	lock     sync.Mutex
	Delay    uint
	Backpack []Object
}

func (h *Hero) Name() string {
	return h.Name_.String()
}

func (h *Hero) Examine() string {
	return "a hero."
}

func (h *Hero) Blocking() bool {
	return false
}

func (h *Hero) Paint() (rune, Color) {
	return '☻', "#fff"
}

func (h *Hero) Think() {
	h.think(true)
}

func (h *Hero) think(ai bool) {
	h.lock.Lock()
	defer h.lock.Unlock()

	if h.Delay > 0 {
		h.Delay--
		return
	}
}

func (h *Hero) InteractOptions() []string {
	return nil
}

func (h *Hero) GiveItem(o Object) {
	h.Backpack = append(h.Backpack, o)
}
<|endoftext|>


<commit_before>package main

import (
	"compress/gzip"
	"encoding/base32"
	"encoding/binary"
	"encoding/gob"
	"log"
	"os"
	"path/filepath"
	"sync"
	"time"
)

type Player struct {
	ID uint64
	Hero
	ZoneX, ZoneY int64
	TileX, TileY uint8
	hud          interface {
		Paint(func(int, int, rune, Color))
		Key(int) bool
	}
	repaint chan struct{}

	Joined    time.Time
	LastLogin time.Time
	Admin     bool

	zone *Zone
}

func (p *Player) Move(dx, dy int) {
	if p.Delay > 0 {
		return
	}
	destX := dx + int(p.TileX)
	destY := dy + int(p.TileY)

	zoneChange := destX < 0 || destY < 0 || destX > 255 || destY > 255

	p.lock.Lock()
	z := p.zone
	p.lock.Unlock()
	z.Lock()
	if !zoneChange {
		zoneChange = z.Tile(uint8(destX), uint8(destY)) == nil
	}
	if !zoneChange && z.Blocked(uint8(destX), uint8(destY)) {
		z.Unlock()
		return
	}
	z.Tile(p.TileX, p.TileY).Remove(p)
	z.Unlock()
	if zoneChange {
		p.RepaintZone()
		ReleaseZone(z)
		p.lock.Lock()
		p.Delay = 2
		if destY < 0 {
			p.ZoneY -= 2
			p.TileX = 127
			p.TileY = 255
		} else if destY > 255 {
			p.ZoneY += 2
			p.TileX = 127
			p.TileY = 0
		} else if destX < 128 {
			if destY < 128 {
				p.ZoneY--
				p.TileX = 255 - zoneOffset[255-64]
				p.TileY = 255 - 64
			} else {
				p.ZoneX--
				p.TileX = 255 - zoneOffset[64]
				p.TileY = 64
			}
		} else {
			if destY < 128 {
				p.ZoneX++
				p.ZoneY--
				p.TileX = zoneOffset[255-64]
				p.TileY = 255 - 64
			} else {
				p.ZoneY++
				p.TileX = zoneOffset[64]
				p.TileY = 64
			}
		}
		z = GrabZone(p.ZoneX, p.ZoneY)
		p.zone = z
		p.lock.Unlock()
		p.Save()
		p.hud = nil
	} else {
		p.lock.Lock()
		p.TileX = uint8(destX)
		p.TileY = uint8(destY)
		p.Delay = 2
		p.lock.Unlock()
	}
	z.Lock()
	z.Tile(p.TileX, p.TileY).Add(p)
	z.Unlock()
	p.RepaintZone()
}

func playerFilename(id uint64) string {
	var buf [binary.MaxVarintLen64]byte
	i := binary.PutUvarint(buf[:], id)
	encoded := base32.StdEncoding.EncodeToString(buf[:i])

	l := len(encoded)
	for encoded[l-1] == '=' {
		l--
	}
	return "p" + encoded[:l] + ".gz"
}

func (p *Player) Save() {
	p.lock.Lock()
	defer p.lock.Unlock()

	dir := seedFilename()
	os.MkdirAll(dir, 0755)

	f, err := os.Create(filepath.Join(dir, playerFilename(p.ID)))
	if err != nil {
		log.Printf("[save:%d] %v", p.ID, err)
		return
	}
	defer f.Close()

	g, err := gzip.NewWriterLevel(f, gzip.BestCompression)
	if err != nil {
		log.Printf("[save:%d] %v", p.ID, err)
		return
	}
	defer g.Close()

	err = gob.NewEncoder(g).Encode(p)
	if err != nil {
		log.Printf("[save:%d] %v", p.ID, err)
	}
}

func LoadPlayer(id uint64) (*Player, error) {
	dir := seedFilename()

	f, err := os.Open(filepath.Join(dir, playerFilename(id)))
	if err != nil {
		return nil, err
	}
	defer f.Close()

	g, err := gzip.NewReader(f)
	if err != nil {
		return nil, err
	}
	defer g.Close()

	d := gob.NewDecoder(g)
	var p Player
	err = d.Decode(&p)
	if err != nil {
		return nil, err
	}
	p.repaint = make(chan struct{}, 1)
	return &p, nil
}

func (p *Player) Repaint() {
	select {
	case p.repaint <- struct{}{}:
	default:
	}
}

func (p *Player) RepaintZone() {
	go func() {
		p.lock.Lock()
		p.zone.Repaint()
		p.lock.Unlock()
	}()
}

func (p *Player) Paint() (rune, Color) {
	if p.Admin {
		return '♚', "#fa0"
	}
	return p.Hero.Paint()
}

func (p *Player) Think() {
	p.think(false)
}

type ZoneEntryHUD string

func (h ZoneEntryHUD) Paint(setcell func(int, int, rune, Color)) {
	i := 0
	for _, r := range h {
		setcell(i, 0, r, "#fff")
		i++
	}
}

func (h ZoneEntryHUD) Key(code int) bool {
	return false
}

type Hero struct {
	Name_ *Name

	lock     sync.Mutex
	Delay    uint
	Backpack []Object
}

func (h *Hero) Name() string {
	return h.Name_.String()
}

func (h *Hero) Examine() string {
	return "a hero."
}

func (h *Hero) Blocking() bool {
	return false
}

func (h *Hero) Paint() (rune, Color) {
	return '☻', "#fff"
}

func (h *Hero) Think() {
	h.think(true)
}

func (h *Hero) think(ai bool) {
	h.lock.Lock()
	defer h.lock.Unlock()

	if h.Delay > 0 {
		h.Delay--
		return
	}
}

func (h *Hero) InteractOptions() []string {
	return nil
}

func (h *Hero) GiveItem(o Object) {
	h.Backpack = append(h.Backpack, o)
}
<commit_msg>fix zone-switching code<commit_after>package main

import (
	"compress/gzip"
	"encoding/base32"
	"encoding/binary"
	"encoding/gob"
	"log"
	"os"
	"path/filepath"
	"sync"
	"time"
)

type Player struct {
	ID uint64
	Hero
	ZoneX, ZoneY int64
	TileX, TileY uint8
	hud          interface {
		Paint(func(int, int, rune, Color))
		Key(int) bool
	}
	repaint chan struct{}

	Joined    time.Time
	LastLogin time.Time
	Admin     bool

	zone *Zone
}

func (p *Player) Move(dx, dy int) {
	if p.Delay > 0 {
		return
	}
	destX := dx + int(p.TileX)
	destY := dy + int(p.TileY)

	zoneChange := destX < 0 || destY < 0 || destX > 255 || destY > 255

	p.lock.Lock()
	z := p.zone
	p.lock.Unlock()
	z.Lock()
	if !zoneChange {
		zoneChange = z.Tile(uint8(destX), uint8(destY)) == nil
	}
	if !zoneChange && z.Blocked(uint8(destX), uint8(destY)) {
		z.Unlock()
		return
	}
	z.Tile(p.TileX, p.TileY).Remove(p)
	z.Unlock()
	if zoneChange {
		p.RepaintZone()
		ReleaseZone(z)
		p.lock.Lock()
		p.Delay = 2
		if destY < 0 {
			p.ZoneY -= 2
			p.TileX = 127
			p.TileY = 255
		} else if destY > 255 {
			p.ZoneY += 2
			p.TileX = 127
			p.TileY = 0
		} else if destX < 128 {
			if destY < 128 {
				p.ZoneX--
				p.ZoneY--
				p.TileX = 255 - zoneOffset[255-64]
				p.TileY = 255 - 64
			} else {
				p.ZoneX--
				p.ZoneY++
				p.TileX = 255 - zoneOffset[64]
				p.TileY = 64
			}
		} else {
			if destY < 128 {
				p.ZoneY--
				p.TileX = zoneOffset[255-64]
				p.TileY = 255 - 64
			} else {
				p.ZoneY++
				p.TileX = zoneOffset[64]
				p.TileY = 64
			}
		}
		z = GrabZone(p.ZoneX, p.ZoneY)
		p.zone = z
		p.lock.Unlock()
		p.Save()
		p.hud = nil
	} else {
		p.lock.Lock()
		p.TileX = uint8(destX)
		p.TileY = uint8(destY)
		p.Delay = 2
		p.lock.Unlock()
	}
	z.Lock()
	z.Tile(p.TileX, p.TileY).Add(p)
	z.Unlock()
	p.RepaintZone()
}

func playerFilename(id uint64) string {
	var buf [binary.MaxVarintLen64]byte
	i := binary.PutUvarint(buf[:], id)
	encoded := base32.StdEncoding.EncodeToString(buf[:i])

	l := len(encoded)
	for encoded[l-1] == '=' {
		l--
	}
	return "p" + encoded[:l] + ".gz"
}

func (p *Player) Save() {
	p.lock.Lock()
	defer p.lock.Unlock()

	dir := seedFilename()
	os.MkdirAll(dir, 0755)

	f, err := os.Create(filepath.Join(dir, playerFilename(p.ID)))
	if err != nil {
		log.Printf("[save:%d] %v", p.ID, err)
		return
	}
	defer f.Close()

	g, err := gzip.NewWriterLevel(f, gzip.BestCompression)
	if err != nil {
		log.Printf("[save:%d] %v", p.ID, err)
		return
	}
	defer g.Close()

	err = gob.NewEncoder(g).Encode(p)
	if err != nil {
		log.Printf("[save:%d] %v", p.ID, err)
	}
}

func LoadPlayer(id uint64) (*Player, error) {
	dir := seedFilename()

	f, err := os.Open(filepath.Join(dir, playerFilename(id)))
	if err != nil {
		return nil, err
	}
	defer f.Close()

	g, err := gzip.NewReader(f)
	if err != nil {
		return nil, err
	}
	defer g.Close()

	d := gob.NewDecoder(g)
	var p Player
	err = d.Decode(&p)
	if err != nil {
		return nil, err
	}
	p.repaint = make(chan struct{}, 1)
	return &p, nil
}

func (p *Player) Repaint() {
	select {
	case p.repaint <- struct{}{}:
	default:
	}
}

func (p *Player) RepaintZone() {
	go func() {
		p.lock.Lock()
		p.zone.Repaint()
		p.lock.Unlock()
	}()
}

func (p *Player) Paint() (rune, Color) {
	if p.Admin {
		return '♚', "#fa0"
	}
	return p.Hero.Paint()
}

func (p *Player) Think() {
	p.think(false)
}

type ZoneEntryHUD string

func (h ZoneEntryHUD) Paint(setcell func(int, int, rune, Color)) {
	i := 0
	for _, r := range h {
		setcell(i, 0, r, "#fff")
		i++
	}
}

func (h ZoneEntryHUD) Key(code int) bool {
	return false
}

type Hero struct {
	Name_ *Name

	lock     sync.Mutex
	Delay    uint
	Backpack []Object
}

func (h *Hero) Name() string {
	return h.Name_.String()
}

func (h *Hero) Examine() string {
	return "a hero."
}

func (h *Hero) Blocking() bool {
	return false
}

func (h *Hero) Paint() (rune, Color) {
	return '☻', "#fff"
}

func (h *Hero) Think() {
	h.think(true)
}

func (h *Hero) think(ai bool) {
	h.lock.Lock()
	defer h.lock.Unlock()

	if h.Delay > 0 {
		h.Delay--
		return
	}
}

func (h *Hero) InteractOptions() []string {
	return nil
}

func (h *Hero) GiveItem(o Object) {
	h.Backpack = append(h.Backpack, o)
}
<|endoftext|>