1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: sp2bv.cxx,v $
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 ************************************************************************/
33 #include "osl/thread.h"
35 #include "rtl/ustring.hxx"
36 #include "rtl/ustrbuf.h"
42 static sal_Bool
hasOption(char const * szOption
, int argc
, char** argv
);
47 "\tsp2bv [-h] [-?] string \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" \
56 "\tThe following options are supported: \n" \
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
74 fprintf(stdout
, HELP_TEXT
);
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
)
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
++)
98 rtl_uStringbuffer_insert( &pBuffer
, &nCapacity
, pBuffer
->length
, &cEscapeChar
, 1);
99 rtl_uStringbuffer_insert( &pBuffer
, &nCapacity
, pBuffer
->length
, pCur
, 1);
103 case '\\': fprintf(stderr
, "sp2vb: file URL contains invalid characters!\n");
106 rtl_uStringbuffer_insert( &pBuffer
, &nCapacity
, pBuffer
->length
, pCur
, 1);
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
);
118 rtl_uString_release(pBuffer
);
119 rtl_uString_release(pPath
);
120 rtl_uString_release(pUrl
);
121 rtl_string_release(pBootVar
);
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
))