Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / cpputools / source / sp2bv / sp2bv.cxx
blob9998e7cdb7c4c3ca3c3732ec09a0858929232952
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 #include <stdio.h>
20 #include <string.h>
21 #include <osl/thread.h>
22 #include <osl/file.h>
23 #include <rtl/ustrbuf.h>
25 static bool hasOption(char const * szOption, int argc, char** argv);
28 #define HELP_TEXT \
29 "SYNOPSIS \n\n" \
30 "\tsp2bv [-h] [-?] string \n\n" \
31 "DESCRIPTION\n\n" \
32 "\tsp2bv stands for \"system path to bootstrap variable\"." \
33 " First the system path is converted into a file URL. Then all " \
34 "characters which have a special meaning in bootstrap variables, " \
35 "such as \'$\' are escaped. The resulting string is written to " \
36 "stdout and can be assigned to a bootstrap variable.\n" \
37 "\n\n" \
38 "OPTIONS \n\n" \
39 "\tThe following options are supported: \n" \
40 "-?\n " \
41 "--help" \
42 " Display help information.\n"
45 int main(int argc, char **argv)
47 if( hasOption("--help",argc, argv) || hasOption("-h", argc, argv))
49 fprintf(stdout, HELP_TEXT);// default
50 return 0;
53 if (argc != 2)
55 fprintf(stdout, HELP_TEXT);
56 return -1;
59 rtl_uString* pPath = nullptr;
60 rtl_string2UString( &pPath, argv[1], strlen(argv[1]),
61 osl_getThreadTextEncoding(),OSTRING_TO_OUSTRING_CVTFLAGS );
63 rtl_uString* pUrl = nullptr;
64 if (osl_getFileURLFromSystemPath(pPath, &pUrl) != osl_File_E_None)
65 return -1;
66 //escape the special characters
68 sal_Unicode cEscapeChar = 0x5c;
69 rtl_uString* pBuffer = nullptr;
70 sal_Int32 nCapacity = 255;
71 rtl_uString_new_WithLength( &pBuffer, nCapacity );
73 const sal_Unicode* pCur = pUrl->buffer;
74 for (int i = 0; i != pUrl->length; i++)
76 switch( *pCur)
78 case '$':
79 rtl_uStringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, &cEscapeChar, 1);
80 rtl_uStringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, pCur, 1);
81 break;
82 case '{':
83 case '}':
84 case '\\': fprintf(stderr, "sp2vb: file URL contains invalid characters!\n");
85 return -1;
86 default:
87 rtl_uStringbuffer_insert( &pBuffer, &nCapacity, pBuffer->length, pCur, 1);
89 pCur ++;
91 //convert back to byte string so that we can print it.
92 rtl_String* pBootVar = nullptr;
93 rtl_uString2String( &pBootVar, pBuffer->buffer, pBuffer->length,
94 osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS);
96 fprintf(stdout, "%s", pBootVar->buffer);
97 fflush(stdout);
99 rtl_uString_release(pBuffer);
100 rtl_uString_release(pPath);
101 rtl_uString_release(pUrl);
102 rtl_string_release(pBootVar);
103 return 0;
107 static bool hasOption(char const * szOption, int argc, char** argv)
109 bool retVal = false;
110 for(int i= 1; i < argc; i++)
112 if( ! strcmp(argv[i], szOption))
114 retVal = true;
115 break;
118 return retVal;
122 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */