OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [tags/] [gnu-dev/] [fsf-gcc-snapshot-1-mar-12/] [or1k-gcc/] [libgo/] [go/] [text/] [template/] [helper.go] - Diff between revs 747 and 783

Go to most recent revision | Only display areas with differences | Details | Blame | View Log

Rev 747 Rev 783
// Copyright 2011 The Go Authors. All rights reserved.
// Copyright 2011 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// license that can be found in the LICENSE file.
// Helper functions to make constructing templates easier.
// Helper functions to make constructing templates easier.
package template
package template
import (
import (
        "fmt"
        "fmt"
        "io/ioutil"
        "io/ioutil"
        "path/filepath"
        "path/filepath"
)
)
// Functions and methods to parse templates.
// Functions and methods to parse templates.
// Must is a helper that wraps a call to a function returning (*Template, error)
// Must is a helper that wraps a call to a function returning (*Template, error)
// and panics if the error is non-nil. It is intended for use in variable
// and panics if the error is non-nil. It is intended for use in variable
// initializations such as
// initializations such as
//      var t = template.Must(template.New("name").Parse("text"))
//      var t = template.Must(template.New("name").Parse("text"))
func Must(t *Template, err error) *Template {
func Must(t *Template, err error) *Template {
        if err != nil {
        if err != nil {
                panic(err)
                panic(err)
        }
        }
        return t
        return t
}
}
// ParseFiles creates a new Template and parses the template definitions from
// ParseFiles creates a new Template and parses the template definitions from
// the named files. The returned template's name will have the (base) name and
// the named files. The returned template's name will have the (base) name and
// (parsed) contents of the first file. There must be at least one file.
// (parsed) contents of the first file. There must be at least one file.
// If an error occurs, parsing stops and the returned *Template is nil.
// If an error occurs, parsing stops and the returned *Template is nil.
func ParseFiles(filenames ...string) (*Template, error) {
func ParseFiles(filenames ...string) (*Template, error) {
        return parseFiles(nil, filenames...)
        return parseFiles(nil, filenames...)
}
}
// ParseFiles parses the named files and associates the resulting templates with
// ParseFiles parses the named files and associates the resulting templates with
// t. If an error occurs, parsing stops and the returned template is nil;
// t. If an error occurs, parsing stops and the returned template is nil;
// otherwise it is t. There must be at least one file.
// otherwise it is t. There must be at least one file.
func (t *Template) ParseFiles(filenames ...string) (*Template, error) {
func (t *Template) ParseFiles(filenames ...string) (*Template, error) {
        return parseFiles(t, filenames...)
        return parseFiles(t, filenames...)
}
}
// parseFiles is the helper for the method and function. If the argument
// parseFiles is the helper for the method and function. If the argument
// template is nil, it is created from the first file.
// template is nil, it is created from the first file.
func parseFiles(t *Template, filenames ...string) (*Template, error) {
func parseFiles(t *Template, filenames ...string) (*Template, error) {
        if len(filenames) == 0 {
        if len(filenames) == 0 {
                // Not really a problem, but be consistent.
                // Not really a problem, but be consistent.
                return nil, fmt.Errorf("template: no files named in call to ParseFiles")
                return nil, fmt.Errorf("template: no files named in call to ParseFiles")
        }
        }
        for _, filename := range filenames {
        for _, filename := range filenames {
                b, err := ioutil.ReadFile(filename)
                b, err := ioutil.ReadFile(filename)
                if err != nil {
                if err != nil {
                        return nil, err
                        return nil, err
                }
                }
                s := string(b)
                s := string(b)
                name := filepath.Base(filename)
                name := filepath.Base(filename)
                // First template becomes return value if not already defined,
                // First template becomes return value if not already defined,
                // and we use that one for subsequent New calls to associate
                // and we use that one for subsequent New calls to associate
                // all the templates together. Also, if this file has the same name
                // all the templates together. Also, if this file has the same name
                // as t, this file becomes the contents of t, so
                // as t, this file becomes the contents of t, so
                //  t, err := New(name).Funcs(xxx).ParseFiles(name)
                //  t, err := New(name).Funcs(xxx).ParseFiles(name)
                // works. Otherwise we create a new template associated with t.
                // works. Otherwise we create a new template associated with t.
                var tmpl *Template
                var tmpl *Template
                if t == nil {
                if t == nil {
                        t = New(name)
                        t = New(name)
                }
                }
                if name == t.Name() {
                if name == t.Name() {
                        tmpl = t
                        tmpl = t
                } else {
                } else {
                        tmpl = t.New(name)
                        tmpl = t.New(name)
                }
                }
                _, err = tmpl.Parse(s)
                _, err = tmpl.Parse(s)
                if err != nil {
                if err != nil {
                        return nil, err
                        return nil, err
                }
                }
        }
        }
        return t, nil
        return t, nil
}
}
// ParseGlob creates a new Template and parses the template definitions from the
// ParseGlob creates a new Template and parses the template definitions from the
// files identified by the pattern, which must match at least one file. The
// files identified by the pattern, which must match at least one file. The
// returned template will have the (base) name and (parsed) contents of the
// returned template will have the (base) name and (parsed) contents of the
// first file matched by the pattern. ParseGlob is equivalent to calling
// first file matched by the pattern. ParseGlob is equivalent to calling
// ParseFiles with the list of files matched by the pattern.
// ParseFiles with the list of files matched by the pattern.
func ParseGlob(pattern string) (*Template, error) {
func ParseGlob(pattern string) (*Template, error) {
        return parseGlob(nil, pattern)
        return parseGlob(nil, pattern)
}
}
// ParseGlob parses the template definitions in the files identified by the
// ParseGlob parses the template definitions in the files identified by the
// pattern and associates the resulting templates with t. The pattern is
// pattern and associates the resulting templates with t. The pattern is
// processed by filepath.Glob and must match at least one file. ParseGlob is
// processed by filepath.Glob and must match at least one file. ParseGlob is
// equivalent to calling t.ParseFiles with the list of files matched by the
// equivalent to calling t.ParseFiles with the list of files matched by the
// pattern.
// pattern.
func (t *Template) ParseGlob(pattern string) (*Template, error) {
func (t *Template) ParseGlob(pattern string) (*Template, error) {
        return parseGlob(t, pattern)
        return parseGlob(t, pattern)
}
}
// parseGlob is the implementation of the function and method ParseGlob.
// parseGlob is the implementation of the function and method ParseGlob.
func parseGlob(t *Template, pattern string) (*Template, error) {
func parseGlob(t *Template, pattern string) (*Template, error) {
        filenames, err := filepath.Glob(pattern)
        filenames, err := filepath.Glob(pattern)
        if err != nil {
        if err != nil {
                return nil, err
                return nil, err
        }
        }
        if len(filenames) == 0 {
        if len(filenames) == 0 {
                return nil, fmt.Errorf("template: pattern matches no files: %#q", pattern)
                return nil, fmt.Errorf("template: pattern matches no files: %#q", pattern)
        }
        }
        return parseFiles(t, filenames...)
        return parseFiles(t, filenames...)
}
}
 
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.