File: load.cxx

package info (click to toggle)
latte 2.1-9
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,064 kB
  • ctags: 1,223
  • sloc: sh: 7,506; cpp: 5,831; ansic: 662; perl: 628; makefile: 350; yacc: 196; lisp: 114
file content (147 lines) | stat: -rw-r--r-- 3,416 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
// Copyright 1998,1999 Zanshin Inc.                  <http://www.zanshin.com/>

// The contents of this file are subject to the Zanshin Public License Version
// 1.0 (the "License"); you may not use this file except in compliance with the
// License.  You should have received a copy of the License with Latte; see
// the file COPYING.  You may also obtain a copy of the License at
// <http://www.zanshin.com/ZPL.html>.
// 
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
// for the specific language governing rights and limitations under the
// License.
// 
// The Original Code is Latte.
// 
// The Initial Developer of the Original Code is Zanshin, Inc.

#include <latte.h>
#include <latte-fstream.h>
#include <unistd.h>

using namespace std;

class LoadVisitor : public Latte_Visitor {
 public:
  void visit_str(Latte_Str &) {}
};

Refcounter<Latte_Obj>
latte_load_file(const shstring &filename,
                Latte_Activation &activation)
{
  LoadVisitor visitor;
  return latte_load_file(filename, activation, visitor);
}

Refcounter<Latte_Obj>
latte_load_file(const shstring &filename,
                Latte_Activation &activation,
                Latte_Visitor &visitor)
{
  ifstream in(filename.str().c_str());

  if (in.fail())
    throw Latte_FileError(filename.str());

  Latte_Reader reader(in, filename, activation);
  reader.process(visitor);
  in.close();

  return latte_true();
}

static Latte_Path path;

Latte_Path &
latte_path()
{
  return path;
}

void
latte_default_path()
{
  path.erase(path.begin(), path.end());

  const char *envpath = getenv("LATTE_PATH");

  if (envpath) {
    do {
      const char *colon = strchr(envpath, ':');
      if (colon) {
	path.push_back(shstring(envpath, colon - envpath));
	envpath = colon + 1;
      } else {
	path.push_back(envpath);
	envpath = 0;
      }
    } while (envpath);
  } else {

#if !defined(WIN32) && !defined(macintosh)
    path.push_back(latte_pkgdatadir);
    path.push_back(".");
#endif // !WIN32 && !macintosh

  }
}

#undef sep

#ifdef macintosh
# define sep ":"
# define ext ".latte"
#endif // macintosh

#ifdef WIN32
# define sep "\\"
# define ext ".lat"
#endif // WIN32

#ifndef sep
# define sep "/"
# define ext ".latte"
#endif // sep

Refcounter<Latte_Obj>
latte_load_library(const shstring &libname, Latte_Activation &activation)
{
  if (path.empty()) {
    latte_string filename = libname.str();

    if (!access(filename.c_str(), F_OK))
      return latte_load_file(filename, activation);

    filename += ext;

    if (!access(filename.c_str(), F_OK))
      return latte_load_file(filename, activation);
  } else {
    for (Latte_Path::const_iterator i = path.begin(); i != path.end(); ++i) {
      latte_string filename = (*i).str();
      filename += sep;
      filename += libname.str();

      if (!access(filename.c_str(), F_OK))
        return latte_load_file(filename, activation);

      filename += ext;

      if (!access(filename.c_str(), F_OK))
        return latte_load_file(filename, activation);
    }

    latte_string filename = libname.str();

    if (!access(filename.c_str(), F_OK))
      return latte_load_file(filename, activation);

    filename += ext;

    if (!access(filename.c_str(), F_OK))
      return latte_load_file(filename, activation);
  }

  throw Latte_FileError(libname.str());
}