예제 #1
0
파일: map.go 프로젝트: manyminds/tmx
// NewMap creates a new map from a given io.Reader
func NewMap(f io.Reader) (*Map, error) {
	var target Map
	data, err := ioutil.ReadAll(f)
	if err != nil {
		return nil, err
	}

	filename := ""
	if f, ok := f.(*os.File); ok {
		filename = filepath.Dir(f.Name()) + string(os.PathSeparator)
	}

	err = xml.Unmarshal(data, &target)
	if err != nil {
		return nil, err
	}

	target.filename = filename

	for key, layer := range target.Layers {
		if e := layer.Data.loadEncodedTiles(); e != nil {
			return nil, e
		}

		target.Layers[key] = layer
	}

	return &target, nil
}
예제 #2
0
// Open opens a FITS file in read-only mode.
func Open(r io.Reader) (*File, error) {
	var err error

	type namer interface {
		Name() string
	}
	name := ""
	if r, ok := r.(namer); ok {
		name = r.Name()
	}

	f := &File{
		dec:     NewDecoder(r),
		name:    name,
		mode:    ReadOnly,
		hdus:    make([]HDU, 0, 1),
		closers: make([]io.Closer, 0, 1),
	}

	defer func() {
		if err != nil {
			f.Close()
		}
	}()

	if rr, ok := r.(io.Closer); ok {
		f.closers = append(f.closers, rr)
	}

	for {
		var hdu HDU
		hdu, err = f.dec.DecodeHDU()
		if err != nil {
			if err != io.EOF {
				return nil, err
			}
			err = nil
			break
		}
		f.hdus = append(f.hdus, hdu)
		f.closers = append(f.closers, hdu)
	}

	return f, err
}