1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20 #include <string.h>
21 #include "psputil.hxx"
22
23 namespace psp {
24
25 /*
26 * string convenience routines
27 */
28
29 sal_Int32
getHexValueOf(sal_Int32 nValue,OStringBuffer & pBuffer)30 getHexValueOf (sal_Int32 nValue, OStringBuffer& pBuffer)
31 {
32 const static char pHex [0x10] = {
33 '0', '1', '2', '3', '4', '5', '6', '7',
34 '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
35
36 pBuffer.append(pHex [(nValue & 0xF0) >> 4]);
37 pBuffer.append(pHex [(nValue & 0x0F) ]);
38
39 return 2;
40 }
41
42 sal_Int32
getAlignedHexValueOf(sal_Int32 nValue,OStringBuffer & pBuffer)43 getAlignedHexValueOf (sal_Int32 nValue, OStringBuffer& pBuffer)
44 {
45 // get sign
46 bool bNegative = nValue < 0;
47 nValue = bNegative ? -nValue : nValue;
48
49 // get required buffer size, must be a multiple of two
50 sal_Int32 nPrecision;
51 if (nValue < 0x80)
52 nPrecision = 2;
53 else
54 if (nValue < 0x8000)
55 nPrecision = 4;
56 else
57 if (nValue < 0x800000)
58 nPrecision = 6;
59 else
60 nPrecision = 8;
61
62 // convert the int into its hex representation, write it into the buffer
63 sal_Int32 nRet = nPrecision;
64 auto const start = pBuffer.getLength();
65 while (nPrecision)
66 {
67 OStringBuffer scratch;
68 nPrecision -= getHexValueOf (nValue % 256, scratch );
69 pBuffer.insert(start, scratch.makeStringAndClear());
70 nValue /= 256;
71 }
72
73 // set sign bit
74 if (bNegative)
75 {
76 switch (pBuffer[start])
77 {
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");
87 }
88 }
89
90 // report precision
91 return nRet;
92 }
93
94 sal_Int32
getValueOf(sal_Int32 nValue,OStringBuffer & pBuffer)95 getValueOf (sal_Int32 nValue, OStringBuffer& pBuffer)
96 {
97 sal_Int32 nChar = 0;
98 if (nValue < 0)
99 {
100 pBuffer.append('-');
101 ++nChar;
102 nValue *= -1;
103 }
104 else
105 if (nValue == 0)
106 {
107 pBuffer.append('0');
108 ++nChar;
109 return nChar;
110 }
111
112 char pInvBuffer [32];
113 sal_Int32 nInvChar = 0;
114 while (nValue > 0)
115 {
116 pInvBuffer [nInvChar++] = '0' + nValue % 10;
117 nValue /= 10;
118 }
119 while (nInvChar > 0)
120 {
121 pBuffer.append(pInvBuffer [--nInvChar]);
122 ++nChar;
123 }
124
125 return nChar;
126 }
127
128 sal_Int32
appendStr(const char * pSrc,OStringBuffer & pDst)129 appendStr (const char* pSrc, OStringBuffer& pDst)
130 {
131 sal_Int32 nBytes = strlen (pSrc);
132 pDst.append(pSrc, nBytes);
133
134 return nBytes;
135 }
136
137 /*
138 * copy strings to file
139 */
140
141 bool
WritePS(osl::File * pFile,const char * pString)142 WritePS (osl::File* pFile, const char* pString)
143 {
144 sal_uInt64 nInLength = rtl_str_getLength (pString);
145 sal_uInt64 nOutLength = 0;
146
147 if (nInLength > 0 && pFile)
148 pFile->write (pString, nInLength, nOutLength);
149
150 return nInLength == nOutLength;
151 }
152
153 bool
WritePS(osl::File * pFile,const char * pString,sal_uInt64 nInLength)154 WritePS (osl::File* pFile, const char* pString, sal_uInt64 nInLength)
155 {
156 sal_uInt64 nOutLength = 0;
157
158 if (nInLength > 0 && pFile)
159 pFile->write (pString, nInLength, nOutLength);
160
161 return nInLength == nOutLength;
162 }
163
164 bool
WritePS(osl::File * pFile,const OString & rString)165 WritePS (osl::File* pFile, const OString &rString)
166 {
167 sal_uInt64 nInLength = rString.getLength();
168 sal_uInt64 nOutLength = 0;
169
170 if (nInLength > 0 && pFile)
171 pFile->write (rString.getStr(), nInLength, nOutLength);
172
173 return nInLength == nOutLength;
174 }
175
176 bool
WritePS(osl::File * pFile,std::u16string_view rString)177 WritePS (osl::File* pFile, std::u16string_view rString)
178 {
179 return WritePS (pFile, OUStringToOString(rString, RTL_TEXTENCODING_ASCII_US));
180 }
181
182 } /* namespace psp */
183
184 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
185