xref: /trunk/main/cpputools/source/sp2bv/sp2bv.cxx (revision 01797804)
1*01797804SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*01797804SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*01797804SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*01797804SAndrew Rist  * distributed with this work for additional information
6*01797804SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*01797804SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*01797804SAndrew Rist  * "License"); you may not use this file except in compliance
9*01797804SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*01797804SAndrew Rist  *
11*01797804SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*01797804SAndrew Rist  *
13*01797804SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*01797804SAndrew Rist  * software distributed under the License is distributed on an
15*01797804SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*01797804SAndrew Rist  * KIND, either express or implied.  See the License for the
17*01797804SAndrew Rist  * specific language governing permissions and limitations
18*01797804SAndrew Rist  * under the License.
19*01797804SAndrew Rist  *
20*01797804SAndrew Rist  *************************************************************/
21*01797804SAndrew Rist 
22*01797804SAndrew Rist 
23cdf0e10cSrcweir #include <stdio.h>
24cdf0e10cSrcweir #include <stdlib.h>
25cdf0e10cSrcweir #include <string.h>
26cdf0e10cSrcweir #include "osl/thread.h"
27cdf0e10cSrcweir #include "osl/file.h"
28cdf0e10cSrcweir #include "rtl/ustring.hxx"
29cdf0e10cSrcweir #include  "rtl/ustrbuf.h"
30cdf0e10cSrcweir 
31cdf0e10cSrcweir 
32cdf0e10cSrcweir 
33cdf0e10cSrcweir using namespace rtl;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir static sal_Bool hasOption(char const * szOption, int argc, char** argv);
36cdf0e10cSrcweir 
37cdf0e10cSrcweir 
38cdf0e10cSrcweir #define HELP_TEXT    \
39cdf0e10cSrcweir "SYNOPSIS \n\n" \
40cdf0e10cSrcweir "\tsp2bv [-h] [-?] string \n\n" \
41cdf0e10cSrcweir "DESCRIPTION\n\n" \
42cdf0e10cSrcweir "\tsp2bv stands for \"system path to bootstrap variable\"." \
43cdf0e10cSrcweir " First the system path is converted into a file URL. Then all " \
44cdf0e10cSrcweir "characters which have a special meaning in bootstrap variables, " \
45cdf0e10cSrcweir "such as \'$\' are escaped. The resulting string is written to " \
46cdf0e10cSrcweir "stdout an can be assigned to a bootstrap variable.\n" \
47cdf0e10cSrcweir "\n\n" \
48cdf0e10cSrcweir "OPTIONS \n\n" \
49cdf0e10cSrcweir "\tThe following options are supported: \n" \
50cdf0e10cSrcweir "-?\n " \
51cdf0e10cSrcweir "--help" \
52cdf0e10cSrcweir "    Display help information.\n"
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 
55cdf0e10cSrcweir 
56cdf0e10cSrcweir 
main(int argc,char ** argv)57cdf0e10cSrcweir int main(int argc, char **argv)
58cdf0e10cSrcweir {
59cdf0e10cSrcweir     if( hasOption("--help",argc, argv) || hasOption("-h", argc, argv))
60cdf0e10cSrcweir     {
61cdf0e10cSrcweir         fprintf(stdout, HELP_TEXT);// default
62cdf0e10cSrcweir         return 0;
63cdf0e10cSrcweir     }
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     if (argc != 2)
66cdf0e10cSrcweir 	{
67cdf0e10cSrcweir         fprintf(stdout, HELP_TEXT);
68cdf0e10cSrcweir 		return -1;
69cdf0e10cSrcweir 	}
70cdf0e10cSrcweir 
71cdf0e10cSrcweir     rtl_uString* pPath = NULL;
72cdf0e10cSrcweir     rtl_string2UString( &pPath, argv[1], strlen(argv[1]),
73cdf0e10cSrcweir                         osl_getThreadTextEncoding(),OSTRING_TO_OUSTRING_CVTFLAGS );
74cdf0e10cSrcweir 
75cdf0e10cSrcweir     rtl_uString* pUrl = NULL;
76cdf0e10cSrcweir     if (osl_getFileURLFromSystemPath(pPath, &pUrl) != osl_File_E_None)
77cdf0e10cSrcweir         return -1;
78cdf0e10cSrcweir //escape the special characters
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     sal_Unicode cEscapeChar = 0x5c;
81cdf0e10cSrcweir     rtl_uString* pBuffer = NULL;
82cdf0e10cSrcweir     sal_Int32 nCapacity = 255;
83cdf0e10cSrcweir     rtl_uString_new_WithLength( &pBuffer, nCapacity );
84cdf0e10cSrcweir 
85cdf0e10cSrcweir     const sal_Unicode* pCur = pUrl->buffer;
86cdf0e10cSrcweir     for (int i = 0; i != pUrl->length; i++)
87cdf0e10cSrcweir     {
88cdf0e10cSrcweir         switch( *pCur)
89cdf0e10cSrcweir         {
90cdf0e10cSrcweir         case '$':
91cdf0e10cSrcweir             rtl_uStringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, &cEscapeChar, 1);
92cdf0e10cSrcweir             rtl_uStringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, pCur, 1);
93cdf0e10cSrcweir             break;
94cdf0e10cSrcweir         case '{':
95cdf0e10cSrcweir         case '}':
96cdf0e10cSrcweir         case '\\': fprintf(stderr, "sp2vb: file URL contains invalid characters!\n");
97cdf0e10cSrcweir             return -1;
98cdf0e10cSrcweir         default:
99cdf0e10cSrcweir             rtl_uStringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, pCur, 1);
100cdf0e10cSrcweir         }
101cdf0e10cSrcweir 		pCur ++;
102cdf0e10cSrcweir     }
103cdf0e10cSrcweir //convert back to byte string so that we can print it.
104cdf0e10cSrcweir     rtl_String* pBootVar = NULL;
105cdf0e10cSrcweir     rtl_uString2String( &pBootVar, pBuffer->buffer, pBuffer->length,
106cdf0e10cSrcweir                         osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS);
107cdf0e10cSrcweir 
108cdf0e10cSrcweir     fprintf(stdout, "%s", pBootVar->buffer);
109cdf0e10cSrcweir     fflush(stdout);
110cdf0e10cSrcweir 
111cdf0e10cSrcweir     rtl_uString_release(pBuffer);
112cdf0e10cSrcweir     rtl_uString_release(pPath);
113cdf0e10cSrcweir     rtl_uString_release(pUrl);
114cdf0e10cSrcweir     rtl_string_release(pBootVar);
115cdf0e10cSrcweir     return 0;
116cdf0e10cSrcweir }
117cdf0e10cSrcweir 
118cdf0e10cSrcweir 
119cdf0e10cSrcweir 
hasOption(char const * szOption,int argc,char ** argv)120cdf0e10cSrcweir static sal_Bool hasOption(char const * szOption, int argc, char** argv)
121cdf0e10cSrcweir {
122cdf0e10cSrcweir     sal_Bool retVal= sal_False;
123cdf0e10cSrcweir     for(sal_Int16 i= 1; i < argc; i++)
124cdf0e10cSrcweir     {
125cdf0e10cSrcweir         if( ! strcmp(argv[i], szOption))
126cdf0e10cSrcweir         {
127cdf0e10cSrcweir             retVal= sal_True;
128cdf0e10cSrcweir             break;
129cdf0e10cSrcweir         }
130cdf0e10cSrcweir     }
131cdf0e10cSrcweir     return retVal;
132cdf0e10cSrcweir }
133cdf0e10cSrcweir 
134cdf0e10cSrcweir 
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 
137cdf0e10cSrcweir 
138