Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*************************************************************************
3 : *
4 : * The Contents of this file are made available subject to the terms of
5 : * either of the following licenses
6 : *
7 : * - GNU Lesser General Public License Version 2.1
8 : * - Sun Industry Standards Source License Version 1.1
9 : *
10 : * Sun Microsystems Inc., October, 2000
11 : *
12 : * GNU Lesser General Public License Version 2.1
13 : * =============================================
14 : * Copyright 2000 by Sun Microsystems, Inc.
15 : * 901 San Antonio Road, Palo Alto, CA 94303, USA
16 : *
17 : * This library is free software; you can redistribute it and/or
18 : * modify it under the terms of the GNU Lesser General Public
19 : * License version 2.1, as published by the Free Software Foundation.
20 : *
21 : * This library is distributed in the hope that it will be useful,
22 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
24 : * Lesser General Public License for more details.
25 : *
26 : * You should have received a copy of the GNU Lesser General Public
27 : * License along with this library; if not, write to the Free Software
28 : * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 : * MA 02111-1307 USA
30 : *
31 : *
32 : * Sun Industry Standards Source License Version 1.1
33 : * =================================================
34 : * The contents of this file are subject to the Sun Industry Standards
35 : * Source License Version 1.1 (the "License"); You may not use this file
36 : * except in compliance with the License. You may obtain a copy of the
37 : * License at http://www.openoffice.org/license.html.
38 : *
39 : * Software provided under this License is provided on an "AS IS" basis,
40 : * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
41 : * WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
42 : * MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
43 : * See the License for the specific provisions governing your rights and
44 : * obligations concerning the Software.
45 : *
46 : * The Initial Developer of the Original Code is: IBM Corporation
47 : *
48 : * Copyright: 2008 by IBM Corporation
49 : *
50 : * All Rights Reserved.
51 : *
52 : * Contributor(s): _______________________________________
53 : *
54 : *
55 : ************************************************************************/
56 : /*************************************************************************
57 : * @file
58 : * For LWP filter architecture prototype
59 : ************************************************************************/
60 : /*************************************************************************
61 : * Change History
62 : Jan 2005 Created
63 : ************************************************************************/
64 :
65 : #include "lwptools.hxx"
66 : #include <rtl/ustrbuf.hxx>
67 : #include <osl/process.h>
68 : #include <osl/thread.h>
69 : #include <osl/file.hxx>
70 : #include <vcl/svapp.hxx>
71 : #include <vcl/settings.hxx>
72 : #include <unicode/datefmt.h>
73 : #include <unicode/udat.h>
74 :
75 : #ifdef SAL_UNX
76 : #define SEPARATOR '/'
77 : #else
78 : #define SEPARATOR '\\'
79 : #endif
80 :
81 : using namespace icu;
82 : using namespace ::rtl;
83 : using namespace ::osl;
84 :
85 : /**
86 : * @descr read lwp unicode string from stream to OUString per aEncoding
87 : */
88 908 : sal_uInt16 LwpTools::QuickReadUnicode(LwpObjectStream* pObjStrm,
89 : OUString& str, sal_uInt16 strlen, rtl_TextEncoding aEncoding)
90 : //strlen: length of bytes
91 : {
92 :
93 908 : sal_uInt16 readLen = 0;
94 908 : OUStringBuffer strBuf(128);
95 :
96 908 : if( !IsUnicodePacked(pObjStrm, strlen) )
97 : {
98 891 : sal_uInt16 len = 0;
99 : sal_Char buf[1024];
100 :
101 2651 : while(strlen)
102 : {
103 869 : strlen>1023?len=1023 :len=strlen;
104 869 : len = pObjStrm->QuickRead(buf, len);
105 869 : buf[len] = '\0';
106 869 : strBuf.append( OUString(buf, len, aEncoding) );
107 869 : strlen -= len;
108 869 : readLen += len;
109 869 : if(!len) break;
110 : }
111 891 : str = strBuf.makeStringAndClear();
112 891 : return readLen;
113 : }
114 : else
115 : {
116 : sal_Char buf[1024];
117 : sal_Unicode unibuf[1024];
118 : sal_uInt8 readbyte;
119 : sal_uInt16 readword;
120 :
121 17 : sal_Bool flag = sal_False; //switch if unicode part reached
122 17 : sal_uInt16 sublen = 0;
123 :
124 491 : while(readLen<strlen)
125 : {
126 457 : if(!flag) //Not unicode string
127 : {
128 : bool bFailure;
129 392 : readbyte = pObjStrm->QuickReaduInt8(&bFailure);
130 392 : if(bFailure) break;
131 392 : readLen+=sizeof(readbyte);
132 :
133 392 : if(readbyte == 0x00)
134 : {
135 17 : flag = sal_True;
136 17 : if(sublen>0) //add it to the strBuf
137 : {
138 5 : strBuf.append( OUString(buf, sublen, aEncoding) ); //try the aEncoding
139 5 : sublen = 0;
140 : }
141 : }
142 : else
143 : {
144 375 : buf[sublen++] = readbyte;
145 : }
146 392 : if(sublen>=1023 || readLen==strlen) //add it to the strBuf
147 : {
148 6 : strBuf.append( OUString(buf, sublen, aEncoding) ); //try the aEncoding
149 6 : sublen = 0;
150 : }
151 : }
152 : else //unicode string
153 : {
154 : bool bFailure;
155 65 : readword = pObjStrm->QuickReaduInt16(&bFailure);
156 65 : if(bFailure) break;
157 65 : readLen+=sizeof(readword);
158 :
159 65 : if(readword == 0x0000)
160 : {
161 6 : flag = sal_False;
162 6 : if(sublen)
163 : {
164 6 : unibuf[sublen] = sal_Unicode('\0');
165 6 : strBuf.append( OUString(unibuf) );
166 6 : sublen = 0;
167 : }
168 : }
169 : else
170 : {
171 59 : unibuf[sublen++] = readword;
172 : }
173 65 : if(sublen>=1023 || readLen==strlen)
174 : {
175 11 : unibuf[sublen] = sal_Unicode('\0');
176 11 : strBuf.append( OUString(unibuf) );
177 11 : sublen = 0;
178 : }
179 : }
180 : }
181 17 : str = strBuf.makeStringAndClear();
182 17 : return readLen;
183 908 : }
184 : }
185 :
186 : /**
187 : * @descr Judge if the data (len) in object stream is lwp unicode packed
188 : */
189 908 : sal_Bool LwpTools::IsUnicodePacked(LwpObjectStream* pObjStrm, sal_uInt16 len)
190 : {
191 : sal_uInt8 byte;
192 908 : sal_uInt16 oldpos = pObjStrm->GetPos();
193 :
194 15298 : for (sal_uInt16 i = 0; i < len; i++)
195 : {
196 14407 : byte = pObjStrm->QuickReaduInt8();
197 14407 : if (byte == 0x00)
198 : {
199 17 : pObjStrm->Seek(oldpos);
200 17 : return sal_True;
201 : }
202 : }
203 891 : pObjStrm->Seek(oldpos);
204 891 : return sal_False;
205 : }
206 :
207 0 : sal_Bool LwpTools::isFileUrl(const OString &fileName)
208 : {
209 0 : if (fileName.indexOf("file://") == 0 )
210 0 : return sal_True;
211 0 : return sal_False;
212 : }
213 :
214 0 : OUString LwpTools::convertToFileUrl(const OString &fileName)
215 : {
216 0 : if ( isFileUrl(fileName) )
217 : {
218 0 : return OStringToOUString(fileName, osl_getThreadTextEncoding());
219 : }
220 :
221 0 : OUString uUrlFileName;
222 0 : OUString uFileName(fileName.getStr(), fileName.getLength(), osl_getThreadTextEncoding());
223 0 : if ( fileName.indexOf('.') == 0 || fileName.indexOf(SEPARATOR) < 0 )
224 : {
225 0 : OUString uWorkingDir;
226 0 : OSL_VERIFY( osl_getProcessWorkingDir(&uWorkingDir.pData) == osl_Process_E_None );
227 0 : OSL_VERIFY( FileBase::getAbsoluteFileURL(uWorkingDir, uFileName, uUrlFileName) == FileBase::E_None );
228 : } else
229 : {
230 0 : OSL_VERIFY( FileBase::getFileURLFromSystemPath(uFileName, uUrlFileName) == FileBase::E_None );
231 : }
232 :
233 0 : return uUrlFileName;
234 : }
235 :
236 0 : OUString LwpTools::DateTimeToOUString(LtTm & dt)
237 : {
238 0 : rtl::OUStringBuffer buf;
239 0 : buf.append(dt.tm_year);
240 0 : buf.append( A2OUSTR("-") );
241 0 : buf.append(dt.tm_mon);
242 0 : buf.append( A2OUSTR("-") );
243 0 : buf.append(dt.tm_mday);
244 0 : buf.append( A2OUSTR("T") );
245 0 : buf.append(dt.tm_hour);
246 0 : buf.append( A2OUSTR(":") );
247 0 : buf.append(dt.tm_min);
248 0 : buf.append( A2OUSTR(":") );
249 0 : buf.append(dt.tm_sec);
250 :
251 0 : return buf.makeStringAndClear();
252 : }
253 :
254 : /**
255 : * @descr get the system date format
256 : */
257 0 : XFDateStyle* LwpTools::GetSystemDateStyle(sal_Bool bLongFormat)
258 : {
259 : icu::DateFormat::EStyle style;
260 0 : if (bLongFormat)
261 0 : style = icu::DateFormat::FULL;//system full date format
262 : else
263 0 : style = icu::DateFormat::SHORT;//system short date format
264 : /* ::com::sun::star::lang::Locale aLocale=Application::GetSettings().GetLocale();
265 : rtl::OUString strLang = aLocale.Language;
266 : rtl::OUString strCountry = aLocale.Country;
267 : strLang = strLang + A2OUSTR("_");
268 : rtl::OUString strLocale = strLang + strCountry;
269 :
270 : int32_t nLength = 0;
271 : int32_t nLengthNeed;
272 : UErrorCode status = U_ZERO_ERROR;
273 : UChar* pattern = NULL;
274 :
275 : UDateFormat* fmt= udat_open(UDAT_FULL, UDAT_FULL,
276 : (char*)(OUStringToOString(strLocale,RTL_TEXTENCODING_MS_1252).getStr()), NULL, 0, NULL, 0, &status);
277 :
278 : nLengthNeed = udat_toPattern(fmt,true,NULL,nLength,&status);
279 : if (status == U_BUFFER_OVERFLOW_ERROR)
280 : {
281 : status = U_ZERO_ERROR;
282 : nLength = nLengthNeed +1;
283 : pattern = (UChar*)malloc(sizeof(UChar)*nLength);
284 : udat_toPattern(fmt,true,pattern,nLength,&status);
285 : }
286 : */
287 : /* FIXME-BCP47: handle language tags! */
288 : //1 get locale for system
289 0 : ::com::sun::star::lang::Locale aLocale=Application::GetSettings().GetLanguageTag().getLocale();
290 0 : rtl::OUString strLang = aLocale.Language;
291 0 : rtl::OUString strCountry = aLocale.Country;
292 0 : icu::Locale bLocale((char*)(OUStringToOString(strLang,RTL_TEXTENCODING_MS_1252).getStr()),
293 0 : (char*)(OUStringToOString(strCountry,RTL_TEXTENCODING_MS_1252).getStr()));
294 : //2 get icu format pattern by locale
295 0 : icu::DateFormat* fmt = icu::DateFormat::createDateInstance(style,bLocale);
296 :
297 0 : int32_t nLength = 0;
298 : int32_t nLengthNeed;
299 0 : UErrorCode status = U_ZERO_ERROR;
300 0 : UChar* pattern = NULL;
301 :
302 0 : nLengthNeed = udat_toPattern((void *const *)fmt,sal_False,NULL,nLength,&status);
303 0 : if (status == U_BUFFER_OVERFLOW_ERROR)
304 : {
305 0 : status = U_ZERO_ERROR;
306 0 : nLength = nLengthNeed +1;
307 0 : pattern = (UChar*)malloc(sizeof(UChar)*nLength);
308 0 : udat_toPattern((void *const *)fmt,sal_False,pattern,nLength,&status);
309 : }
310 0 : if (pattern == NULL)
311 0 : return NULL;
312 : // 3 parse pattern string,per icu date/time format syntax, there are 20 letters reserved
313 : // as patter letter,each represent a element in date/time and its repeat numbers represent
314 : // different format: for exampel: M produces '1',MM produces '01', MMM produces 'Jan', MMMM produces 'Januaray'
315 : // letter other than these letters is regard as text in the format, for example ','in 'Jan,2005'
316 : // we parse pattern string letter by letter and get the time format.
317 : UChar cSymbol;
318 : UChar cTmp;
319 0 : XFDateStyle* pDateStyle = new XFDateStyle;
320 :
321 0 : for (int32_t i=0;i<nLengthNeed;)
322 : {
323 0 : cSymbol = pattern[i];
324 : int32_t j;
325 0 : switch(cSymbol)
326 : {
327 : case 'G':
328 : {
329 0 : for (j=1;;j++)
330 : {
331 0 : cTmp = pattern[i+j];
332 0 : if (cTmp != cSymbol)
333 : {
334 0 : i=i+j;
335 0 : break;
336 : }
337 : }
338 0 : pDateStyle->AddEra();
339 0 : break;
340 : }
341 : case 'y':
342 : {
343 0 : for (j=1;;j++)
344 : {
345 0 : cTmp = pattern[i+j];
346 0 : if (cTmp != cSymbol)
347 : {
348 0 : i=i+j;
349 0 : break;
350 : }
351 : }
352 0 : if (j <= 2)
353 0 : pDateStyle->AddYear(sal_False);
354 : else
355 0 : pDateStyle->AddYear();
356 0 : break;
357 : }
358 : case 'M':
359 : {
360 0 : for (j=1;;j++)
361 : {
362 0 : cTmp = pattern[i+j];
363 0 : if (cTmp != cSymbol)
364 : {
365 0 : i=i+j;
366 0 : break;
367 : }
368 : }
369 0 : if (j==1)
370 0 : pDateStyle->AddMonth(sal_False,sal_False);
371 0 : else if (j==2)
372 0 : pDateStyle->AddMonth(sal_True,sal_False);
373 0 : else if (j==3)
374 0 : pDateStyle->AddMonth(sal_False,sal_True);
375 : else
376 0 : pDateStyle->AddMonth(sal_True,sal_True);
377 0 : break;
378 : }
379 : case 'd':
380 : {
381 0 : for (j=1;;j++)
382 : {
383 0 : cTmp = pattern[i+j];
384 0 : if (cTmp != cSymbol)
385 : {
386 0 : i=i+j;
387 0 : break;
388 : }
389 : }
390 0 : if (j==1)
391 0 : pDateStyle->AddMonthDay(sal_False);
392 : else
393 0 : pDateStyle->AddMonthDay();
394 0 : break;
395 : }
396 : case 'h':
397 : {
398 0 : for (j=1;;j++)
399 : {
400 0 : cTmp = pattern[i+j];
401 0 : if (cTmp != cSymbol)
402 : {
403 0 : i=i+j;
404 0 : break;
405 : }
406 : }
407 0 : if (j==1)
408 0 : pDateStyle->AddHour(sal_False);
409 : else
410 0 : pDateStyle->AddHour();
411 0 : break;
412 : }
413 : case 'H':
414 : {
415 0 : for (j=1;;j++)
416 : {
417 0 : cTmp = pattern[i+j];
418 0 : if (cTmp != cSymbol)
419 : {
420 0 : i=i+j;
421 0 : break;
422 : }
423 : }
424 0 : if (j==1)
425 0 : pDateStyle->AddHour(sal_False);
426 : else
427 0 : pDateStyle->AddHour();
428 0 : break;
429 : }
430 : case 'm':
431 : {
432 0 : for (j=1;;j++)
433 : {
434 0 : cTmp = pattern[i+j];
435 0 : if (cTmp != cSymbol)
436 : {
437 0 : i=i+j;
438 0 : break;
439 : }
440 : }
441 0 : if (j==1)
442 0 : pDateStyle->AddMinute(sal_False);
443 : else
444 0 : pDateStyle->AddMinute();
445 0 : break;
446 : }
447 : case 's':
448 : {
449 0 : for (j=1;;j++)
450 : {
451 0 : cTmp = pattern[i+j];
452 0 : if (cTmp != cSymbol)
453 : {
454 0 : i=i+j;
455 0 : break;
456 : }
457 : }
458 0 : if (j==1)
459 0 : pDateStyle->AddSecond(sal_False,0);
460 : else
461 0 : pDateStyle->AddSecond(sal_True,0);
462 0 : break;
463 : }
464 : case 'S':
465 : {
466 0 : for (j=1;;j++)
467 : {
468 0 : cTmp = pattern[i+j];
469 0 : if (cTmp != cSymbol)
470 : {
471 0 : i=i+j;
472 0 : break;
473 : }
474 : }
475 : /*if (j==1)
476 : pDateStyle->AddSecond(sal_False);
477 : else
478 : pDateStyle->AddSecond();*/
479 0 : break;
480 : }
481 : case 'E':
482 : {
483 0 : for (j=1;;j++)
484 : {
485 0 : cTmp = pattern[i+j];
486 0 : if (cTmp != cSymbol)
487 : {
488 0 : i=i+j;
489 0 : break;
490 : }
491 : }
492 0 : if (j<=2)
493 0 : pDateStyle->AddWeekDay(sal_False);
494 : else
495 0 : pDateStyle->AddWeekDay();
496 0 : break;
497 : }
498 : case 'D':
499 : {
500 0 : for (j=1;;j++)
501 : {
502 0 : cTmp = pattern[i+j];
503 0 : if (cTmp != cSymbol)
504 : {
505 0 : i=i+j;
506 0 : break;
507 : }
508 : }
509 : /*if (j==1)
510 : pDateStyle->AddWeekDay(sal_False);
511 : else
512 : pDateStyle->AddWeekDay();*/
513 0 : break;
514 : }
515 : case 'F':
516 : {
517 0 : for (j=1;;j++)
518 : {
519 0 : cTmp = pattern[i+j];
520 0 : if (cTmp != cSymbol)
521 : {
522 0 : i=i+j;
523 0 : break;
524 : }
525 : }
526 : /*if (j==1)
527 : pDateStyle->AddWeekDay(sal_False);
528 : else
529 : pDateStyle->AddWeekDay();*/
530 0 : break;
531 : }
532 : case 'w':
533 : {
534 0 : for (j=1;;j++)
535 : {
536 0 : cTmp = pattern[i+j];
537 0 : if (cTmp != cSymbol)
538 : {
539 0 : i=i+j;
540 0 : break;
541 : }
542 : }
543 : /*if (j==1)
544 : pDateStyle->AddWeekDay(sal_False);
545 : else
546 : pDateStyle->AddWeekDay();*/
547 0 : break;
548 : }
549 : case 'W':
550 : {
551 0 : for (j=1;;j++)
552 : {
553 0 : cTmp = pattern[i+j];
554 0 : if (cTmp != cSymbol)
555 : {
556 0 : i=i+j;
557 0 : break;
558 : }
559 : }
560 : /*if (j==1)
561 : pDateStyle->AddWeekDay(sal_False);
562 : else
563 : pDateStyle->AddWeekDay();*/
564 0 : break;
565 : }
566 : case 'a':
567 : {
568 0 : for (j=1;;j++)
569 : {
570 0 : cTmp = pattern[i+j];
571 0 : if (cTmp != cSymbol)
572 : {
573 0 : i=i+j;
574 0 : break;
575 : }
576 : }
577 0 : pDateStyle->AddAmPm(sal_True);
578 0 : break;
579 : }
580 : case 'k':
581 : {
582 0 : for (j=1;;j++)
583 : {
584 0 : cTmp = pattern[i+j];
585 0 : if (cTmp != cSymbol)
586 : {
587 0 : i=i+j;
588 0 : break;
589 : }
590 : }
591 0 : break;
592 : }
593 : case 'K':
594 : {
595 0 : for (j=1;;j++)
596 : {
597 0 : cTmp = pattern[i+j];
598 0 : if (cTmp != cSymbol)
599 : {
600 0 : i=i+j;
601 0 : break;
602 : }
603 : }
604 0 : if (j==1)
605 0 : pDateStyle->AddHour(sal_False);
606 : else
607 0 : pDateStyle->AddHour();
608 0 : break;
609 : }
610 : case 'Z':
611 : {
612 0 : for (j=1;;j++)
613 : {
614 0 : cTmp = pattern[i+j];
615 0 : if (cTmp != cSymbol)
616 : {
617 0 : i=i+j;
618 0 : break;
619 : }
620 : }
621 0 : break;
622 : }
623 : case '\''://'
624 : {
625 0 : for (j=1;;j++)
626 : {
627 0 : cTmp = pattern[i+j];
628 0 : if (cTmp != cSymbol)
629 : {
630 0 : i=i+j;
631 0 : break;
632 : }
633 : }
634 0 : break;
635 : }
636 : case '"':
637 : {
638 0 : pDateStyle->AddText(OUString(A2OUSTR("'")));
639 0 : break;
640 : }
641 : default:
642 : {
643 0 : if ((cSymbol>='A' && cSymbol<='Z') || (cSymbol>='a' && cSymbol<='z') )
644 0 : return NULL;
645 : else//TEXT
646 : {
647 : //UChar buffer[1024];
648 : sal_Unicode buffer[1024];
649 0 : buffer[0] = cSymbol;
650 0 : for (j=1;;j++)
651 : {
652 0 : cTmp = pattern[i+j];
653 0 : if ((cTmp>='A' && cTmp<='Z') || (cTmp>='a' && cTmp<='z') ||
654 : cTmp=='\'' || cTmp=='"' )
655 : {
656 0 : i=i+j;
657 0 : buffer[j]= '\0';
658 0 : break;
659 : }
660 : else
661 0 : buffer[j] = cTmp;
662 : }
663 :
664 0 : pDateStyle->AddText(OUString(buffer));//keep for all parsed
665 : }
666 0 : break;
667 : }
668 : }
669 : }
670 : // udat_close(fmt);
671 0 : return pDateStyle;
672 : }
673 : /**
674 : * @descr get the system time format
675 : */
676 0 : XFTimeStyle* LwpTools::GetSystemTimeStyle()
677 : {
678 : /* ::com::sun::star::lang::Locale aLocale=Application::GetSettings().GetLocale();
679 : rtl::OUString strLang = aLocale.Language;
680 : rtl::OUString strCountry = aLocale.Country;
681 : strLang = strLang + A2OUSTR("_");
682 : rtl::OUString strLocale = strLang + strCountry;
683 :
684 : int32_t nLength = 0;
685 : int32_t nLengthNeed;
686 : UErrorCode status = U_ZERO_ERROR;
687 : UChar* pattern = NULL;
688 :
689 : UDateFormat* fmt= udat_open(UDAT_FULL, UDAT_FULL,
690 : (char*)(OUStringToOString(strLocale,RTL_TEXTENCODING_MS_1252).getStr()), NULL, 0, NULL, 0, &status);
691 :
692 : nLengthNeed = udat_toPattern(fmt,true,NULL,nLength,&status);
693 : if (status == U_BUFFER_OVERFLOW_ERROR)
694 : {
695 : status = U_ZERO_ERROR;
696 : nLength = nLengthNeed +1;
697 : pattern = (UChar*)malloc(sizeof(UChar)*nLength);
698 : udat_toPattern(fmt,true,pattern,nLength,&status);
699 : }
700 : */
701 : /* FIXME-BCP47: handle language tags! */
702 : //1 get locale for system
703 0 : ::com::sun::star::lang::Locale aLocale=Application::GetSettings().GetLanguageTag().getLocale();
704 0 : rtl::OUString strLang = aLocale.Language;
705 0 : rtl::OUString strCountry = aLocale.Country;
706 0 : icu::Locale bLocale((char*)(OUStringToOString(strLang,RTL_TEXTENCODING_MS_1252).getStr()),
707 0 : (char*)(OUStringToOString(strCountry,RTL_TEXTENCODING_MS_1252).getStr()));
708 :
709 0 : icu::DateFormat* fmt = icu::DateFormat::createTimeInstance(icu::DateFormat::DEFAULT,bLocale);
710 : //2 get icu format pattern by locale
711 0 : int32_t nLength = 0;
712 : int32_t nLengthNeed;
713 0 : UErrorCode status = U_ZERO_ERROR;
714 0 : UChar* pattern = NULL;
715 0 : nLengthNeed = udat_toPattern((void *const *)fmt,false,NULL,nLength,&status);
716 0 : if (status == U_BUFFER_OVERFLOW_ERROR)
717 : {
718 0 : status = U_ZERO_ERROR;
719 0 : nLength = nLengthNeed +1;
720 0 : pattern = (UChar*)malloc(sizeof(UChar)*nLength);
721 0 : udat_toPattern((void *const *)fmt,false,pattern,nLength,&status);
722 : }
723 :
724 0 : if (pattern == NULL)
725 0 : return NULL;
726 : // 3 parse pattern string,per icu date/time format syntax, there are 20 letters reserved
727 : // as patter letter,each represent a element in date/time and its repeat numbers represent
728 : // different format: for exampel: M produces '1',MM produces '01', MMM produces 'Jan', MMMM produces 'Januaray'
729 : // letter other than these letters is regard as text in the format, for example ','in 'Jan,2005'
730 : // we parse pattern string letter by letter and get the time format.
731 : // for time format ,for there is not date info,we can only parse the letter representing time.
732 : UChar cSymbol;
733 : UChar cTmp;
734 0 : XFTimeStyle* pTimeStyle = new XFTimeStyle;
735 :
736 0 : for (int32_t i=0;i<nLengthNeed;)
737 : {
738 0 : cSymbol = pattern[i];
739 : int32_t j;
740 0 : switch(cSymbol)
741 : {
742 : case 'h':
743 : {
744 0 : for (j=1;;j++)
745 : {
746 0 : cTmp = pattern[i+j];
747 0 : if (cTmp != cSymbol)
748 : {
749 0 : i=i+j;
750 0 : break;
751 : }
752 : }
753 0 : if (j==1)
754 0 : pTimeStyle->AddHour(sal_False);
755 : else
756 0 : pTimeStyle->AddHour();
757 0 : break;
758 : }
759 : case 'H':
760 : {
761 0 : for (j=1;;j++)
762 : {
763 0 : cTmp = pattern[i+j];
764 0 : if (cTmp != cSymbol)
765 : {
766 0 : i=i+j;
767 0 : break;
768 : }
769 : }
770 0 : if (j==1)
771 0 : pTimeStyle->AddHour(sal_False);
772 : else
773 0 : pTimeStyle->AddHour();
774 0 : break;
775 : }
776 : case 'm':
777 : {
778 0 : for (j=1;;j++)
779 : {
780 0 : cTmp = pattern[i+j];
781 0 : if (cTmp != cSymbol)
782 : {
783 0 : i=i+j;
784 0 : break;
785 : }
786 : }
787 0 : if (j==1)
788 0 : pTimeStyle->AddMinute(sal_False);
789 : else
790 0 : pTimeStyle->AddMinute();
791 0 : break;
792 : }
793 : case 's':
794 : {
795 0 : for (j=1;;j++)
796 : {
797 0 : cTmp = pattern[i+j];
798 0 : if (cTmp != cSymbol)
799 : {
800 0 : i=i+j;
801 0 : break;
802 : }
803 : }
804 0 : if (j==1)
805 0 : pTimeStyle->AddSecond(sal_False,0);
806 : else
807 0 : pTimeStyle->AddSecond(sal_True,0);
808 0 : break;
809 : }
810 : case 'S':
811 : {
812 0 : for (j=1;;j++)
813 : {
814 0 : cTmp = pattern[i+j];
815 0 : if (cTmp != cSymbol)
816 : {
817 0 : i=i+j;
818 0 : break;
819 : }
820 : }
821 : /*if (j==1)
822 : pDateStyle->AddSecond(sal_False);
823 : else
824 : pDateStyle->AddSecond();*/
825 0 : break;
826 : }
827 : case 'a':
828 : {
829 0 : for (j=1;;j++)
830 : {
831 0 : cTmp = pattern[i+j];
832 0 : if (cTmp != cSymbol)
833 : {
834 0 : i=i+j;
835 0 : break;
836 : }
837 : }
838 0 : pTimeStyle->SetAmPm(sal_True);
839 0 : break;
840 : }
841 : case 'k':
842 : {
843 0 : for (j=1;;j++)
844 : {
845 0 : cTmp = pattern[i+j];
846 0 : if (cTmp != cSymbol)
847 : {
848 0 : i=i+j;
849 0 : break;
850 : }
851 : }
852 0 : break;
853 : }
854 : case 'K':
855 : {
856 0 : for (j=1;;j++)
857 : {
858 0 : cTmp = pattern[i+j];
859 0 : if (cTmp != cSymbol)
860 : {
861 0 : i=i+j;
862 0 : break;
863 : }
864 : }
865 0 : if (j==1)
866 0 : pTimeStyle->AddHour(sal_False);
867 : else
868 0 : pTimeStyle->AddHour();
869 0 : break;
870 : }
871 : case '\''://'
872 : {
873 0 : for (j=1;;j++)
874 : {
875 0 : cTmp = pattern[i+j];
876 0 : if (cTmp != cSymbol)
877 : {
878 0 : i=i+j;
879 0 : break;
880 : }
881 : }
882 0 : break;
883 : }
884 : case '"':
885 : {
886 0 : pTimeStyle->AddText(OUString(A2OUSTR("'")));
887 0 : break;
888 : }
889 : default:
890 : {
891 0 : if ((cSymbol>='A' && cSymbol<='Z') || (cSymbol>='a' && cSymbol<='z') )
892 0 : return NULL;
893 : else//TEXT
894 : {
895 : sal_Unicode buffer[1024];
896 0 : buffer[0] = cSymbol;
897 : //strBuffer.append(cSymbol);
898 0 : for (j=1;;j++)
899 : {
900 0 : cTmp = pattern[i+j];
901 0 : if ((cTmp>='A' && cTmp<='Z') || (cTmp>='a' && cTmp<='z') ||
902 : cTmp=='\'' || cTmp=='"' )
903 : {
904 0 : i=i+j;
905 0 : buffer[j]= '\0';
906 0 : break;
907 : }
908 : else
909 0 : buffer[j] = cTmp;
910 : }
911 0 : pTimeStyle->AddText(OUString(buffer));//keep for all parsed
912 : }
913 0 : break;
914 : }
915 : }
916 : }
917 : // udat_close(fmt);
918 0 : return pTimeStyle;
919 : }
920 :
921 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|