fix baseline build (old cairo) - 'cairo_rectangle_int_t' does not name a type
[LibreOffice.git] / lotuswordpro / source / filter / lwptools.cxx
blob3b1516dafa41fabb4460ea68a78024229d4dc564
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * either of the following licenses
7 * - GNU Lesser General Public License Version 2.1
8 * - Sun Industry Standards Source License Version 1.1
10 * Sun Microsystems Inc., October, 2000
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
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.
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.
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
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.
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.
46 * The Initial Developer of the Original Code is: IBM Corporation
48 * Copyright: 2008 by IBM Corporation
50 * All Rights Reserved.
52 * Contributor(s): _______________________________________
55 ************************************************************************/
56 /*************************************************************************
57 * @file
58 * For LWP filter architecture prototype
59 ************************************************************************/
60 /*************************************************************************
61 * Change History
62 Jan 2005 Created
63 ************************************************************************/
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 #include <i18nlangtag/languagetagicu.hxx>
76 #ifdef SAL_UNX
77 #define SEPARATOR '/'
78 #else
79 #define SEPARATOR '\\'
80 #endif
82 using namespace ::osl;
84 /**
85 * @descr read lwp unicode string from stream to OUString per aEncoding
87 sal_uInt16 LwpTools::QuickReadUnicode(LwpObjectStream* pObjStrm,
88 OUString& str, sal_uInt16 strlen, rtl_TextEncoding aEncoding)
89 //strlen: length of bytes
92 sal_uInt16 readLen = 0;
93 OUStringBuffer strBuf(128);
95 if( !IsUnicodePacked(pObjStrm, strlen) )
97 sal_uInt16 len = 0;
98 sal_Char buf[1024];
100 while(strlen)
102 strlen>1023?len=1023 :len=strlen;
103 len = pObjStrm->QuickRead(buf, len);
104 buf[len] = '\0';
105 strBuf.append( OUString(buf, len, aEncoding) );
106 strlen -= len;
107 readLen += len;
108 if(!len) break;
110 str = strBuf.makeStringAndClear();
111 return readLen;
113 else
115 sal_Char buf[1024];
116 sal_Unicode unibuf[1024];
117 sal_uInt8 readbyte;
118 sal_uInt16 readword;
120 bool flag = false; //switch if unicode part reached
121 sal_uInt16 sublen = 0;
123 while(readLen<strlen)
125 if(!flag) //Not unicode string
127 bool bFailure;
128 readbyte = pObjStrm->QuickReaduInt8(&bFailure);
129 if(bFailure) break;
130 readLen+=sizeof(readbyte);
132 if(readbyte == 0x00)
134 flag = true;
135 if(sublen>0) //add it to the strBuf
137 strBuf.append( OUString(buf, sublen, aEncoding) ); //try the aEncoding
138 sublen = 0;
141 else
143 buf[sublen++] = readbyte;
145 if(sublen>=1023 || readLen==strlen) //add it to the strBuf
147 strBuf.append( OUString(buf, sublen, aEncoding) ); //try the aEncoding
148 sublen = 0;
151 else //unicode string
153 bool bFailure;
154 readword = pObjStrm->QuickReaduInt16(&bFailure);
155 if(bFailure) break;
156 readLen+=sizeof(readword);
158 if(readword == 0x0000)
160 flag = false;
161 if(sublen)
163 unibuf[sublen] = '\0';
164 strBuf.append( OUString(unibuf) );
165 sublen = 0;
168 else
170 unibuf[sublen++] = readword;
172 if(sublen>=1023 || readLen==strlen)
174 unibuf[sublen] = '\0';
175 strBuf.append( OUString(unibuf) );
176 sublen = 0;
180 str = strBuf.makeStringAndClear();
181 return readLen;
186 * @descr Judge if the data (len) in object stream is lwp unicode packed
188 bool LwpTools::IsUnicodePacked(LwpObjectStream* pObjStrm, sal_uInt16 len)
190 sal_uInt8 byte;
191 sal_uInt16 oldpos = pObjStrm->GetPos();
193 for (sal_uInt16 i = 0; i < len; i++)
195 byte = pObjStrm->QuickReaduInt8();
196 if (byte == 0x00)
198 pObjStrm->Seek(oldpos);
199 return true;
202 pObjStrm->Seek(oldpos);
203 return false;
206 bool LwpTools::isFileUrl(const OString &fileName)
208 if (fileName.startsWith("file://") )
209 return true;
210 return false;
213 OUString LwpTools::convertToFileUrl(const OString &fileName)
215 if ( isFileUrl(fileName) )
217 return OStringToOUString(fileName, osl_getThreadTextEncoding());
220 OUString uUrlFileName;
221 OUString uFileName(fileName.getStr(), fileName.getLength(), osl_getThreadTextEncoding());
222 if ( fileName.startsWith(".") || fileName.indexOf(SEPARATOR) < 0 )
224 OUString uWorkingDir;
225 OSL_VERIFY( osl_getProcessWorkingDir(&uWorkingDir.pData) == osl_Process_E_None );
226 OSL_VERIFY( FileBase::getAbsoluteFileURL(uWorkingDir, uFileName, uUrlFileName) == FileBase::E_None );
227 } else
229 OSL_VERIFY( FileBase::getFileURLFromSystemPath(uFileName, uUrlFileName) == FileBase::E_None );
232 return uUrlFileName;
235 OUString LwpTools::DateTimeToOUString(LtTm & dt)
237 OUString aResult = OUString::number(dt.tm_year) + "-" + OUString::number(dt.tm_mon) + "-" + OUString::number(dt.tm_mday) +
238 "T" + OUString::number(dt.tm_hour) + ":" + OUString::number(dt.tm_min) + ":" + OUString::number(dt.tm_sec);
240 return aResult;
244 * @descr get the system date format
246 XFDateStyle* LwpTools::GetSystemDateStyle(bool bLongFormat)
248 icu::DateFormat::EStyle style;
249 if (bLongFormat)
250 style = icu::DateFormat::FULL;//system full date format
251 else
252 style = icu::DateFormat::SHORT;//system short date format
254 //1 get locale for system
255 icu::Locale aLocale( LanguageTagIcu::getIcuLocale( Application::GetSettings().GetLanguageTag()));
256 //2 get icu format pattern by locale
257 icu::DateFormat* fmt = icu::DateFormat::createDateInstance(style,aLocale);
259 int32_t nLength = 0;
260 int32_t nLengthNeed;
261 UErrorCode status = U_ZERO_ERROR;
262 UChar* pattern = NULL;
264 nLengthNeed = udat_toPattern(reinterpret_cast<void **>(fmt),sal_False,NULL,nLength,&status);
265 if (status == U_BUFFER_OVERFLOW_ERROR)
267 status = U_ZERO_ERROR;
268 nLength = nLengthNeed +1;
269 pattern = static_cast<UChar*>(malloc(sizeof(UChar)*nLength));
270 udat_toPattern(reinterpret_cast<void **>(fmt),sal_False,pattern,nLength,&status);
272 if (pattern == NULL)
273 return NULL;
274 // 3 parse pattern string,per icu date/time format syntax, there are 20 letters reserved
275 // as patter letter,each represent a element in date/time and its repeat numbers represent
276 // different format: for exampel: M produces '1',MM produces '01', MMM produces 'Jan', MMMM produces 'Januaray'
277 // letter other than these letters is regard as text in the format, for example ','in 'Jan,2005'
278 // we parse pattern string letter by letter and get the time format.
279 UChar cSymbol;
280 UChar cTmp;
281 XFDateStyle* pDateStyle = new XFDateStyle;
283 for (int32_t i=0;i<nLengthNeed;)
285 cSymbol = pattern[i];
286 int32_t j;
287 switch(cSymbol)
289 case 'G':
291 for (j=1;;j++)
293 cTmp = pattern[i+j];
294 if (cTmp != cSymbol)
296 i=i+j;
297 break;
300 pDateStyle->AddEra();
301 break;
303 case 'y':
305 for (j=1;;j++)
307 cTmp = pattern[i+j];
308 if (cTmp != cSymbol)
310 i=i+j;
311 break;
314 if (j <= 2)
315 pDateStyle->AddYear(false);
316 else
317 pDateStyle->AddYear();
318 break;
320 case 'M':
322 for (j=1;;j++)
324 cTmp = pattern[i+j];
325 if (cTmp != cSymbol)
327 i=i+j;
328 break;
331 if (j==1)
332 pDateStyle->AddMonth(false,false);
333 else if (j==2)
334 pDateStyle->AddMonth(true,false);
335 else if (j==3)
336 pDateStyle->AddMonth(false,true);
337 else
338 pDateStyle->AddMonth(true,true);
339 break;
341 case 'd':
343 for (j=1;;j++)
345 cTmp = pattern[i+j];
346 if (cTmp != cSymbol)
348 i=i+j;
349 break;
352 if (j==1)
353 pDateStyle->AddMonthDay(false);
354 else
355 pDateStyle->AddMonthDay();
356 break;
358 case 'h':
360 for (j=1;;j++)
362 cTmp = pattern[i+j];
363 if (cTmp != cSymbol)
365 i=i+j;
366 break;
369 if (j==1)
370 pDateStyle->AddHour(false);
371 else
372 pDateStyle->AddHour();
373 break;
375 case 'H':
377 for (j=1;;j++)
379 cTmp = pattern[i+j];
380 if (cTmp != cSymbol)
382 i=i+j;
383 break;
386 if (j==1)
387 pDateStyle->AddHour(false);
388 else
389 pDateStyle->AddHour();
390 break;
392 case 'm':
394 for (j=1;;j++)
396 cTmp = pattern[i+j];
397 if (cTmp != cSymbol)
399 i=i+j;
400 break;
403 if (j==1)
404 pDateStyle->AddMinute(false);
405 else
406 pDateStyle->AddMinute();
407 break;
409 case 's':
411 for (j=1;;j++)
413 cTmp = pattern[i+j];
414 if (cTmp != cSymbol)
416 i=i+j;
417 break;
420 if (j==1)
421 pDateStyle->AddSecond(false,0);
422 else
423 pDateStyle->AddSecond(true,0);
424 break;
426 case 'S':
428 for (j=1;;j++)
430 cTmp = pattern[i+j];
431 if (cTmp != cSymbol)
433 i=i+j;
434 break;
437 /*if (j==1)
438 pDateStyle->AddSecond(sal_False);
439 else
440 pDateStyle->AddSecond();*/
441 break;
443 case 'E':
445 for (j=1;;j++)
447 cTmp = pattern[i+j];
448 if (cTmp != cSymbol)
450 i=i+j;
451 break;
454 if (j<=2)
455 pDateStyle->AddWeekDay(false);
456 else
457 pDateStyle->AddWeekDay();
458 break;
460 case 'D':
462 for (j=1;;j++)
464 cTmp = pattern[i+j];
465 if (cTmp != cSymbol)
467 i=i+j;
468 break;
471 /*if (j==1)
472 pDateStyle->AddWeekDay(sal_False);
473 else
474 pDateStyle->AddWeekDay();*/
475 break;
477 case 'F':
479 for (j=1;;j++)
481 cTmp = pattern[i+j];
482 if (cTmp != cSymbol)
484 i=i+j;
485 break;
488 /*if (j==1)
489 pDateStyle->AddWeekDay(sal_False);
490 else
491 pDateStyle->AddWeekDay();*/
492 break;
494 case 'w':
496 for (j=1;;j++)
498 cTmp = pattern[i+j];
499 if (cTmp != cSymbol)
501 i=i+j;
502 break;
505 /*if (j==1)
506 pDateStyle->AddWeekDay(sal_False);
507 else
508 pDateStyle->AddWeekDay();*/
509 break;
511 case 'W':
513 for (j=1;;j++)
515 cTmp = pattern[i+j];
516 if (cTmp != cSymbol)
518 i=i+j;
519 break;
522 /*if (j==1)
523 pDateStyle->AddWeekDay(sal_False);
524 else
525 pDateStyle->AddWeekDay();*/
526 break;
528 case 'a':
530 for (j=1;;j++)
532 cTmp = pattern[i+j];
533 if (cTmp != cSymbol)
535 i=i+j;
536 break;
539 pDateStyle->AddAmPm(true);
540 break;
542 case 'k':
544 for (j=1;;j++)
546 cTmp = pattern[i+j];
547 if (cTmp != cSymbol)
549 i=i+j;
550 break;
553 break;
555 case 'K':
557 for (j=1;;j++)
559 cTmp = pattern[i+j];
560 if (cTmp != cSymbol)
562 i=i+j;
563 break;
566 if (j==1)
567 pDateStyle->AddHour(false);
568 else
569 pDateStyle->AddHour();
570 break;
572 case 'Z':
574 for (j=1;;j++)
576 cTmp = pattern[i+j];
577 if (cTmp != cSymbol)
579 i=i+j;
580 break;
583 break;
585 case '\''://'
587 for (j=1;;j++)
589 cTmp = pattern[i+j];
590 if (cTmp != cSymbol)
592 i=i+j;
593 break;
596 break;
598 case '"':
600 pDateStyle->AddText("'");
601 break;
603 default:
605 if ((cSymbol>='A' && cSymbol<='Z') || (cSymbol>='a' && cSymbol<='z') )
607 delete pDateStyle;
608 return NULL;
610 else//TEXT
612 //UChar buffer[1024];
613 sal_Unicode buffer[1024];
614 buffer[0] = cSymbol;
615 for (j=1;;j++)
617 cTmp = pattern[i+j];
618 if ((cTmp>='A' && cTmp<='Z') || (cTmp>='a' && cTmp<='z') ||
619 cTmp=='\'' || cTmp=='"' )
621 i=i+j;
622 buffer[j]= '\0';
623 break;
625 else
626 buffer[j] = cTmp;
629 pDateStyle->AddText(OUString(buffer));//keep for all parsed
631 break;
635 // udat_close(fmt);
636 return pDateStyle;
639 * @descr get the system time format
641 XFTimeStyle* LwpTools::GetSystemTimeStyle()
643 //1 get locale for system
644 icu::Locale aLocale( LanguageTagIcu::getIcuLocale( Application::GetSettings().GetLanguageTag()));
645 //2 get icu format pattern by locale
646 icu::DateFormat* fmt = icu::DateFormat::createTimeInstance(icu::DateFormat::DEFAULT,aLocale);
648 int32_t nLength = 0;
649 int32_t nLengthNeed;
650 UErrorCode status = U_ZERO_ERROR;
651 UChar* pattern = NULL;
652 nLengthNeed = udat_toPattern(reinterpret_cast<void **>(fmt),false,NULL,nLength,&status);
653 if (status == U_BUFFER_OVERFLOW_ERROR)
655 status = U_ZERO_ERROR;
656 nLength = nLengthNeed +1;
657 pattern = static_cast<UChar*>(malloc(sizeof(UChar)*nLength));
658 udat_toPattern(reinterpret_cast<void **>(fmt),false,pattern,nLength,&status);
661 if (pattern == NULL)
662 return NULL;
663 // 3 parse pattern string,per icu date/time format syntax, there are 20 letters reserved
664 // as patter letter,each represent a element in date/time and its repeat numbers represent
665 // different format: for exampel: M produces '1',MM produces '01', MMM produces 'Jan', MMMM produces 'Januaray'
666 // letter other than these letters is regard as text in the format, for example ','in 'Jan,2005'
667 // we parse pattern string letter by letter and get the time format.
668 // for time format ,for there is not date info,we can only parse the letter representing time.
669 UChar cSymbol;
670 UChar cTmp;
671 XFTimeStyle* pTimeStyle = new XFTimeStyle;
673 for (int32_t i=0;i<nLengthNeed;)
675 cSymbol = pattern[i];
676 int32_t j;
677 switch(cSymbol)
679 case 'h':
681 for (j=1;;j++)
683 cTmp = pattern[i+j];
684 if (cTmp != cSymbol)
686 i=i+j;
687 break;
690 if (j==1)
691 pTimeStyle->AddHour(false);
692 else
693 pTimeStyle->AddHour();
694 break;
696 case 'H':
698 for (j=1;;j++)
700 cTmp = pattern[i+j];
701 if (cTmp != cSymbol)
703 i=i+j;
704 break;
707 if (j==1)
708 pTimeStyle->AddHour(false);
709 else
710 pTimeStyle->AddHour();
711 break;
713 case 'm':
715 for (j=1;;j++)
717 cTmp = pattern[i+j];
718 if (cTmp != cSymbol)
720 i=i+j;
721 break;
724 if (j==1)
725 pTimeStyle->AddMinute(false);
726 else
727 pTimeStyle->AddMinute();
728 break;
730 case 's':
732 for (j=1;;j++)
734 cTmp = pattern[i+j];
735 if (cTmp != cSymbol)
737 i=i+j;
738 break;
741 if (j==1)
742 pTimeStyle->AddSecond(false,0);
743 else
744 pTimeStyle->AddSecond(true,0);
745 break;
747 case 'S':
749 for (j=1;;j++)
751 cTmp = pattern[i+j];
752 if (cTmp != cSymbol)
754 i=i+j;
755 break;
758 /*if (j==1)
759 pDateStyle->AddSecond(sal_False);
760 else
761 pDateStyle->AddSecond();*/
762 break;
764 case 'a':
766 for (j=1;;j++)
768 cTmp = pattern[i+j];
769 if (cTmp != cSymbol)
771 i=i+j;
772 break;
775 pTimeStyle->SetAmPm(true);
776 break;
778 case 'k':
780 for (j=1;;j++)
782 cTmp = pattern[i+j];
783 if (cTmp != cSymbol)
785 i=i+j;
786 break;
789 break;
791 case 'K':
793 for (j=1;;j++)
795 cTmp = pattern[i+j];
796 if (cTmp != cSymbol)
798 i=i+j;
799 break;
802 if (j==1)
803 pTimeStyle->AddHour(false);
804 else
805 pTimeStyle->AddHour();
806 break;
808 case '\''://'
810 for (j=1;;j++)
812 cTmp = pattern[i+j];
813 if (cTmp != cSymbol)
815 i=i+j;
816 break;
819 break;
821 case '"':
823 pTimeStyle->AddText("'");
824 break;
826 default:
828 if ((cSymbol>='A' && cSymbol<='Z') || (cSymbol>='a' && cSymbol<='z') )
830 delete pTimeStyle;
831 return NULL;
833 else//TEXT
835 sal_Unicode buffer[1024];
836 buffer[0] = cSymbol;
837 //strBuffer.append(cSymbol);
838 for (j=1;;j++)
840 cTmp = pattern[i+j];
841 if ((cTmp>='A' && cTmp<='Z') || (cTmp>='a' && cTmp<='z') ||
842 cTmp=='\'' || cTmp=='"' )
844 i=i+j;
845 buffer[j]= '\0';
846 break;
848 else
849 buffer[j] = cTmp;
851 pTimeStyle->AddText(OUString(buffer));//keep for all parsed
853 break;
857 // udat_close(fmt);
858 return pTimeStyle;
861 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */