xref: /aoo41x/main/registry/tools/fileurl.cxx (revision 51134e9e)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #include "fileurl.hxx"
25 
26 #include "rtl/ustring.hxx"
27 #include "osl/diagnose.h"
28 #include "osl/file.hxx"
29 #include "osl/process.h"
30 #include "osl/thread.h"
31 
32 #include <string.h>
33 
34 #ifdef SAL_UNX
35 #define SEPARATOR '/'
36 #else
37 #define SEPARATOR '\\'
38 #endif
39 
40 using rtl::OUString;
41 using osl::FileBase;
42 
43 namespace registry
44 {
45 namespace tools
46 {
47 
convertToFileUrl(char const * filename,size_t length)48 OUString convertToFileUrl(char const * filename, size_t length)
49 {
50     OUString const uFileName(filename, length, osl_getThreadTextEncoding());
51     if (strncmp(filename, "file://", 7) == 0)
52     {
53         // already a FileUrl.
54         return uFileName;
55     }
56 
57     OUString uFileUrl;
58     if (length > 0)
59     {
60         if ((filename[0] == '.') || (filename[0] != SEPARATOR))
61         {
62             // relative path name.
63             OUString uWorkingDir;
64             if (osl_getProcessWorkingDir(&uWorkingDir.pData) != osl_Process_E_None)
65             {
66                 OSL_ASSERT(false);
67             }
68             if (FileBase::getAbsoluteFileURL(uWorkingDir, uFileName, uFileUrl) != FileBase::E_None)
69             {
70                 OSL_ASSERT(false);
71             }
72         }
73         else
74         {
75             // absolute path name.
76             if (FileBase::getFileURLFromSystemPath(uFileName, uFileUrl) != FileBase::E_None)
77             {
78                 OSL_ASSERT(false);
79             }
80         }
81     }
82     return uFileUrl;
83 }
84 
85 } // namespace tools
86 } // namespace registry
87