update dev300-m58
[ooovba.git] / cpputools / source / sp2bv / sp2bv.cxx
blob6a8cf78d28f28b871c58cdb3a9857e1529834ede
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: sp2bv.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include "osl/thread.h"
34 #include "osl/file.h"
35 #include "rtl/ustring.hxx"
36 #include "rtl/ustrbuf.h"
40 using namespace rtl;
42 static sal_Bool hasOption(char const * szOption, int argc, char** argv);
45 #define HELP_TEXT \
46 "SYNOPSIS \n\n" \
47 "\tsp2bv [-h] [-?] string \n\n" \
48 "DESCRIPTION\n\n" \
49 "\tsp2bv stands for \"system path to bootstrap variable\"." \
50 " First the system path is converted into a file URL. Then all " \
51 "characters which have a special meaning in bootstrap variables, " \
52 "such as \'$\' are escaped. The resulting string is written to " \
53 "stdout an can be assigned to a bootstrap variable.\n" \
54 "\n\n" \
55 "OPTIONS \n\n" \
56 "\tThe following options are supported: \n" \
57 "-?\n " \
58 "--help" \
59 " Display help information.\n"
64 int main(int argc, char **argv)
66 if( hasOption("--help",argc, argv) || hasOption("-h", argc, argv))
68 fprintf(stdout, HELP_TEXT);// default
69 return 0;
72 if (argc != 2)
74 fprintf(stdout, HELP_TEXT);
75 return -1;
78 rtl_uString* pPath = NULL;
79 rtl_string2UString( &pPath, argv[1], strlen(argv[1]),
80 osl_getThreadTextEncoding(),OSTRING_TO_OUSTRING_CVTFLAGS );
82 rtl_uString* pUrl = NULL;
83 if (osl_getFileURLFromSystemPath(pPath, &pUrl) != osl_File_E_None)
84 return -1;
85 //escape the special characters
87 sal_Unicode cEscapeChar = 0x5c;
88 rtl_uString* pBuffer = NULL;
89 sal_Int32 nCapacity = 255;
90 rtl_uString_new_WithLength( &pBuffer, nCapacity );
92 const sal_Unicode* pCur = pUrl->buffer;
93 for (int i = 0; i != pUrl->length; i++)
95 switch( *pCur)
97 case '$':
98 rtl_uStringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, &cEscapeChar, 1);
99 rtl_uStringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, pCur, 1);
100 break;
101 case '{':
102 case '}':
103 case '\\': fprintf(stderr, "sp2vb: file URL contains invalid characters!\n");
104 return -1;
105 default:
106 rtl_uStringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, pCur, 1);
108 pCur ++;
110 //convert back to byte string so that we can print it.
111 rtl_String* pBootVar = NULL;
112 rtl_uString2String( &pBootVar, pBuffer->buffer, pBuffer->length,
113 osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS);
115 fprintf(stdout, "%s", pBootVar->buffer);
116 fflush(stdout);
118 rtl_uString_release(pBuffer);
119 rtl_uString_release(pPath);
120 rtl_uString_release(pUrl);
121 rtl_string_release(pBootVar);
122 return 0;
127 static sal_Bool hasOption(char const * szOption, int argc, char** argv)
129 sal_Bool retVal= sal_False;
130 for(sal_Int16 i= 1; i < argc; i++)
132 if( ! strcmp(argv[i], szOption))
134 retVal= sal_True;
135 break;
138 return retVal;