1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is TransforMiiX XSLT processor code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * Jonas Sicking.
19 : * Portions created by the Initial Developer are Copyright (C) 2002
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Jonas Sicking <sicking@bigfoot.com>
24 : *
25 : * Alternatively, the contents of this file may be used under the terms of
26 : * either the GNU General Public License Version 2 or later (the "GPL"), or
27 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 : * in which case the provisions of the GPL or the LGPL are applicable instead
29 : * of those above. If you wish to allow use of your version of this file only
30 : * under the terms of either the GPL or the LGPL, and not to allow others to
31 : * use your version of this file under the terms of the MPL, indicate your
32 : * decision by deleting the provisions above and replace them with the notice
33 : * and other provisions required by the GPL or the LGPL. If you do not delete
34 : * the provisions above, a recipient may use your version of this file under
35 : * the terms of any one of the MPL, the GPL or the LGPL.
36 : *
37 : * ***** END LICENSE BLOCK ***** */
38 :
39 : #include "txXSLTNumber.h"
40 : #include "nsReadableUtils.h"
41 : #include "txCore.h"
42 :
43 0 : class txDecimalCounter : public txFormattedCounter {
44 : public:
45 0 : txDecimalCounter() : mMinLength(1), mGroupSize(50)
46 : {
47 0 : }
48 :
49 : txDecimalCounter(PRInt32 aMinLength, PRInt32 aGroupSize,
50 : const nsAString& mGroupSeparator);
51 :
52 : virtual void appendNumber(PRInt32 aNumber, nsAString& aDest);
53 :
54 : private:
55 : PRInt32 mMinLength;
56 : PRInt32 mGroupSize;
57 : nsString mGroupSeparator;
58 : };
59 :
60 0 : class txAlphaCounter : public txFormattedCounter {
61 : public:
62 0 : txAlphaCounter(PRUnichar aOffset) : mOffset(aOffset)
63 : {
64 0 : }
65 :
66 : virtual void appendNumber(PRInt32 aNumber, nsAString& aDest);
67 :
68 : private:
69 : PRUnichar mOffset;
70 : };
71 :
72 0 : class txRomanCounter : public txFormattedCounter {
73 : public:
74 0 : txRomanCounter(bool aUpper) : mTableOffset(aUpper ? 30 : 0)
75 : {
76 0 : }
77 :
78 : void appendNumber(PRInt32 aNumber, nsAString& aDest);
79 :
80 : private:
81 : PRInt32 mTableOffset;
82 : };
83 :
84 :
85 : nsresult
86 0 : txFormattedCounter::getCounterFor(const nsAFlatString& aToken,
87 : PRInt32 aGroupSize,
88 : const nsAString& aGroupSeparator,
89 : txFormattedCounter*& aCounter)
90 : {
91 0 : PRInt32 length = aToken.Length();
92 0 : NS_ASSERTION(length, "getting counter for empty token");
93 0 : aCounter = 0;
94 :
95 0 : if (length == 1) {
96 0 : PRUnichar ch = aToken.CharAt(0);
97 0 : switch (ch) {
98 :
99 : case 'i':
100 : case 'I':
101 0 : aCounter = new txRomanCounter(ch == 'I');
102 0 : break;
103 :
104 : case 'a':
105 : case 'A':
106 0 : aCounter = new txAlphaCounter(ch);
107 0 : break;
108 :
109 : case '1':
110 : default:
111 : // if we don't recognize the token then use "1"
112 : aCounter = new txDecimalCounter(1, aGroupSize,
113 0 : aGroupSeparator);
114 0 : break;
115 : }
116 0 : return aCounter ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
117 : }
118 :
119 : // for now, the only multi-char token we support are decimals
120 : PRInt32 i;
121 0 : for (i = 0; i < length-1; ++i) {
122 0 : if (aToken.CharAt(i) != '0')
123 0 : break;
124 : }
125 0 : if (i == length-1 && aToken.CharAt(i) == '1') {
126 0 : aCounter = new txDecimalCounter(length, aGroupSize, aGroupSeparator);
127 : }
128 : else {
129 : // if we don't recognize the token then use '1'
130 0 : aCounter = new txDecimalCounter(1, aGroupSize, aGroupSeparator);
131 : }
132 :
133 0 : return aCounter ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
134 : }
135 :
136 :
137 0 : txDecimalCounter::txDecimalCounter(PRInt32 aMinLength, PRInt32 aGroupSize,
138 : const nsAString& aGroupSeparator)
139 : : mMinLength(aMinLength), mGroupSize(aGroupSize),
140 0 : mGroupSeparator(aGroupSeparator)
141 : {
142 0 : if (mGroupSize <= 0) {
143 0 : mGroupSize = aMinLength + 10;
144 : }
145 0 : }
146 :
147 0 : void txDecimalCounter::appendNumber(PRInt32 aNumber, nsAString& aDest)
148 : {
149 0 : const PRInt32 bufsize = 10; //must be able to fit an PRInt32
150 : PRUnichar buf[bufsize];
151 0 : PRInt32 pos = bufsize;
152 0 : while (aNumber > 0) {
153 0 : PRInt32 ch = aNumber % 10;
154 0 : aNumber /= 10;
155 0 : buf[--pos] = ch + '0';
156 : }
157 :
158 : // in case we didn't get a long enough string
159 0 : PRInt32 end = (bufsize > mMinLength) ? bufsize - mMinLength : 0;
160 0 : while (pos > end) {
161 0 : buf[--pos] = '0';
162 : }
163 :
164 : // in case we *still* didn't get a long enough string.
165 : // this should be very rare since it only happens if mMinLength is bigger
166 : // then the length of any PRInt32.
167 : // pos will always be zero
168 0 : PRInt32 extraPos = mMinLength;
169 0 : while (extraPos > bufsize) {
170 0 : aDest.Append(PRUnichar('0'));
171 0 : --extraPos;
172 0 : if (extraPos % mGroupSize == 0) {
173 0 : aDest.Append(mGroupSeparator);
174 : }
175 : }
176 :
177 : // copy string to buffer
178 0 : if (mGroupSize >= bufsize - pos) {
179 : // no grouping will occur
180 0 : aDest.Append(buf + pos, (PRUint32)(bufsize - pos));
181 : }
182 : else {
183 : // append chars up to first grouping separator
184 0 : PRInt32 len = ((bufsize - pos - 1) % mGroupSize) + 1;
185 0 : aDest.Append(buf + pos, len);
186 0 : pos += len;
187 0 : while (bufsize - pos > 0) {
188 0 : aDest.Append(mGroupSeparator);
189 0 : aDest.Append(buf + pos, mGroupSize);
190 0 : pos += mGroupSize;
191 : }
192 0 : NS_ASSERTION(bufsize == pos, "error while grouping");
193 : }
194 0 : }
195 :
196 :
197 0 : void txAlphaCounter::appendNumber(PRInt32 aNumber, nsAString& aDest)
198 : {
199 : PRUnichar buf[12];
200 0 : buf[11] = 0;
201 0 : PRInt32 pos = 11;
202 0 : while (aNumber > 0) {
203 0 : --aNumber;
204 0 : PRInt32 ch = aNumber % 26;
205 0 : aNumber /= 26;
206 0 : buf[--pos] = ch + mOffset;
207 : }
208 :
209 0 : aDest.Append(buf + pos, (PRUint32)(11 - pos));
210 0 : }
211 :
212 :
213 : const char* const kTxRomanNumbers[] =
214 : {"", "c", "cc", "ccc", "cd", "d", "dc", "dcc", "dccc", "cm",
215 : "", "x", "xx", "xxx", "xl", "l", "lx", "lxx", "lxxx", "xc",
216 : "", "i", "ii", "iii", "iv", "v", "vi", "vii", "viii", "ix",
217 : "", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM",
218 : "", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC",
219 : "", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"};
220 :
221 0 : void txRomanCounter::appendNumber(PRInt32 aNumber, nsAString& aDest)
222 : {
223 : // Numbers bigger then 3999 and negative numbers can't be done in roman
224 0 : if (PRUint32(aNumber) >= 4000) {
225 0 : txDecimalCounter().appendNumber(aNumber, aDest);
226 0 : return;
227 : }
228 :
229 0 : while (aNumber >= 1000) {
230 0 : aDest.Append(!mTableOffset ? PRUnichar('m') : PRUnichar('M'));
231 0 : aNumber -= 1000;
232 : }
233 :
234 : PRInt32 posValue;
235 :
236 : // Hundreds
237 0 : posValue = aNumber / 100;
238 0 : aNumber %= 100;
239 0 : AppendASCIItoUTF16(kTxRomanNumbers[posValue + mTableOffset], aDest);
240 : // Tens
241 0 : posValue = aNumber / 10;
242 0 : aNumber %= 10;
243 0 : AppendASCIItoUTF16(kTxRomanNumbers[10 + posValue + mTableOffset], aDest);
244 : // Ones
245 0 : AppendASCIItoUTF16(kTxRomanNumbers[20 + aNumber + mTableOffset], aDest);
246 : }
|