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,
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 /*************************************************************************
58 * For LWP filter architecture prototype
59 ************************************************************************/
61 #include <lwptools.hxx>
62 #include <rtl/ustrbuf.hxx>
63 #include <o3tl/string_view.hxx>
64 #include <osl/process.h>
65 #include <osl/thread.h>
66 #include <osl/file.hxx>
67 #include <vcl/svapp.hxx>
68 #include <vcl/settings.hxx>
69 #include <unicode/datefmt.h>
70 #include <unicode/udat.h>
71 #include <i18nlangtag/languagetagicu.hxx>
76 #define SEPARATOR '\\'
79 using namespace ::osl
;
82 * @descr read lwp unicode string from stream to OUString per aEncoding
84 void LwpTools::QuickReadUnicode(LwpObjectStream
* pObjStrm
,
85 OUString
& str
, sal_uInt16 strlen
, rtl_TextEncoding aEncoding
)
86 //strlen: length of bytes
88 OUStringBuffer
strBuf(128);
90 if( !IsUnicodePacked(pObjStrm
, strlen
) )
97 len
= std::min(sal_uInt16(1023), strlen
);
98 len
= pObjStrm
->QuickRead(buf
, len
);
100 strBuf
.append( OUString(buf
, len
, aEncoding
) );
104 str
= strBuf
.makeStringAndClear();
109 sal_Unicode unibuf
[1024];
113 bool flag
= false; //switch if unicode part reached
114 sal_uInt16 sublen
= 0;
116 sal_uInt16 readLen
= 0;
117 while(readLen
<strlen
)
119 if(!flag
) //Not unicode string
122 readbyte
= pObjStrm
->QuickReaduInt8(&bFailure
);
124 readLen
+=sizeof(readbyte
);
129 if(sublen
>0) //add it to the strBuf
131 strBuf
.append( OUString(buf
, sublen
, aEncoding
) ); //try the aEncoding
137 buf
[sublen
++] = readbyte
;
139 if(sublen
>=1023 || readLen
==strlen
) //add it to the strBuf
141 strBuf
.append( OUString(buf
, sublen
, aEncoding
) ); //try the aEncoding
145 else //unicode string
148 readword
= pObjStrm
->QuickReaduInt16(&bFailure
);
150 readLen
+=sizeof(readword
);
152 if(readword
== 0x0000)
157 unibuf
[sublen
] = '\0';
158 strBuf
.append( unibuf
);
164 unibuf
[sublen
++] = readword
;
166 if(sublen
>=1023 || readLen
==strlen
)
168 unibuf
[sublen
] = '\0';
169 strBuf
.append( unibuf
);
174 str
= strBuf
.makeStringAndClear();
179 * @descr Judge if the data (len) in object stream is lwp unicode packed
181 bool LwpTools::IsUnicodePacked(LwpObjectStream
* pObjStrm
, sal_uInt16 len
)
184 sal_uInt16 oldpos
= pObjStrm
->GetPos();
186 for (sal_uInt16 i
= 0; i
< len
; i
++)
188 byte
= pObjStrm
->QuickReaduInt8();
191 pObjStrm
->Seek(oldpos
);
195 pObjStrm
->Seek(oldpos
);
199 bool LwpTools::isFileUrl(std::string_view fileName
)
201 return o3tl::starts_with(fileName
, "file://");
204 OUString
LwpTools::convertToFileUrl(const OString
&fileName
)
206 if ( isFileUrl(fileName
) )
208 return OStringToOUString(fileName
, osl_getThreadTextEncoding());
211 OUString uUrlFileName
;
212 OUString
uFileName(fileName
.getStr(), fileName
.getLength(), osl_getThreadTextEncoding());
213 if ( fileName
.startsWith(".") || fileName
.indexOf(SEPARATOR
) < 0 )
215 OUString uWorkingDir
;
216 OSL_VERIFY( osl_getProcessWorkingDir(&uWorkingDir
.pData
) == osl_Process_E_None
);
217 OSL_VERIFY( FileBase::getAbsoluteFileURL(uWorkingDir
, uFileName
, uUrlFileName
) == FileBase::E_None
);
220 OSL_VERIFY( FileBase::getFileURLFromSystemPath(uFileName
, uUrlFileName
) == FileBase::E_None
);
226 OUString
LwpTools::DateTimeToOUString(const LtTm
&dt
)
228 OUString aResult
= OUString::number(dt
.tm_year
) + "-" + OUString::number(dt
.tm_mon
) + "-" + OUString::number(dt
.tm_mday
) +
229 "T" + OUString::number(dt
.tm_hour
) + ":" + OUString::number(dt
.tm_min
) + ":" + OUString::number(dt
.tm_sec
);
235 * @descr get the system date format
237 std::unique_ptr
<XFDateStyle
> LwpTools::GetSystemDateStyle(bool bLongFormat
)
239 icu::DateFormat::EStyle style
;
241 style
= icu::DateFormat::FULL
;//system full date format
243 style
= icu::DateFormat::SHORT
;//system short date format
245 //1 get locale for system
246 icu::Locale
aLocale( LanguageTagIcu::getIcuLocale( Application::GetSettings().GetLanguageTag()));
247 //2 get icu format pattern by locale
248 icu::DateFormat
* fmt
= icu::DateFormat::createDateInstance(style
,aLocale
);
252 UErrorCode status
= U_ZERO_ERROR
;
253 UChar
* pattern
= nullptr;
255 nLengthNeed
= udat_toPattern(reinterpret_cast<void **>(fmt
),false,nullptr,nLength
,&status
);
256 if (status
== U_BUFFER_OVERFLOW_ERROR
)
258 status
= U_ZERO_ERROR
;
259 nLength
= nLengthNeed
+1;
260 pattern
= static_cast<UChar
*>(malloc(sizeof(UChar
)*nLength
));
261 udat_toPattern(reinterpret_cast<void **>(fmt
),false,pattern
,nLength
,&status
);
263 if (pattern
== nullptr)
265 // 3 parse pattern string,per icu date/time format syntax, there are 20 letters reserved
266 // as pattern letter,each represent an element in date/time and its repeat numbers represent
267 // different format: for example: M produces '1', MM produces '01', MMM produces 'Jan', MMMM produces 'January'
268 // letter other than these letters is regard as text in the format, for example ',' in 'Jan,2005'
269 // we parse pattern string letter by letter and get the time format.
272 std::unique_ptr
<XFDateStyle
> pDateStyle(new XFDateStyle
);
274 for (int32_t i
=0;i
<nLengthNeed
;)
276 cSymbol
= pattern
[i
];
291 pDateStyle
->AddEra();
306 pDateStyle
->AddYear(false);
308 pDateStyle
->AddYear();
323 pDateStyle
->AddMonth(false);
325 pDateStyle
->AddMonth();
327 pDateStyle
->AddMonth(false,true);
329 pDateStyle
->AddMonth(true,true);
344 pDateStyle
->AddMonthDay(false);
346 pDateStyle
->AddMonthDay();
361 pDateStyle
->AddHour(false);
363 pDateStyle
->AddHour();
378 pDateStyle
->AddHour(false);
380 pDateStyle
->AddHour();
395 pDateStyle
->AddMinute(false);
397 pDateStyle
->AddMinute();
412 pDateStyle
->AddSecond(false);
414 pDateStyle
->AddSecond();
429 pDateStyle->AddSecond(sal_False);
431 pDateStyle->AddSecond();*/
446 pDateStyle
->AddWeekDay(false);
448 pDateStyle
->AddWeekDay();
463 pDateStyle->AddWeekDay(sal_False);
465 pDateStyle->AddWeekDay();*/
480 pDateStyle->AddWeekDay(sal_False);
482 pDateStyle->AddWeekDay();*/
497 pDateStyle->AddWeekDay(sal_False);
499 pDateStyle->AddWeekDay();*/
514 pDateStyle->AddWeekDay(sal_False);
516 pDateStyle->AddWeekDay();*/
530 pDateStyle
->AddAmPm();
558 pDateStyle
->AddHour(false);
560 pDateStyle
->AddHour();
591 pDateStyle
->AddText(u
"'"_ustr
);
596 if ((cSymbol
>='A' && cSymbol
<='Z') || (cSymbol
>='a' && cSymbol
<='z') )
602 //UChar buffer[1024];
603 sal_Unicode buffer
[1024];
608 if ((cTmp
>='A' && cTmp
<='Z') || (cTmp
>='a' && cTmp
<='z') ||
609 cTmp
=='\'' || cTmp
=='"' )
619 pDateStyle
->AddText(OUString(buffer
));//keep for all parsed
629 * @descr get the system time format
631 std::unique_ptr
<XFTimeStyle
> LwpTools::GetSystemTimeStyle()
633 //1 get locale for system
634 icu::Locale
aLocale( LanguageTagIcu::getIcuLocale( Application::GetSettings().GetLanguageTag()));
635 //2 get icu format pattern by locale
636 icu::DateFormat
* fmt
= icu::DateFormat::createTimeInstance(icu::DateFormat::DEFAULT
,aLocale
);
640 UErrorCode status
= U_ZERO_ERROR
;
641 UChar
* pattern
= nullptr;
642 nLengthNeed
= udat_toPattern(reinterpret_cast<void **>(fmt
),false,nullptr,nLength
,&status
);
643 if (status
== U_BUFFER_OVERFLOW_ERROR
)
645 status
= U_ZERO_ERROR
;
646 nLength
= nLengthNeed
+1;
647 pattern
= static_cast<UChar
*>(malloc(sizeof(UChar
)*nLength
));
648 udat_toPattern(reinterpret_cast<void **>(fmt
),false,pattern
,nLength
,&status
);
651 if (pattern
== nullptr)
653 // 3 parse pattern string,per icu date/time format syntax, there are 20 letters reserved
654 // as pattern letter,each represent an element in date/time and its repeat numbers represent
655 // different format: for example: M produces '1',MM produces '01', MMM produces 'Jan', MMMM produces 'Januaray'
656 // letter other than these letters is regard as text in the format, for example ','in 'Jan,2005'
657 // we parse pattern string letter by letter and get the time format.
658 // for time format ,for there is not date info,we can only parse the letter representing time.
661 std::unique_ptr
<XFTimeStyle
> pTimeStyle(new XFTimeStyle
);
663 for (int32_t i
=0;i
<nLengthNeed
;)
665 cSymbol
= pattern
[i
];
681 pTimeStyle
->AddHour(false);
683 pTimeStyle
->AddHour();
698 pTimeStyle
->AddHour(false);
700 pTimeStyle
->AddHour();
715 pTimeStyle
->AddMinute(false);
717 pTimeStyle
->AddMinute();
732 pTimeStyle
->AddSecond(false);
734 pTimeStyle
->AddSecond();
749 pDateStyle->AddSecond(sal_False);
751 pDateStyle->AddSecond();*/
765 pTimeStyle
->SetAmPm(true);
793 pTimeStyle
->AddHour(false);
795 pTimeStyle
->AddHour();
813 pTimeStyle
->AddText(u
"'"_ustr
);
818 if ((cSymbol
>='A' && cSymbol
<='Z') || (cSymbol
>='a' && cSymbol
<='z') )
824 sal_Unicode buffer
[1024];
826 //strBuffer.append(cSymbol);
830 if ((cTmp
>='A' && cTmp
<='Z') || (cTmp
>='a' && cTmp
<='z') ||
831 cTmp
=='\'' || cTmp
=='"' )
840 pTimeStyle
->AddText(OUString(buffer
));//keep for all parsed
850 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */