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 24 #include <stdio.h> 25 26 #include <sal/main.h> 27 28 #include <rtl/ustrbuf.hxx> 29 #include <rtl/string.hxx> 30 31 using rtl::OUString; 32 using rtl::OUStringBuffer; 33 using rtl::OString; 34 SAL_IMPLEMENT_MAIN()35SAL_IMPLEMENT_MAIN() 36 { 37 // string concatination 38 39 sal_Int32 n = 42; 40 double pi = 3.14159; 41 42 // give it an initial size, should be a good guess. 43 // stringbuffer extends if necessary 44 OUStringBuffer buf( 128 ); 45 46 // append an ascii string 47 buf.appendAscii( "pi ( here " ); 48 49 // numbers can be simply appended 50 buf.append( pi ); 51 52 // lets the compiler count the stringlength, so this is more efficient than 53 // the above appendAscii call, where length of the string must be calculated at 54 // runtime 55 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" ) multiplied with " ) ); 56 buf.append( n ); 57 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM(" gives ") ); 58 buf.append( (double)( n * pi ) ); 59 buf.appendAscii( RTL_CONSTASCII_STRINGPARAM( "." ) ); 60 61 // now transfer the buffer into the string. 62 // afterwards buffer is empty and may be reused again ! 63 OUString string = buf.makeStringAndClear(); 64 65 // I could of course also used the OStringBuffer directly 66 OString oString = rtl::OUStringToOString( string , RTL_TEXTENCODING_ASCII_US ); 67 68 // just to print something 69 printf( "%s\n" ,oString.getStr() ); 70 71 return 0; 72 } 73 74