1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
10 #include <sal/config.h>
11 #include <sal/log.hxx>
17 #include <osl/file.hxx>
18 #include <rtl/character.hxx>
19 #include <rtl/ref.hxx>
20 #include <rtl/ustrbuf.hxx>
21 #include <rtl/ustring.hxx>
22 #include <unoidl/unoidl.hxx>
24 #include "sourceprovider-scanner.hxx"
25 #include "sourcetreeprovider.hxx"
29 #include <osl/thread.h>
32 namespace unoidl
{ namespace detail
{
36 //TODO: Bad hack to work around osl::FileStatus::getFileName not determining the
37 // original spelling of a file name (not even with
38 // osl_FileStatus_Mask_Validate):
39 OUString
getFileName(OUString
const & uri
, osl::FileStatus
const & status
) {
41 sal_Int32 i
= uri
.lastIndexOf('/') + 1;
43 if (osl::FileBase::getSystemPathFromFileURL(uri
.copy(0, i
), path
)
44 != osl::FileBase::E_None
)
48 "cannot getSystemPathFromFileURL(" << uri
.copy(0, i
) << ")");
49 return status
.getFileName();
51 OString
dir(OUStringToOString(path
, osl_getThreadTextEncoding()));
52 OString
name(OUStringToOString(uri
.copy(i
), osl_getThreadTextEncoding()));
53 DIR * d
= opendir(dir
.getStr());
55 SAL_WARN("unoidl", "cannot opendir(" << dir
<< ")");
56 return status
.getFileName();
61 int e
= readdir_r(d
, &ent
, &p
);
63 SAL_WARN("unoidl", "cannot readdir_r");
65 return status
.getFileName();
69 "unoidl", "cannot find " << name
<< " via readdir of " << dir
);
71 return status
.getFileName();
73 if (name
.equalsIgnoreAsciiCase(p
->d_name
)) {
76 p
->d_name
, std::strlen(p
->d_name
), osl_getThreadTextEncoding());
81 return status
.getFileName();
85 bool exists(OUString
const & uri
, bool directory
) {
86 osl::DirectoryItem item
;
87 osl::FileStatus
status(
88 osl_FileStatus_Mask_Type
| osl_FileStatus_Mask_FileName
);
89 return osl::DirectoryItem::get(uri
, item
) == osl::FileBase::E_None
90 && item
.getFileStatus(status
) == osl::FileBase::E_None
91 && (status
.getFileType() == osl::FileStatus::Directory
) == directory
92 && getFileName(uri
, status
) == uri
.copy(uri
.lastIndexOf('/') + 1);
95 class Cursor
: public MapCursor
{
100 virtual ~Cursor() throw () override
{}
102 virtual rtl::Reference
<Entity
> getNext(OUString
*) override
103 { return rtl::Reference
<Entity
>(); } //TODO
106 class SourceModuleEntity
: public ModuleEntity
{
108 SourceModuleEntity() {}
111 virtual ~SourceModuleEntity() throw () override
{}
113 virtual std::vector
<OUString
> getMemberNames() const override
114 { return std::vector
<OUString
>(); } //TODO
116 virtual rtl::Reference
< MapCursor
> createCursor() const override
117 { return new Cursor
; }
122 SourceTreeProvider::SourceTreeProvider(Manager
& manager
, OUString
const & uri
):
123 manager_(manager
), uri_(uri
.endsWith("/") ? uri
: uri
+ "/")
126 rtl::Reference
<MapCursor
> SourceTreeProvider::createRootCursor() const {
130 rtl::Reference
<Entity
> SourceTreeProvider::findEntity(OUString
const & name
)
133 std::map
< OUString
, rtl::Reference
<Entity
> >::iterator
ci(
135 if (ci
!= cache_
.end()) {
138 // Match name against
139 // name ::= identifier ("." identifier)*
140 // identifier ::= upper-blocks | lower-block
141 // upper-blocks ::= upper ("_"? alnum)*
142 // lower-block :== lower alnum*
143 // alnum ::= digit | upper | lower
144 // digit ::= "0"--"9"
145 // upper ::= "A"--"Z"
146 // lower ::= "a"--"z"
147 OUStringBuffer
buf(name
);
150 for (; i
!= name
.getLength(); ++i
) {
151 sal_Unicode c
= name
[i
];
153 assert(i
== start
|| i
!= 0);
154 if (i
== start
|| name
[i
- 1] == '_') {
155 throw FileFormatException( //TODO
156 "", "Illegal UNOIDL identifier \"" + name
+ "\"");
160 } else if (c
== '_') {
161 assert(i
== start
|| i
!= 0);
162 if (i
== start
|| name
[i
- 1] == '_'
163 || !rtl::isAsciiUpperCase(name
[start
]))
165 throw FileFormatException( //TODO
166 "", "Illegal UNOIDL identifier \"" + name
+ "\"");
168 } else if (rtl::isAsciiDigit(c
)) {
170 throw FileFormatException( //TODO
171 "", "Illegal UNOIDL identifier \"" + name
+ "\"");
173 } else if (!rtl::isAsciiAlpha(c
)) {
174 throw FileFormatException( //TODO
175 "", "Illegal UNOIDL identifier \"" + name
+ "\"");
179 throw FileFormatException( //TODO
180 "", "Illegal UNOIDL identifier \"" + name
+ "\"");
182 OUString
uri(uri_
+ buf
.makeStringAndClear());
183 rtl::Reference
<Entity
> ent
;
184 // Prevent conflicts between foo/ and Foo.idl on case-preserving file
186 if (exists(uri
, true) && !exists(uri
+ ".idl", false)) {
187 ent
= new SourceModuleEntity
;
190 SourceProviderScannerData
data(&manager_
);
191 if (parse(uri
, &data
)) {
192 std::map
<OUString
, SourceProviderEntity
>::const_iterator
j(
193 data
.entities
.find(name
));
194 if (j
!= data
.entities
.end()) {
195 ent
= j
->second
.entity
;
199 "<" << uri
<< "> does not define entity " << name
);
202 cache_
.emplace(name
, ent
);
206 SourceTreeProvider::~SourceTreeProvider() throw () {}
210 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */