package model
import (
"bufio"
"io"
"os"
"strings"
)
type File struct {
abspath string
relpath string
}
func NewFile(abs, rel string) *File {
return &File{
abspath: abs,
relpath: rel,
}
}
func (file *File) Abspath() string {
if file != nil {
return file.abspath
}
return ""
}
func (file *File) Relpath() string {
if file != nil {
return file.relpath
}
return ""
}
func (file *File) String() string {
return file.Relpath()
}
func (file *File) OpenReader(do func(reader io.Reader)) error {
if file == nil || file.abspath == "" {
return nil
}
f, err := os.Open(file.abspath)
if err != nil {
return err
}
defer f.Close()
do(f)
return nil
}
func (file File) ReadLine(do func(line string)) {
file.OpenReader(func(reader io.Reader) {
ReadLine(reader, do)
})
}
func (file File) ReadLineNoComment(t *CommentType, do func(line string)) {
file.OpenReader(func(reader io.Reader) {
ReadLineNoComment(reader, t, do)
})
}
type CommentType struct {
Simple string
Begin string
End string
}
var (
CTypeComment = &CommentType{
Simple: "//",
Begin: "/*",
End: "*/",
}
PythonTypeComment = &CommentType{
Simple: "#",
Begin: "'''",
End: "'''",
}
)
func ReadLine(reader io.Reader, do func(line string)) {
if do == nil {
return
}
scanner := bufio.NewScanner(reader)
for scanner.Scan() {
do(strings.TrimRight(scanner.Text(), "\n\r"))
}
}
func ReadLineNoComment(reader io.Reader, t *CommentType, do func(line string)) {
if do == nil {
return
}
if t == nil {
t = CTypeComment
}
comment := false
ReadLine(reader, func(line string) {
if t.Simple != "" {
i := strings.Index(line, t.Simple)
if i != -1 {
line = line[:i]
}
}
if t.Begin != "" && t.End != "" {
for {
if start_i := strings.Index(line, t.Begin); !comment && start_i != -1 {
comment = true
do(line[:start_i])
line = line[start_i+len(t.Begin):]
continue
}
if end_i := strings.Index(line, t.End); comment && end_i != -1 {
comment = false
line = line[end_i+len(t.End):]
continue
}
break
}
if comment {
return
}
}
do(line)
})
}
type ResCallback func(file *File, root ...*DepGraph)