コード例 #1
0
ファイル: file.go プロジェクト: huangjiasingle/gohper
func ReadProperties(fname string) (map[string]string, error) {
	props := make(map[string]string)
	err := file.Filter(fname, func(_ int, line []byte) ([]byte, error) {
		p := pair.Parse(unsafe2.String(line), "=").Trim()
		if p.HasKey() {
			props[p.Key] = strings2.TrimAfter(p.Value, "#")
		}

		return line, nil
	})

	return props, err
}
コード例 #2
0
ファイル: apidoc.go プロジェクト: peternoyes/apidoc
func process(path string, wg *sync.WaitGroup) {
	sectionState := PARSE_INIT
	dataState := PARSE_INIT
	var a *API
	var sec *section
	var category string = "global"

	dataStart := 0

	err := file.Filter(path, func(linum int, line []byte) error {
		line = bytes.Replace(line, tab, tabreplace, -1)
		originLine := string(line)

		if !bytes.HasPrefix(line, unsafe2.Bytes(comment)) {
			sectionState = PARSE_INIT
			return nil
		}

		line = bytes.TrimSpace(line[len(comment):])
		line = bytes.Replace(line, []byte{'\t'}, []byte("    "), -1)
		if len(line) == 0 {
			return nil
		}

		linestr := string(line)
		switch tag := matchTag(linestr); tag {
		case TAG_CATEGORY:
			category = strings.TrimSpace(linestr[tag.Strlen():])

		case TAG_API:
			a = &API{}
			sectionState = PARSE_API
			name := strings.TrimSpace(linestr[tag.Strlen():])
			names := strings2.SplitAndTrim(name, TAG_AT_CATEGORY)
			a.name = names[0]

			if len(names) > 1 {
				as.AddAPI(names[1], a)
			} else {
				as.AddAPI(category, a)
			}
		case TAG_SUBAPI:
			a = &API{}
			sectionState = PARSE_API
			a.name = strings.TrimSpace(linestr[tag.Strlen():])

			as.AddSubAPI(a.name, a)
		case TAG_APIINCL:
			if a != nil {
				a.subapis = append(a.subapis, strings2.SplitAndTrim(linestr[tag.Strlen():], ",")...)
			}
		case TAG_ENDAPI:
			sectionState = PARSE_INIT

		case TAG_HEADER:
			sec = &section{}
			sectionState = PARSE_BODY
			dataState = PARSE_HEADER

			as.AddSubHeader(strings.TrimSpace(linestr[tag.Strlen():]), sec)
		case TAG_HEADERINCL:
			if sec != nil {
				sec.subheaders = append(sec.subheaders, strings2.SplitAndTrim(linestr[tag.Strlen():], ",")...)
			}

		case TAG_RESP:
			if sectionState != PARSE_INIT {
				sectionState = PARSE_BODY
				dataState = PARSE_STATUS
				sec = &section{}
				a.resps = append(a.resps, sec)
			}
		case TAG_SUBRESP:
			name := strings.TrimSpace(linestr[tag.Strlen():])
			sectionState = PARSE_BODY
			dataState = PARSE_STATUS
			sec = &section{}
			as.AddSubResp(name, sec)
		case TAG_RESPINCL:
			if a != nil {
				a.subresps = append(a.subresps, strings2.SplitAndTrim(linestr[tag.Strlen():], ",")...)
			}

		case TAG_REQ:
			if sectionState != PARSE_INIT {
				sectionState = PARSE_BODY
				sec = &section{}
				a.req = sec
				dataState = PARSE_STATUS
			}

		default:
			if sectionState == PARSE_INIT {
				break
			} else if sectionState == PARSE_API {
				a.desc = append(a.desc, linestr)

				break
			}

			switch dataState {
			case PARSE_STATUS:
				sec.headerLine = linestr
				dataState = PARSE_HEADER

			case PARSE_HEADER:
				if !strings.HasPrefix(linestr, TAG_DATA.String()) {
					sec.headers = append(sec.headers, linestr)

					break
				}

				dataStart = strings.Index(originLine, TAG_DATA.String()) + TAG_DATA.Strlen()
				dataState = PARSE_DATA
				fallthrough

			case PARSE_DATA:
				if len(originLine) >= dataStart {
					sec.datas = append(sec.datas, originLine[dataStart:])
				} else {
					reportErrorln("skipped data line: %s:%d:%s", path, linum, originLine)
				}
			}
		}

		return nil
	})
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
	}

	wg.Done()
}