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: res.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 ************************************************************************/
31 #if OSL_DEBUG_LEVEL == 0
38 #include <interface.hxx>
45 static hash_map
< string
, string
>* pStringResources
= NULL
;
47 static string
getResFileName( const char* progname
)
49 string aRet
= progname
;
50 size_t pos
= aRet
.rfind( '/' );
51 // FIXME: search PATH if necessary
52 assert( pos
!= string::npos
);
54 aRet
.append( "/resource/crash_dump.res" );
59 static void filterString( string
& rString
)
61 static const char* pProductName
= getenv( "PRODUCTNAME" );
62 static int nProductLen
= pProductName
? strlen( pProductName
) : 0;
63 static const char* pProductVersion
= getenv( "PRODUCTVERSION" );
64 static int nVersionLen
= pProductVersion
? strlen( pProductVersion
) : 0;
66 // fill in eventually escaped characters
67 string::size_type pos
= 0;
68 while( (pos
= rString
.find( '\\' ) ) != string::npos
)
71 switch( rString
[pos
+1] )
73 case 't': cRep
= '\t';break;
74 case 'n': cRep
= '\n';break;
75 case 'r': cRep
= '\r';break;
76 case 'f': cRep
= '\f';break;
77 default: cRep
= rString
[pos
+1];
80 rString
.replace( pos
, 2, &cRep
, 1 );
82 while( (pos
= rString
.find( '~' ) ) != string::npos
)
84 // replace mnemonic marker
85 rString
.replace( pos
, 1, "_", 1 );
87 while( (pos
= rString
.find( "%PRODUCTNAME%" ) ) != string::npos
)
89 rString
.replace( pos
, 13, pProductName
? pProductName
: "OpenOffice" );
91 while( (pos
= rString
.find( "%PRODUCTVERSION%" ) ) != string::npos
)
93 rString
.replace( pos
, 16, pProductVersion
? pProductVersion
: "" );
95 // remove whitespace at end
96 pos
= rString
.find_last_not_of( "\r\n\t\f " );
97 if( pos
!= string::npos
)
98 rString
.erase( pos
+1 );
101 void StringResource::init( int argc
, char** argv
)
103 pStringResources
= new hash_map
< string
, string
>();
105 string aResFile
= getResFileName( argv
[0] );
107 FILE* fp
= fopen( aResFile
.c_str(), "r" );
113 while( ! feof( fp
) )
115 if( ! fgets( buf
, sizeof(buf
), fp
) )
118 char* pEq
= strchr( buf
, '=' );
119 if( ! pEq
|| *(pEq
+1) == 0 ) // invalid line
121 aKey
= string(buf
, pEq
-buf
);
123 while( (aValue
.empty() || aValue
[ aValue
.size()-1 ] != '\n') && ! feof( fp
) )
125 if( fgets( buf
, sizeof( buf
), fp
) )
126 aValue
.append( buf
);
128 filterString( aValue
);
129 (*pStringResources
)[aKey
] = aValue
;
135 const char* StringResource::get( const char* pKey
)
137 hash_map
< string
, string
>::const_iterator it
= pStringResources
->find( pKey
);
138 return (it
== pStringResources
->end()) ? "" : it
->second
.c_str();