Version 6.4.0.0.beta1, tag libreoffice-6.4.0.0.beta1
[LibreOffice.git] / vcl / unx / generic / print / psputil.cxx
blobeefbc23d4353b4febf6f09ecab44419f9c14c5eb
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 .
20 #include <string.h>
21 #include "psputil.hxx"
23 namespace psp {
26 * string convenience routines
29 sal_Int32
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) ]);
39 return 2;
42 sal_Int32
43 getAlignedHexValueOf (sal_Int32 nValue, OStringBuffer& pBuffer)
45 // get sign
46 bool bNegative = nValue < 0;
47 nValue = bNegative ? -nValue : nValue;
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;
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)
67 OStringBuffer scratch;
68 nPrecision -= getHexValueOf (nValue % 256, scratch );
69 pBuffer.insert(start, scratch.makeStringAndClear());
70 nValue /= 256;
73 // set sign bit
74 if (bNegative)
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");
90 // report precision
91 return nRet;
94 sal_Int32
95 getValueOf (sal_Int32 nValue, OStringBuffer& pBuffer)
97 sal_Int32 nChar = 0;
98 if (nValue < 0)
100 pBuffer.append('-');
101 ++nChar;
102 nValue *= -1;
104 else
105 if (nValue == 0)
107 pBuffer.append('0');
108 ++nChar;
109 return nChar;
112 sal_Char pInvBuffer [32];
113 sal_Int32 nInvChar = 0;
114 while (nValue > 0)
116 pInvBuffer [nInvChar++] = '0' + nValue % 10;
117 nValue /= 10;
119 while (nInvChar > 0)
121 pBuffer.append(pInvBuffer [--nInvChar]);
122 ++nChar;
125 return nChar;
128 sal_Int32
129 appendStr (const sal_Char* pSrc, OStringBuffer& pDst)
131 sal_Int32 nBytes = strlen (pSrc);
132 pDst.append(pSrc, nBytes);
134 return nBytes;
138 * copy strings to file
141 bool
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;
153 bool
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;
164 bool
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;
176 bool
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: */