Commits

Fazlul Shahriar  committed a28e35c

hdf4: make some types/methods public

  • Participants
  • Parent commits 24bc83f

Comments (0)

Files changed (3)

File hdf4/const.go

 // /usr/include/htags.h
 // /usr/include/hntdefs.h
 
-// Semi-autogenerated from the table in appendix A of spec
+// Record.Tag
 const (
 	DFTAG_AR       = 312  // Aspect ratio
 	DFTAG_CAL      = 731  // Calibration information
 	DFTAG_XYP      = 500  // X-Y position
 )
 
-// nt.typ
+// NumType.Type
 const (
 	DFNT_NONE    = 0 //indicates that number type not set
 	DFNT_VERSION = 1 // current version of NT info
 	DFNT_UINT64 = 27
 )
 
-// nt.class
+// NumType.Class
 const (
 	// floating point
 	DFNTF_NONE   = 0 // indicates subclass is not set

File hdf4/hdf4.go

 type File struct {
 	fd      *os.File
 	r       *bufio.Reader
-	ds      []descriptor
-	version *version
+	Records []Record
+	Version *Version
 }
 
-const descriptorSize = 12
-
-type descriptor struct { // Data Descriptor
-	tag, ref    uint16
+type Record struct {
+	Tag, Ref    uint16
 	offset, len uint32
 	file        *File
+	info        interface{}
 }
 
 func get8(b []byte) (uint8, []byte) {
 		return nil, err
 	}
 
-	d := file.lookupDescriptor(DFTAG_VERSION, -1)
-	if d == nil {
+	rec := file.LookupRecord(DFTAG_VERSION, -1)
+	if rec == nil {
 		return nil, os.NewError("version descriptor missing")
 	}
-	b, err := d.readRecord()
+	b, err := rec.readPayload()
 	if err != nil {
 		return nil, err
 	}
-	file.version = parseVersion(b)
+	file.Version = parseVersion(b)
 
 	return file, nil
 }
 }
 
 func (file *File) readDescriptors() os.Error {
-	file.ds = nil
+	file.Records = nil
 	for {
 		// Read block header
 		var bheader [2 + 4]byte
 		bnext, b := get32(b)
 
 		// Read descriptors in this block
+		const descriptorSize = 12
 		b = make([]byte, bsize*descriptorSize)
 		if _, err := io.ReadFull(file.r, b); err != nil {
 			return err
 		}
 		for i := 0; i < int(bsize); i++ {
-			d := new(descriptor)
-			d.file = file
-			d.tag, b = get16(b)
-			d.ref, b = get16(b)
-			d.offset, b = get32(b)
-			d.len, b = get32(b)
-			if d.tag != DFTAG_NULL {
-				file.ds = append(file.ds, *d)
+			rec := new(Record)
+			rec.file = file
+			rec.Tag, b = get16(b)
+			rec.Ref, b = get16(b)
+			rec.offset, b = get32(b)
+			rec.len, b = get32(b)
+			if rec.Tag != DFTAG_NULL {
+				file.Records = append(file.Records, *rec)
 			}
 		}
 
 	return nil
 }
 
-// LookupDescriptor returns the first descriptor matching tag
-// and ref, which uniquely identifies a descriptor. If ref < 0,
-// first descriptor matching tag is returns.
-func (file *File) lookupDescriptor(tag, ref int) *descriptor {
-	for _, d := range file.ds {
-		if ref < 0 && int(d.tag) == tag {
-			return &d
+// LookupRecord returns the first record matching tag
+// and ref, which uniquely identifies a record. If ref < 0,
+// first record matching tag is returns.
+func (file *File) LookupRecord(tag, ref int) *Record {
+	for _, rec := range file.Records {
+		if ref < 0 && int(rec.Tag) == tag {
+			return &rec
 		}
-		if int(d.ref) == ref && int(d.tag) == tag {
-			return &d
+		if int(rec.Ref) == ref && int(rec.Tag) == tag {
+			return &rec
 		}
 	}
 	return nil
 }
 
-func (d *descriptor) readRecord() ([]byte, os.Error) {
-	if _, err := d.file.fd.Seek(int64(d.offset), os.SEEK_SET); err != nil {
+func (rec *Record) readPayload() ([]byte, os.Error) {
+	if _, err := rec.file.fd.Seek(int64(rec.offset), os.SEEK_SET); err != nil {
 		return nil, err
 	}
-	r := bufio.NewReader(d.file.fd)
+	r := bufio.NewReader(rec.file.fd)
 
-	b := make([]byte, d.len)
+	b := make([]byte, rec.len)
 	if _, err := io.ReadFull(r, b); err != nil {
 		return nil, err
 	}
 	return b, nil
 }
 
-type version struct {
-	major, minor, release uint32
-	descr                 string
+type Version struct {
+	Major, Minor, Release uint32
+	Description           string
 }
 
-func parseVersion(b []byte) *version {
-	v := new(version)
-	v.major, b = get32(b)
-	v.minor, b = get32(b)
-	v.release, b = get32(b)
-	v.descr = string(b[:])
+func parseVersion(b []byte) *Version {
+	v := new(Version)
+	v.Major, b = get32(b)
+	v.Minor, b = get32(b)
+	v.Release, b = get32(b)
+	v.Description = string(b[:])
 	return v
 }
 
-type nt struct {
-	version, typ, width, class uint8
+type NumType struct {
+	version, Type, width, Class uint8
 }
 
-func parseNT(b []byte) *nt {
-	nt := new(nt)
+func parseNumType(b []byte) *NumType {
+	nt := new(NumType)
 	nt.version, b = get8(b)
-	nt.typ, b = get8(b)
+	nt.Type, b = get8(b)
 	nt.width, b = get8(b)
-	nt.class, b = get8(b)
+	nt.Class, b = get8(b)
 	return nt
 }
 
-type sdd struct {
-	dim                  []uint32
+// Scientific Data Dimension
+type SDD struct {
+	Dim                  []uint32
 	dataTag, dataRef     uint16
 	scaleTags, scaleRefs []uint16
 }
 
-func parseSDD(b []byte) *sdd {
-	sdd := new(sdd)
+func parseSDD(b []byte) *SDD {
+	sdd := new(SDD)
 	rank, b := get16(b)
 
-	sdd.dim = make([]uint32, rank)
+	sdd.Dim = make([]uint32, rank)
 	for i := 0; i < int(rank); i++ {
-		sdd.dim[i], b = get32(b)
+		sdd.Dim[i], b = get32(b)
 	}
 
 	sdd.dataTag, b = get16(b)
 	return sdd
 }
 
-type ndg struct {
+// Numeric Data Group
+type NDG struct {
 	tags, refs []uint16
 }
 
-func parseNDG(b []byte) *ndg {
+func parseNDG(b []byte) *NDG {
 	var tag, ref uint16
 
-	ndg := new(ndg)
+	ndg := new(NDG)
 	for len(b) > 0 {
 		tag, b = get16(b)
 		ref, b = get16(b)
 	return ndg
 }
 
-type vgroup struct {
+type VGroup struct {
 	tags, refs   []uint16
-	name         []byte
-	class        []byte
+	Name         []byte
+	Class        []byte
 	extag, exref uint16
 	version      uint16
 }
 
-func parseVgroup(b []byte) *vgroup {
-	vg := new(vgroup)
+func parseVGroup(b []byte) *VGroup {
+	vg := new(VGroup)
 	nelt, b := get16(b)
 
 	vg.tags = make([]uint16, nelt)
 	for i := 0; i < int(nelt); i++ {
 		vg.refs[i], b = get16(b)
 	}
-	vg.name, b = getstring(b)
-	vg.class, b = getstring(b)
+	vg.Name, b = getstring(b)
+	vg.Class, b = getstring(b)
 	vg.extag, b = get16(b)
 	vg.exref, b = get16(b)
 	vg.version, b = get16(b)

File hdf4/hdf4_test.go

 const hdfPath = "/home/fhs/glasslab/src/cmd/modis_crefl/tbase.hdf"
 
 func readRecord(t *testing.T, file *File, tag, ref int) []byte {
-	d := file.lookupDescriptor(tag, ref)
-	if d == nil {
+	rec := file.LookupRecord(tag, ref)
+	if rec == nil {
 		t.Fatalf("descriptor missing")
 	}
-	b, err := d.readRecord()
+	b, err := rec.readPayload()
 	if err != nil {
 		t.Fatalf("descriptor read failed: %v", err)
 	}
 	}
 	defer file.Close()
 
-	log.Printf("ds=%v\n", file.ds)
-	log.Printf("version=%v\n", file.version)
+	log.Printf("Records=%v\n", file.Records)
+	log.Printf("Version=%v\n", file.Version)
 
 	b := readRecord(t, file, DFTAG_NT, -1)
-	nt := parseNT(b)
+	nt := parseNumType(b)
 	log.Printf("nt = %v\n", nt)
 
 	b = readRecord(t, file, DFTAG_SDD, -1)
 	log.Printf("ndg = %v\n", ndg)
 
 	b = readRecord(t, file, DFTAG_VG, -1)
-	log.Printf("vg buf len = %d\n", len(b))
-	vg := parseVgroup(b)
-	log.Printf("vg = %v (name=%s, class=%s)\n", vg, string(vg.name), string(vg.class))
+	vg := parseVGroup(b)
+	log.Printf("vg = %v (name=%s, class=%s)\n", vg, string(vg.Name), string(vg.Class))
 }