github.com/gotranspile/cxgo@v0.3.7/vfs.go (about) 1 package cxgo 2 3 import ( 4 "io" 5 "io/ioutil" 6 "os" 7 "strings" 8 "time" 9 10 "github.com/gotranspile/cxgo/libs" 11 "modernc.org/cc/v3" 12 ) 13 14 var timeRun = time.Now() 15 16 func newIncludeFS(c *libs.Env) cc.Filesystem { 17 return includeFS{c: c} 18 } 19 20 type includeFS struct { 21 c *libs.Env 22 } 23 24 func (fs includeFS) content(path string, sys bool) (string, error) { 25 if !sys { 26 return "", os.ErrNotExist 27 } 28 l, ok := fs.c.NewLibrary(path) 29 if !ok { 30 return "", os.ErrNotExist 31 } 32 return l.Header, nil 33 } 34 35 func (fs includeFS) Stat(path string, sys bool) (os.FileInfo, error) { 36 data, err := fs.content(path, sys) 37 if err != nil { 38 return nil, err 39 } 40 return includeFI{name: path, data: data}, nil 41 } 42 43 func (fs includeFS) Open(path string, sys bool) (io.ReadCloser, error) { 44 data, err := fs.content(path, sys) 45 if err != nil { 46 return nil, err 47 } 48 return ioutil.NopCloser(strings.NewReader(data)), nil 49 } 50 51 type includeFI struct { 52 name string 53 data string 54 } 55 56 func (fi includeFI) Name() string { 57 return fi.name 58 } 59 60 func (fi includeFI) Size() int64 { 61 return int64(len(fi.data)) 62 } 63 64 func (fi includeFI) Mode() os.FileMode { 65 return 0 66 } 67 68 func (fi includeFI) ModTime() time.Time { 69 return timeRun 70 } 71 72 func (fi includeFI) IsDir() bool { 73 return false 74 } 75 76 func (fi includeFI) Sys() interface{} { 77 return fi 78 }