xref: /aoo42x/main/cpputools/source/sp2bv/sp2bv.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 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include "osl/thread.h"
31 #include "osl/file.h"
32 #include "rtl/ustring.hxx"
33 #include  "rtl/ustrbuf.h"
34 
35 
36 
37 using namespace rtl;
38 
39 static sal_Bool hasOption(char const * szOption, int argc, char** argv);
40 
41 
42 #define HELP_TEXT    \
43 "SYNOPSIS \n\n" \
44 "\tsp2bv [-h] [-?] string \n\n" \
45 "DESCRIPTION\n\n" \
46 "\tsp2bv stands for \"system path to bootstrap variable\"." \
47 " First the system path is converted into a file URL. Then all " \
48 "characters which have a special meaning in bootstrap variables, " \
49 "such as \'$\' are escaped. The resulting string is written to " \
50 "stdout an can be assigned to a bootstrap variable.\n" \
51 "\n\n" \
52 "OPTIONS \n\n" \
53 "\tThe following options are supported: \n" \
54 "-?\n " \
55 "--help" \
56 "    Display help information.\n"
57 
58 
59 
60 
61 int main(int argc, char **argv)
62 {
63     if( hasOption("--help",argc, argv) || hasOption("-h", argc, argv))
64     {
65         fprintf(stdout, HELP_TEXT);// default
66         return 0;
67     }
68 
69     if (argc != 2)
70 	{
71         fprintf(stdout, HELP_TEXT);
72 		return -1;
73 	}
74 
75     rtl_uString* pPath = NULL;
76     rtl_string2UString( &pPath, argv[1], strlen(argv[1]),
77                         osl_getThreadTextEncoding(),OSTRING_TO_OUSTRING_CVTFLAGS );
78 
79     rtl_uString* pUrl = NULL;
80     if (osl_getFileURLFromSystemPath(pPath, &pUrl) != osl_File_E_None)
81         return -1;
82 //escape the special characters
83 
84     sal_Unicode cEscapeChar = 0x5c;
85     rtl_uString* pBuffer = NULL;
86     sal_Int32 nCapacity = 255;
87     rtl_uString_new_WithLength( &pBuffer, nCapacity );
88 
89     const sal_Unicode* pCur = pUrl->buffer;
90     for (int i = 0; i != pUrl->length; i++)
91     {
92         switch( *pCur)
93         {
94         case '$':
95             rtl_uStringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, &cEscapeChar, 1);
96             rtl_uStringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, pCur, 1);
97             break;
98         case '{':
99         case '}':
100         case '\\': fprintf(stderr, "sp2vb: file URL contains invalid characters!\n");
101             return -1;
102         default:
103             rtl_uStringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, pCur, 1);
104         }
105 		pCur ++;
106     }
107 //convert back to byte string so that we can print it.
108     rtl_String* pBootVar = NULL;
109     rtl_uString2String( &pBootVar, pBuffer->buffer, pBuffer->length,
110                         osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS);
111 
112     fprintf(stdout, "%s", pBootVar->buffer);
113     fflush(stdout);
114 
115     rtl_uString_release(pBuffer);
116     rtl_uString_release(pPath);
117     rtl_uString_release(pUrl);
118     rtl_string_release(pBootVar);
119     return 0;
120 }
121 
122 
123 
124 static sal_Bool hasOption(char const * szOption, int argc, char** argv)
125 {
126     sal_Bool retVal= sal_False;
127     for(sal_Int16 i= 1; i < argc; i++)
128     {
129         if( ! strcmp(argv[i], szOption))
130         {
131             retVal= sal_True;
132             break;
133         }
134     }
135     return retVal;
136 }
137 
138 
139 
140 
141 
142