xref: /aoo42x/main/registry/tools/fileurl.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "fileurl.hxx"
29 
30 #include "rtl/ustring.hxx"
31 #include "osl/diagnose.h"
32 #include "osl/file.hxx"
33 #include "osl/process.h"
34 #include "osl/thread.h"
35 
36 #include <string.h>
37 
38 #ifdef SAL_UNX
39 #define SEPARATOR '/'
40 #else
41 #define SEPARATOR '\\'
42 #endif
43 
44 using rtl::OUString;
45 using osl::FileBase;
46 
47 namespace registry
48 {
49 namespace tools
50 {
51 
52 OUString convertToFileUrl(char const * filename, size_t length)
53 {
54     OUString const uFileName(filename, length, osl_getThreadTextEncoding());
55     if (strncmp(filename, "file://", 7) == 0)
56     {
57         // already a FileUrl.
58         return uFileName;
59     }
60 
61     OUString uFileUrl;
62     if (length > 0)
63     {
64         if ((filename[0] == '.') || (filename[0] != SEPARATOR))
65         {
66             // relative path name.
67             OUString uWorkingDir;
68             if (osl_getProcessWorkingDir(&uWorkingDir.pData) != osl_Process_E_None)
69             {
70                 OSL_ASSERT(false);
71             }
72             if (FileBase::getAbsoluteFileURL(uWorkingDir, uFileName, uFileUrl) != FileBase::E_None)
73             {
74                 OSL_ASSERT(false);
75             }
76         }
77         else
78         {
79             // absolute path name.
80             if (FileBase::getFileURLFromSystemPath(uFileName, uFileUrl) != FileBase::E_None)
81             {
82                 OSL_ASSERT(false);
83             }
84         }
85     }
86     return uFileUrl;
87 }
88 
89 } // namespace tools
90 } // namespace registry
91