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