コード例 #1
0
ファイル: ctar.go プロジェクト: surma-dump/ctar
func TraverseFileTree(path string) ([]string, os.Error) {
	l := vector.StringVector(make([]string, 1, 20))
	l.Push(path)
	d, e := IsDirectory(path)
	if e != nil {
		return nil, e
	}

	if d {
		c, e := GetDirectoryContent(path)
		if e != nil {
			return nil, e
		}

		for _, file := range c {
			s, e := TraverseFileTree(path + "/" + file)
			v := vector.StringVector(s)
			if e != nil {
				return nil, e
			}

			l.AppendVector(&v)
		}
	}

	return l, nil

}
コード例 #2
0
ファイル: autocompletion.go プロジェクト: kij/gocode
func NewOutBuffers(ctx *AutoCompleteContext) *OutBuffers {
	b := new(OutBuffers)
	b.tmpbuf = bytes.NewBuffer(make([]byte, 0, 1024))
	b.names = vector.StringVector(make([]string, 0, 1024))
	b.types = vector.StringVector(make([]string, 0, 1024))
	b.classes = vector.StringVector(make([]string, 0, 1024))
	b.ctx = ctx
	return b
}
コード例 #3
0
// ReadMIMEHeader reads a MIME-style header from r.
// The header is a sequence of possibly continued Key: Value lines
// ending in a blank line.
// The returned map m maps CanonicalHeaderKey(key) to a
// sequence of values in the same order encountered in the input.
//
// For example, consider this input:
//
//	My-Key: Value 1
//	Long-Key: Even
//	       Longer Value
//	My-Key: Value 2
//
// Given that input, ReadMIMEHeader returns the map:
//
//	map[string][]string{
//		"My-Key": []string{"Value 1", "Value 2"},
//		"Long-Key": []string{"Even Longer Value"},
//	}
//
func (r *Reader) ReadMIMEHeader() (map[string][]string, os.Error) {
	m := make(map[string][]string)
	for {
		kv, err := r.ReadContinuedLineBytes()
		if len(kv) == 0 {
			return m, err
		}

		// Key ends at first colon; must not have spaces.
		i := bytes.IndexByte(kv, ':')
		if i < 0 || bytes.IndexByte(kv[0:i], ' ') >= 0 {
			return m, ProtocolError("malformed MIME header line: " + string(kv))
		}
		key := CanonicalHeaderKey(string(kv[0:i]))

		// Skip initial spaces in value.
		i++ // skip colon
		for i < len(kv) && (kv[i] == ' ' || kv[i] == '\t') {
			i++
		}
		value := string(kv[i:])

		v := vector.StringVector(m[key])
		v.Push(value)
		m[key] = v

		if err != nil {
			return m, err
		}
	}
	panic("unreachable")
}
コード例 #4
0
ファイル: PlanetWars.go プロジェクト: vil1/ai-contest
func main() {
	in := bufio.NewReader(os.Stdin)
	lines := vector.StringVector(make([]string, 0, 500))

	for {
		for {
			l, err := in.ReadString('\n')

			if err != nil {
				log.Stderr("Error reading map, expected more input\n")
				return
			}

			if l == "go\n" {
				pw := ParseGameState(lines)
				DoTurn(pw)
				pw.EndTurn()
				break
			} else {
				lines.Push(l)
			}
		}

		lines = lines[0:0]
	}
}
コード例 #5
0
ファイル: decision.go プロジェクト: pombredanne/webmachine.go
func (p *wmDecisionCore) variances() []string {
	var v vector.StringVector
	var ctp []MediaTypeHandler
	var ep []EncodingHandler
	var cp []CharsetHandler
	arr := make([]string, 1)
	arr[0] = "*"
	ctp, p.req, p.cxt, _, _ = p.handler.ContentTypesProvided(p.req, p.cxt)
	ep, p.req, p.cxt, _, _ = p.handler.EncodingsProvided(arr, p.req, p.cxt)
	cp, p.req, p.cxt, _, _ = p.handler.CharsetsProvided(arr, p.req, p.cxt)
	if len(ctp) > 1 {
		v.Push("Accept")
	}
	if len(ep) > 1 {
		v.Push("Accept-Encoding")
	}
	if len(cp) > 1 {
		v.Push("Accept-Charset")
	}
	var headers []string
	headers, p.req, p.cxt, _, _ = p.handler.Variances(p.req, p.cxt)
	v2 := vector.StringVector(headers)
	v.AppendVector(&v2)
	return v
}
コード例 #6
0
ファイル: nntp.go プロジェクト: rapgamer/golang-china
// Internal. Parses headers in NNTP articles. Most of this is stolen from the http package,
// and it should probably be split out into a generic RFC822 header-parsing package.
func (c *Conn) readHeader(r *bufio.Reader) (res *Article, err os.Error) {
	res = new(Article)
	res.Header = make(map[string][]string)
	for {
		var key, value string
		if key, value, err = readKeyValue(r); err != nil {
			return nil, err
		}
		if key == "" {
			break
		}
		key = http.CanonicalHeaderKey(key)
		// RFC 3977 says nothing about duplicate keys' values being equivalent to
		// a single key joined with commas, so we keep all values seperate.
		oldvalue, present := res.Header[key]
		if present {
			sv := vector.StringVector(oldvalue)
			sv.Push(value)
			res.Header[key] = []string(sv)
		} else {
			res.Header[key] = []string{value}
		}
	}
	return res, nil
}
コード例 #7
0
ファイル: ctar.go プロジェクト: surma-dump/ctar
func ChannelToSliceString(in <-chan string) []string {

	v := vector.StringVector(make([]string, 1, 20))
	for i := range in {
		v.Push(i)
	}

	return v[1:v.Len()]
}
コード例 #8
0
ファイル: dice.go プロジェクト: zot/bills-tools
func dumpResults(totMargin int, margins map[string]map[int]int) {
	vec := vector.StringVector(make([]string, 0, 32))
	for k := range margins {
		vec.Push(k)
	}
	sort.StringArray(vec).Sort()
	for _, dice := range vec {
		println("Margins for", dice)
		dumpMargin(totMargin, margins[dice])
	}
}
コード例 #9
0
ファイル: ctar.go プロジェクト: surma-dump/ctar
func TraverseFileTreeFiltered(path string) ([]string, os.Error) {
	list, e := TraverseFileTree(path)
	if e != nil {
		return nil, e
	}

	v := vector.StringVector(make([]string, 1, 20))
	for _, l := range list {
		if len(l) != 0 {
			v.Push(l)
		}
	}

	return v[1:v.Len()], nil

}
コード例 #10
0
ファイル: request.go プロジェクト: rapgamer/golang-china
func ParseQuery(query string) (m map[string][]string, err os.Error) {
	m = make(map[string][]string)
	for _, kv := range strings.Split(query, "&", 0) {
		kvPair := strings.Split(kv, "=", 2)

		var key, value string
		var e os.Error
		key, e = URLUnescape(kvPair[0])
		if e == nil && len(kvPair) > 1 {
			value, e = URLUnescape(kvPair[1])
		}
		if e != nil {
			err = e
		}

		vec := vector.StringVector(m[key])
		vec.Push(value)
		m[key] = vec
	}

	return
}
コード例 #11
0
ファイル: request.go プロジェクト: richlowe/gcc
func parseQuery(m map[string][]string, query string) (err os.Error) {
	for _, kv := range strings.Split(query, "&", -1) {
		if len(kv) == 0 {
			continue
		}
		kvPair := strings.Split(kv, "=", 2)

		var key, value string
		var e os.Error
		key, e = URLUnescape(kvPair[0])
		if e == nil && len(kvPair) > 1 {
			value, e = URLUnescape(kvPair[1])
		}
		if e != nil {
			err = e
			continue
		}
		vec := vector.StringVector(m[key])
		vec.Push(value)
		m[key] = vec
	}
	return err
}
コード例 #12
0
func (f *allFlags) parseOne(index int) (ok bool, next int) {
	s := os.Args[index]
	// Take care of non-flag arguments.
	if len(s) == 0 || s[0] != '-' || s == "-" {
		f.args.Push(s)
		return true, index + 1
	}
	if s == "--" {
		v := vector.StringVector(os.Args[index+1:])
		f.args.AppendVector(&v)
		return false, -1
	}
	var errorStr string
	// Sort out flag arguments.
	if s[1] != '-' {
		for {
			// Deal with shortname flags
			sname, sz := utf8.DecodeRuneInString(s[1:])
			if sname == utf8.RuneError {
				errorStr = "invalid UTF-8 character"
				goto argError
			}
			name, ok := f.snames[sname]
			if !ok {
				errorStr = fmt.Sprintf("flag provided but not defined: -%s\n", string(sname))
				goto argError
			}
			rest := s[1+sz:]
			// Check for (bad) extraneous flags
			if _, ok := f.actual[name]; ok {
				errorStr = fmt.Sprintf("flag specified twice: -%s\n", string(sname))
				goto argError
			}
			flag, ok := f.formal[name]
			if !ok {
				errorStr = fmt.Sprintf("flag provided but not defined: -%s\n", string(sname))
				goto argError
			}
			// Try and understand the value of the flag
			if f, ok := flag.Value.(*boolValue); ok { // special case: doesn't need an arg
				f.set("true")
				s = "-" + rest
				continue
			}
			has_value := false
			if rest != "" {
				has_value = true
			}
			if !has_value && index < len(os.Args)-1 {
				has_value = true
				index++
				rest = os.Args[index]
			}
			if !has_value {
				errorStr = fmt.Sprintf("flag needs an argument: -%s\n", string(sname))
				goto argError
			}
			if ok = flag.Value.set(rest); !ok {
				errorStr = fmt.Sprintf("invalid value %s for flag: -%s\n", rest, string(sname))
				goto argError
			}
			break
		}
	} else {
		// Long name flags
		name := s[2:]
		if name[0] == '-' || name[0] == '=' {
			errorStr = fmt.Sprintln("bad flag syntax:", s)
			goto argError
		}
		has_value := false
		value := ""
		for i, rune := range name {
			if rune == '=' {
				value = name[i+1:] // the '=' rune has len 1
				has_value = true
				name = name[0:i]
				break
			}
		}
		// Check for (bad) extraneous flags
		if _, ok := f.actual[name]; ok {
			errorStr = fmt.Sprintf("flag specified twice: -%s\n", name)
			goto argError
		}
		flag, ok := f.formal[name]
		if !ok {
			errorStr = fmt.Sprintf("flag provided but not defined: -%s\n", name)
			goto argError
		}
		// Try and understand the value of the flag
		if f, ok := flag.Value.(*boolValue); ok { // special case: doesn't need an arg
			if has_value {
				if !f.set(value) {
					errorStr = fmt.Sprintf("invalid boolean value %t for flag: -%s\n", value, name)
					goto argError
				}
			} else {
				f.set("true")
			}
		} else {
			// It must have a value, which might be the next argument.
			if !has_value && index < len(os.Args)-1 {
				// value is the next arg
				has_value = true
				index++
				value = os.Args[index]
			}
			if !has_value {
				errorStr = fmt.Sprintf("flag needs an argument: -%s\n", name)
				goto argError
			}
			if ok = flag.Value.set(value); !ok {
				errorStr = fmt.Sprintf("invalid value %s for flag: -%s\n", value, name)
				goto argError
			}
		}
		f.actual[name] = flag
	}
	return true, index + 1
argError:
	fmt.Fprint(os.Stderr, errorStr)
	Usage()
	os.Exit(2)
	return false, -1
}
コード例 #13
0
ファイル: pipeline.go プロジェクト: pombredanne/dsocial.go
func (p *Pipeline) groupImport(cs ContactsService, ds DataStoreService, dsocialUserId string, group *Group, minimumIncludes *list.List, allowAdd, allowDelete, allowUpdate bool) (*dm.Group, string, os.Error) {
	emptyGroup := new(dm.Group)
	if group == nil || group.Value == nil {
		return nil, "", nil
	}
	//fmt.Printf("[PIPELINE]: Syncing group: %s\n", group.Value.Name)
	if group.Value.ContactIds == nil {
		group.Value.ContactIds = make([]string, 0, 10)
	}
	if group.Value.ContactNames == nil {
		group.Value.ContactNames = make([]string, 0, 10)
	}
	if len(group.Value.ContactIds) == 0 && len(group.Value.ContactNames) == 0 && minimumIncludes != nil {
		sv1 := vector.StringVector(group.Value.ContactIds)
		sv2 := vector.StringVector(group.Value.ContactNames)
		sv1.Resize(sv1.Len(), sv1.Len()+minimumIncludes.Len())
		sv2.Resize(sv2.Len(), sv2.Len()+minimumIncludes.Len())
		for iter := minimumIncludes.Front(); iter != nil; iter = iter.Next() {
			contactRef := iter.Value.(*dm.ContactRef)
			sv1.Push(contactRef.Id)
			sv2.Push(contactRef.Name)
		}
	} else if minimumIncludes != nil {
		for iter := minimumIncludes.Front(); iter != nil; iter = iter.Next() {
			contactRef := iter.Value.(*dm.ContactRef)
			refLocation := -1
			if contactRef.Id != "" {
				for i, id := range group.Value.ContactIds {
					if id == contactRef.Id {
						refLocation = i
						break
					}
				}
			}
			if refLocation == -1 && contactRef.Name != "" {
				for i, name := range group.Value.ContactNames {
					if name == contactRef.Name {
						refLocation = i
						break
					}
				}
			}
			if refLocation == -1 {
				sv1 := vector.StringVector(group.Value.ContactIds)
				sv2 := vector.StringVector(group.Value.ContactNames)
				sv1.Push(contactRef.Id)
				sv2.Push(contactRef.Name)
			} else {
				group.Value.ContactIds[refLocation] = contactRef.Id
				group.Value.ContactNames[refLocation] = contactRef.Name
			}
		}
	}
	extDsocialGroup, _, err := ds.RetrieveDsocialGroupForExternalGroup(group.ExternalServiceId, group.ExternalUserId, group.ExternalGroupId, dsocialUserId)
	if err != nil {
		return nil, "", err
	}
	var matchingGroup *dm.Group
	var isSame bool
	if extDsocialGroup == nil {
		// We don't have a mapping for this external group to an internal group mapping
		// meaning we've never imported this group previously from THIS service, but we may
		// already have the group in our system, so let's see if we can find it
		matchingGroup, isSame, err = p.findMatchingDsocialGroup(ds, dsocialUserId, group)
		if err != nil {
			return matchingGroup, "", err
		}
		if matchingGroup != nil {
			group.DsocialGroupId = matchingGroup.Id
			extGroup := cs.ConvertToExternalGroup(matchingGroup, nil, dsocialUserId)
			ds.StoreExternalGroup(group.ExternalServiceId, group.ExternalUserId, dsocialUserId, group.ExternalGroupId, extGroup)
			extDsocialGroup = cs.ConvertToDsocialGroup(extGroup, matchingGroup, dsocialUserId)
			if extDsocialGroup != nil {
				AddIdsForDsocialGroup(extDsocialGroup, ds, dsocialUserId)
				fmt.Printf("[PIPELINE]: groupImport() before store dsoc group ExternalGroupId: %v and extDsocialGroup.Id %v matchingGroup.Id %v\n", group.ExternalGroupId, extDsocialGroup.Id, matchingGroup.Id)
				//group.ExternalGroupId = extDsocialGroup.Id
				//extDsocialGroup.Id = group.DsocialGroupId
				extDsocialGroup, err = ds.StoreDsocialGroupForExternalGroup(group.ExternalServiceId, group.ExternalUserId, group.ExternalGroupId, dsocialUserId, extDsocialGroup)
				if extDsocialGroup == nil || err != nil {
					return matchingGroup, "", err
				}
				//extDsocialGroup.Id = group.DsocialGroupId
				fmt.Printf("[PIPELINE]: groupImport() before store mapping ExternalGroupId: %v and DsocialGroupId %v\n", group.ExternalGroupId, group.DsocialGroupId)
				if _, _, err = ds.StoreDsocialExternalGroupMapping(group.ExternalServiceId, group.ExternalUserId, group.ExternalGroupId, dsocialUserId, group.DsocialGroupId); err != nil {
					return matchingGroup, "", err
				}
			}
		}
	} else {
		// we have a mapping for this external group to an internal group mapping
		// from THIS service, therefore let's use it
		if group.DsocialGroupId == "" {
			group.DsocialGroupId, err = ds.DsocialIdForExternalGroupId(group.ExternalServiceId, group.ExternalUserId, dsocialUserId, group.ExternalGroupId)
			if err != nil {
				return nil, "", err
			}
		}
		if group.DsocialGroupId != "" {
			matchingGroup, _, err = ds.RetrieveDsocialGroup(dsocialUserId, group.DsocialGroupId)
			if err != nil {
				return nil, "", err
			}
		}
	}
	// ensure we have a contact Id
	if group.DsocialGroupId == "" {
		if matchingGroup != nil {
			group.DsocialGroupId = matchingGroup.Id
			fmt.Printf("[PIPELINE]: Will be using matchingGroup Id: %v\n", matchingGroup.Id)
		}
		if group.DsocialGroupId == "" {
			newGroup := &dm.Group{UserId: dsocialUserId}
			AddIdsForDsocialGroup(newGroup, ds, dsocialUserId)
			thegroup, err := ds.StoreDsocialGroup(dsocialUserId, newGroup.Id, newGroup)
			if err != nil {
				return nil, "", err
			}
			group.DsocialGroupId = thegroup.Id
		}
	}
	if _, isSame = emptyGroup.IsSimilarOrUpdated(extDsocialGroup, group.Value); isSame {
		return matchingGroup, "", nil
	}
	l := new(list.List)
	emptyGroup.GenerateChanges(extDsocialGroup, group.Value, nil, l)
	l = p.removeUnacceptedChanges(l, allowAdd, allowDelete, allowUpdate)
	changes := make([]*dm.Change, l.Len())
	for i, iter := 0, l.Front(); iter != nil; i, iter = i+1, iter.Next() {
		changes[i] = iter.Value.(*dm.Change)
	}
	changeset := &dm.ChangeSet{
		CreatedAt:      time.UTC().Format(dm.UTC_DATETIME_FORMAT),
		ChangedBy:      group.ExternalServiceId,
		ChangeImportId: group.ExternalGroupId,
		RecordId:       group.DsocialGroupId,
		Changes:        changes,
	}
	_, err = ds.StoreGroupChangeSet(dsocialUserId, changeset)
	if err != nil {
		return matchingGroup, changeset.Id, nil
	}
	if extDsocialGroup == nil {
		fmt.Printf("[PIPELINE]: OriginalExternalGroup is nil and group.DsocialGroupId is %v and group.Value.Id was %v\n", group.DsocialGroupId, group.Value.Id)
		group.Value.Id = group.DsocialGroupId
		AddIdsForDsocialGroup(group.Value, ds, dsocialUserId)
		group.Value, err = ds.StoreDsocialGroup(dsocialUserId, group.DsocialGroupId, group.Value)
		if err != nil {
			return matchingGroup, changeset.Id, err
		}
		storedDsocialGroup, err := ds.StoreDsocialGroupForExternalGroup(group.ExternalServiceId, group.ExternalUserId, group.ExternalGroupId, dsocialUserId, group.Value)
		_, _, err2 := ds.StoreDsocialExternalGroupMapping(group.ExternalServiceId, group.ExternalUserId, group.ExternalGroupId, dsocialUserId, group.DsocialGroupId)
		if err == nil {
			err = err2
		}
		return storedDsocialGroup, changeset.Id, err
	}
	var storedDsocialGroup *dm.Group = nil
	if group.DsocialGroupId != "" {
		if storedDsocialGroup, _, err = ds.RetrieveDsocialGroup(dsocialUserId, group.DsocialGroupId); err != nil {
			return matchingGroup, changeset.Id, err
		}
	}
	if storedDsocialGroup == nil {
		storedDsocialGroup = new(dm.Group)
	}
	for j, iter := 0, l.Front(); iter != nil; j, iter = j+1, iter.Next() {
		change := iter.Value.(*dm.Change)
		dm.ApplyChange(storedDsocialGroup, change)
		changes[j] = change
	}
	AddIdsForDsocialGroup(storedDsocialGroup, ds, dsocialUserId)
	_, err = ds.StoreDsocialGroup(dsocialUserId, group.DsocialGroupId, storedDsocialGroup)
	return storedDsocialGroup, changeset.Id, err
}
コード例 #14
0
ファイル: exec.go プロジェクト: andradeandrey/doozer
// ForkExec runs `cmd` in a child process, with all configuration options as
// specified in `cx` and the listen fds `lf` in its initial set of fds.
func (cx *Context) ForkExec(cmd string, lf []*os.File) (pid int, err os.Error) {
	// This is not easy to do, because Go does not give us a plain fork
	// function. Here's the trick: we fork/exec the same program currently
	// running in this process, then send configuration parameters to it over a
	// pipe.
	//
	// After the first exec, the child process will be running this program,
	// almost as if we had simply forked, but with a fresh address space. Once
	// the child reads the configuration data from its pipe, it has enough
	// information to set up the process environment and exec a second time,
	// starting the program we really want.

	if !hasProc() {
		return 0, os.NewError("doozer: Your OS doesn't have /proc")
	}

	var r run
	r.Context = *cx
	r.cmd = cmd

	var ir, iw, sr, sw *os.File
	sb := make([]byte, 4)

	// These pipe fds are already close-on-exec, which is what we want for iw
	// and sr. It's also okay for ir and sw, because we explicitly supply them
	// to os.ForkExec.
	ir, iw, err = os.Pipe()
	if err != nil {
		return
	}
	defer ir.Close()
	defer iw.Close()
	sr, sw, err = os.Pipe()
	if err != nil {
		return
	}
	defer sr.Close()
	defer sw.Close()

	// Boring, ugly code to set up the list of child fds.
	files := make([]*os.File, passListenFdsStart+len(lf))
	files[0] = os.Stdin
	files[1] = os.Stdout
	files[2] = os.Stderr
	files[inputReadFd] = ir
	files[statusWriteFd] = sw
	copy(files[passListenFdsStart:], lf)

	// Boring, ugly code to set up the child environment vars.
	envv := vector.StringVector(os.Environ())

	// See if we are giving it any listen fds.
	if lf != nil {
		envv.Push(fmt.Sprintf("LISTEN_FDS=%d", len(lf)))
	}

	// Okay, here we go...

	// Fork and exec the same program we're currently running. Give it `cookie`
	// as its only arg, telling it to behave as our helper program and run
	// function `execInChild`.
	pid, err = os.ForkExec("/proc/self/exe", []string{cookie}, envv, "", files)
	if err != nil {
		return 0, err
	}

	// Send it the configuration data.
	en := gob.NewEncoder(iw)
	err = en.Encode(&r)
	if err != nil {
		goto parenterror
	}

	// Be sure not to deadlock if the child is successful.
	sw.Close()

	// Check if the child had an error.
	var n int
	n, err = sr.Read(sb)
	if err != os.EOF || n != 0 {
		// got a full error code?
		if n == len(sb) {
			// decode it (big-endian)
			errno := 0 | // this 0 is to trick gofmt
				(int32(sb[0]) << 24) |
				(int32(sb[1]) << 16) |
				(int32(sb[2]) << 8) |
				(int32(sb[3]) << 0)
			err = &ChildError{os.Errno(errno)}
		}
		if err == nil {
			err = os.EPIPE
		}
		goto parenterror
	}

	// Read got EOF, so status pipe closed on exec, so exec succeeded.
	return pid, nil

parenterror:
	// error of some sort after creating the child

	// make sure the child is well and truly dead
	syscall.Kill(pid, syscall.SIGKILL)

	// wait for it to exit, so we don't accumulate zombies
	var wstatus syscall.WaitStatus
	_, e1 := syscall.Wait4(pid, &wstatus, 0, nil)
	for e1 == syscall.EINTR {
		_, e1 = syscall.Wait4(pid, &wstatus, 0, nil)
	}
	return 0, err
}
コード例 #15
0
ファイル: web.go プロジェクト: andradeandrey/twister
// Append value to slice for given key.
func (m StringsMap) Append(key string, value string) {
	v := vector.StringVector(m[key])
	v.Push(value)
	m[key] = v
}