1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include "psputil.hxx"
26 * string convenience routines
30 getHexValueOf (sal_Int32 nValue
, OStringBuffer
& pBuffer
)
32 const static sal_Char pHex
[0x10] = {
33 '0', '1', '2', '3', '4', '5', '6', '7',
34 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
36 pBuffer
.append(pHex
[(nValue
& 0xF0) >> 4]);
37 pBuffer
.append(pHex
[(nValue
& 0x0F) ]);
43 getAlignedHexValueOf (sal_Int32 nValue
, OStringBuffer
& pBuffer
)
46 bool bNegative
= nValue
< 0;
47 nValue
= bNegative
? -nValue
: nValue
;
49 // get required buffer size, must be a multiple of two
57 if (nValue
< 0x800000)
62 // convert the int into its hex representation, write it into the buffer
63 sal_Int32 nRet
= nPrecision
;
64 auto const start
= pBuffer
.getLength();
67 OStringBuffer scratch
;
68 nPrecision
-= getHexValueOf (nValue
% 256, scratch
);
69 pBuffer
.insert(start
, scratch
.makeStringAndClear());
76 switch (pBuffer
[start
])
78 case '0' : pBuffer
[start
] = '8'; break;
79 case '1' : pBuffer
[start
] = '9'; break;
80 case '2' : pBuffer
[start
] = 'A'; break;
81 case '3' : pBuffer
[start
] = 'B'; break;
82 case '4' : pBuffer
[start
] = 'C'; break;
83 case '5' : pBuffer
[start
] = 'D'; break;
84 case '6' : pBuffer
[start
] = 'E'; break;
85 case '7' : pBuffer
[start
] = 'F'; break;
86 default: OSL_FAIL("Already a signed value");
95 getValueOf (sal_Int32 nValue
, OStringBuffer
& pBuffer
)
112 sal_Char pInvBuffer
[32];
113 sal_Int32 nInvChar
= 0;
116 pInvBuffer
[nInvChar
++] = '0' + nValue
% 10;
121 pBuffer
.append(pInvBuffer
[--nInvChar
]);
129 appendStr (const sal_Char
* pSrc
, OStringBuffer
& pDst
)
131 sal_Int32 nBytes
= strlen (pSrc
);
132 pDst
.append(pSrc
, nBytes
);
138 * copy strings to file
142 WritePS (osl::File
* pFile
, const sal_Char
* pString
)
144 sal_uInt64 nInLength
= rtl_str_getLength (pString
);
145 sal_uInt64 nOutLength
= 0;
147 if (nInLength
> 0 && pFile
)
148 pFile
->write (pString
, nInLength
, nOutLength
);
150 return nInLength
== nOutLength
;
154 WritePS (osl::File
* pFile
, const sal_Char
* pString
, sal_uInt64 nInLength
)
156 sal_uInt64 nOutLength
= 0;
158 if (nInLength
> 0 && pFile
)
159 pFile
->write (pString
, nInLength
, nOutLength
);
161 return nInLength
== nOutLength
;
165 WritePS (osl::File
* pFile
, const OString
&rString
)
167 sal_uInt64 nInLength
= rString
.getLength();
168 sal_uInt64 nOutLength
= 0;
170 if (nInLength
> 0 && pFile
)
171 pFile
->write (rString
.getStr(), nInLength
, nOutLength
);
173 return nInLength
== nOutLength
;
177 WritePS (osl::File
* pFile
, const OUString
&rString
)
179 return WritePS (pFile
, OUStringToOString(rString
, RTL_TEXTENCODING_ASCII_US
));
182 } /* namespace psp */
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */