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