示例#1
0
文件: main.go 项目: boutros/marc
func main() {
	in := flag.String("i", "", "input file")
	f := flag.String("f", "", "output format: (m)arc, (l)ine-marc, marc(x)ml")

	flag.Parse()

	if *in == "" || *f == "" {
		flag.Usage()
		os.Exit(1)
	}

	inF, err := os.Open(*in)
	if err != nil {
		log.Fatal(err)
	}
	defer inF.Close()

	from, err := detectFormat(inF)
	if err != nil {
		log.Fatalf("%s: %v", inF.Name(), err)
	}

	var to marc.Format
	switch *f {
	case "m", "M":
		to = marc.MARC
	case "l", "L":
		to = marc.LineMARC
	case "x", "X":
		to = marc.MARCXML
	default:
		log.Println("illegal option for flag -f")
		flag.Usage()
		os.Exit(1)
	}

	if from == to {
		log.Println("nothing to do; input format same as output format")
		os.Exit(1)
	}

	dec := marc.NewDecoder(inF, from)
	enc := marc.NewEncoder(os.Stdout, to)

	for rec, err := dec.Decode(); err != io.EOF; rec, err = dec.Decode() {
		if err != nil {
			log.Println(err)
			continue
		}
		if err = enc.Encode(rec); err != nil {
			log.Println(err)
		}
	}
	enc.Flush()
}
示例#2
0
文件: main.go 项目: boutros/marc
func main() {
	log.SetFlags(0)
	log.SetPrefix("marcdump: ")
	var (
		useColors = flag.Bool("color", true, "use colored terminal output")
		//filter    = flag.String("filter", "", "only print specified fields, ex.: 100b,245a")
	)

	flag.Usage = func() {
		fmt.Fprintf(os.Stderr, "Usage: marcdump [options...] file\n\nOptions:\n")
		flag.PrintDefaults()
	}

	flag.Parse()

	if len(flag.Args()) == 0 {
		flag.Usage()
		os.Exit(1)
	}

	f, err := os.Open(flag.Args()[0])
	if err != nil {
		log.Fatal(err)
	}

	// Detect format
	sniff := make([]byte, 64)
	_, err = f.Read(sniff)
	if err != nil {
		log.Fatal(err)
	}
	format := marc.DetectFormat(sniff)
	switch format {
	case marc.MARC, marc.LineMARC, marc.MARCXML:
		break
	default:
		log.Fatal("Unknown MARC format")
	}

	// rewind reader
	_, err = f.Seek(0, 0)
	if err != nil {
		log.Fatal(err)
	}

	dec := marc.NewDecoder(f, format)
	for r, err := dec.Decode(); err != io.EOF; r, err = dec.Decode() {
		if err != nil {
			log.Fatal(err)
		}
		r.DumpTo(os.Stdout, *useColors)
	}

}
示例#3
0
文件: main.go 项目: boutros/marc
func main() {
	log.SetFlags(0)
	log.SetPrefix("marccheck: ")
	if len(os.Args) < 2 {
		fmt.Fprintf(os.Stderr, "Usage: marccheck <marcdatabase>\n")
		os.Exit(1)
	}

	f, err := os.Open(os.Args[1])
	if err != nil {
		log.Fatal(err)
	}
	stats, err := os.Stat(f.Name())
	if err != nil {
		log.Fatal(err)
	}
	size := stats.Size()

	// Detect format
	sniff := make([]byte, 64)
	_, err = f.Read(sniff)
	if err != nil {
		log.Fatal(err)
	}
	format := marc.DetectFormat(sniff)
	switch format {
	case marc.MARC, marc.LineMARC, marc.MARCXML:
		break
	default:
		log.Fatal("Unknown MARC format")
	}

	// rewind reader
	_, err = f.Seek(0, 0)
	if err != nil {
		log.Fatal(err)
	}

	dec := marc.NewDecoder(f, format)
	c := 0
	start := time.Now()

	for _, err := dec.Decode(); err != io.EOF; _, err = dec.Decode() {
		if err != nil {
			log.Fatal(err)
		}
		c++
	}
	fmt.Printf("Done in %s\n", time.Now().Sub(start))
	fmt.Printf("Number of records: %d\n", c)
	fmt.Printf("Average parsing speed: %.2f MB/s", float64(size)/time.Now().Sub(start).Seconds()/1048576)
}
示例#4
0
文件: main.go 项目: boutros/marc
func main() {
	log.SetFlags(0)
	log.SetPrefix("marcstats: ")

	tags := flag.String("t", "", "generate statistics from these tag contents (ex '100e,700a' )")
	flag.Parse()

	if len(flag.Args()) < 1 {
		fmt.Fprintf(os.Stderr, "Usage: marcstats -t<tags> <marcdatabase>\n")
		os.Exit(1)
	}

	f, err := os.Open(flag.Args()[0])
	if err != nil {
		log.Fatal(err)
	}

	// Detect format
	sniff := make([]byte, 64)
	_, err = f.Read(sniff)
	if err != nil {
		log.Fatal(err)
	}
	format := marc.DetectFormat(sniff)
	switch format {
	case marc.MARC, marc.LineMARC, marc.MARCXML:
		break
	default:
		log.Fatal("Unknown MARC format")
	}

	// rewind reader
	_, err = f.Seek(0, 0)
	if err != nil {
		log.Fatal(err)
	}

	dec := marc.NewDecoder(f, format)
	c := 0
	start := time.Now()
	stats := newStats()
	var filterTags []string
	if *tags != "" {
		filterTags = strings.Split(*tags, ",")
		for _, t := range filterTags {
			if len(t) < 4 {
				log.Fatalf("wrong tag filter format: %q (should be a three-digit number plus one or more subfieldcharacters, ex. \"100e,655ax\")", t)
			}
		}
	}
	filterStats := make(map[string]tagStats)

	for rec, err := dec.Decode(); err != io.EOF; rec, err = dec.Decode() {
		if err != nil {
			log.Fatal(err)
		}
		if len(filterTags) > 0 {
			filterCounts(filterTags, filterStats, rec)
		} else {
			if err := stats.countRecord(rec); err != nil {
				log.Println(err)
			}
		}
		c++
	}
	fmt.Printf("Done in %s\n", time.Now().Sub(start))
	fmt.Printf("Number of records: %d\n", c)
	if len(filterTags) > 0 {
		for tag, e := range filterStats {
			fmt.Println(tag)
			ranks := rankByWordCount(e)
			for _, p := range ranks {
				fmt.Printf("\t%6d %s\n", p.Value, p.Key)
			}
			fmt.Println()
		}
	} else {
		stats.Dump()
	}
}