summaryrefslogtreecommitdiff
path: root/ucb/workben/ucb/srcharg.cxx
blob: 987956ee9fc4d991b54974c4b5bfa0dd1adfe58d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_ucb.hxx"

#include <limits>
#include <com/sun/star/ucb/RuleOperator.hpp>
#include <com/sun/star/ucb/SearchInfo.hpp>
#include <com/sun/star/util/Date.hpp>
#include <tools/date.hxx>
#include <tools/inetmime.hxx>
#include <tools/string.hxx>

#include <srcharg.hxx>

namespace unnamed_chaos_ucbdemo_srcharg {}
using namespace unnamed_chaos_ucbdemo_srcharg;
    // unnamed namespaces don't work well yet...

using namespace com::sun::star;

//============================================================================
//
//  skipWhiteSpace
//
//============================================================================

namespace unnamed_chaos_ucbdemo_srcharg {

void skipWhiteSpace(sal_Unicode const *& rBegin, sal_Unicode const * pEnd)
{
    while (rBegin != pEnd
           && (*rBegin == '\n' || *rBegin == '\t' || *rBegin == ' '))
        ++rBegin;
}

//============================================================================
//
//  scanAtom
//
//============================================================================

String scanAtom(sal_Unicode const *& rBegin, sal_Unicode const * pEnd)
{
    sal_Unicode const * pTheBegin = rBegin;
    while (rBegin != pEnd && INetMIME::isAlpha(*rBegin))
        ++rBegin;
    return String(pTheBegin, rBegin - pTheBegin);
}

//============================================================================
//
//  scanProperty
//
//============================================================================

String scanProperty(sal_Unicode const *& rBegin, sal_Unicode const * pEnd)
{
    sal_Unicode const * pTheBegin = rBegin;
    while (rBegin != pEnd
           && !(*rBegin == '\n' || *rBegin == '\t' || *rBegin == ' '))
        ++rBegin;
    return String(pTheBegin, rBegin - pTheBegin);
}

//============================================================================
//
//  scanOperator
//
//============================================================================

String scanOperator(sal_Unicode const *& rBegin, sal_Unicode const * pEnd)
{
    sal_Unicode const * pTheBegin = rBegin;
    while (rBegin != pEnd
           && (INetMIME::isAlpha(*rBegin) || *rBegin == '!'
               || *rBegin >= '<' && *rBegin <= '>'))
        ++rBegin;
    return String(pTheBegin, rBegin - pTheBegin);
}

}

//============================================================================
//
//  parseSearchArgument
//
//============================================================================

bool parseSearchArgument(String const & rInput, ucb::SearchInfo & rInfo)
{
    /* Format of rInput:

       argument = *option [criterium *("OR" criterium)]

       option = ("--RECURSE" "=" ("NONE" / "ONE" / "DEEP"))
                    / (("--BASE" / "--FOLDERVIEW" / "--DOCVIEW"
                                / "--INDIRECT")
                           "=" bool)

       criterium = "EMPTY" / (term *("AND" term))

       term = text-term / date-term / numeric-term / bool-term

       text-term = property ("CONT" / "!CONT" / ">=" / "<=" / "==" / "!=")
                       string *("-C" / "-R")

       date-term = property
                       (((">=" / "<=" / "==" / "!=") date)
                            / (("OLDER" / "YOUNGER") number))

       numeric-term = property (">=" / "<=" / "==" / "!=") number

       bool-term = property ("TRUE" / "FALSE")

       property = 1*VCHAR

       string = DQUOTE
                    *(<any Unicode code point except DQUOTE or "\">
                          / ("\" %x75 4HEXDIG)  ; \uHHHH
                          / ("\" (DQUOTE / "\")))
                    DQUOTE

       date = 1*2DIGIT "/" 1*2DIGIT "/" 4DIGIT  ; mm/dd/yyyy

       number = ["+" / "-"] 1*DIGIT
    */

    sal_Unicode const * p = rInput.GetBuffer();
    sal_Unicode const * pEnd = p + rInput.Len();

    // Parse options:
    rInfo.Recursion = ucb::SearchRecursion_ONE_LEVEL;
    rInfo.IncludeBase = true;
    rInfo.RespectFolderViewRestrictions = true;
    rInfo.RespectDocViewRestrictions = false;
    rInfo.FollowIndirections = false;
    enum OptionID { OPT_RECURSE, OPT_BASE, OPT_FOLDERVIEW, OPT_DOCVIEW,
                    OPT_INDIRECT, OPT_Count };
    struct OptionInfo
    {
        bool m_bSpecified;
        sal_Bool * m_pValue;
    };
    OptionInfo aOptions[OPT_Count];
    aOptions[OPT_RECURSE].m_bSpecified = false;
    aOptions[OPT_RECURSE].m_pValue = 0;
    aOptions[OPT_BASE].m_bSpecified = false;
    aOptions[OPT_BASE].m_pValue = &rInfo.IncludeBase;
    aOptions[OPT_FOLDERVIEW].m_bSpecified = false;
    aOptions[OPT_FOLDERVIEW].m_pValue
        = &rInfo.RespectFolderViewRestrictions;
    aOptions[OPT_DOCVIEW].m_bSpecified = false;
    aOptions[OPT_DOCVIEW].m_pValue = &rInfo.RespectDocViewRestrictions;
    aOptions[OPT_INDIRECT].m_bSpecified = false;
    aOptions[OPT_INDIRECT].m_pValue = &rInfo.FollowIndirections;
    while (p != pEnd)
    {
        sal_Unicode const * q = p;

        skipWhiteSpace(q, pEnd);
        if (pEnd - q < 2 || *q++ != '-' || *q++ != '-')
            break;
        String aOption(scanAtom(q, pEnd));
        OptionID eID;
        if (aOption.EqualsIgnoreCaseAscii("recurse"))
            eID = OPT_RECURSE;
        else if (aOption.EqualsIgnoreCaseAscii("base"))
            eID = OPT_BASE;
        else if (aOption.EqualsIgnoreCaseAscii("folderview"))
            eID = OPT_FOLDERVIEW;
        else if (aOption.EqualsIgnoreCaseAscii("docview"))
            eID = OPT_DOCVIEW;
        else if (aOption.EqualsIgnoreCaseAscii("indirect"))
            eID = OPT_INDIRECT;
        else
            break;

        if (aOptions[eID].m_bSpecified)
            break;
        aOptions[eID].m_bSpecified = true;

        skipWhiteSpace(q, pEnd);
        if (q == pEnd || *q++ != '=')
            break;

        skipWhiteSpace(q, pEnd);
        String aValue(scanAtom(q, pEnd));
        if (eID == OPT_RECURSE)
        {
            if (aValue.EqualsIgnoreCaseAscii("none"))
                rInfo.Recursion = ucb::SearchRecursion_NONE;
            else if (aValue.EqualsIgnoreCaseAscii("one"))
                rInfo.Recursion = ucb::SearchRecursion_ONE_LEVEL;
            else if (aValue.EqualsIgnoreCaseAscii("deep"))
                rInfo.Recursion = ucb::SearchRecursion_DEEP;
            else
                break;
        }
        else if (aValue.EqualsIgnoreCaseAscii("true"))
            *aOptions[eID].m_pValue = true;
        else if (aValue.EqualsIgnoreCaseAscii("false"))
            *aOptions[eID].m_pValue = false;
        else
            break;

        p = q;
    }

    // Parse criteria:
    ucb::SearchCriterium aCriterium;
    for (;;)
    {
        sal_Unicode const * q = p;

        // Parse either property name or "empty":
        skipWhiteSpace(q, pEnd);
        String aProperty(scanProperty(q, pEnd));
        sal_Unicode const * pPropertyEnd = q;

        // Parse operator:
        skipWhiteSpace(q, pEnd);
        String aOperator(scanOperator(q, pEnd));
        struct Operator
        {
            sal_Char const * m_pName;
            sal_Int16 m_nText;
            sal_Int16 m_nDate;
            sal_Int16 m_nNumeric;
            sal_Int16 m_nBool;
        };
        static Operator const aOperators[]
            = { { "cont", ucb::RuleOperator::CONTAINS, 0, 0, 0 },
                { "!cont", ucb::RuleOperator::CONTAINSNOT, 0, 0, 0 },
                { ">=", ucb::RuleOperator::GREATEREQUAL,
                  ucb::RuleOperator::GREATEREQUAL,
                  ucb::RuleOperator::GREATEREQUAL, 0 },
                { "<=", ucb::RuleOperator::LESSEQUAL,
                  ucb::RuleOperator::LESSEQUAL, ucb::RuleOperator::LESSEQUAL,
                  0 },
                { "==", ucb::RuleOperator::EQUAL, ucb::RuleOperator::EQUAL,
                  ucb::RuleOperator::EQUAL, 0 },
                { "!=", ucb::RuleOperator::NOTEQUAL,
                  ucb::RuleOperator::NOTEQUAL, ucb::RuleOperator::NOTEQUAL,
                  0 },
                { "true", 0, 0, 0, ucb::RuleOperator::VALUE_TRUE },
                { "false", 0, 0, 0, ucb::RuleOperator::VALUE_FALSE } };
        int const nOperatorCount = sizeof aOperators / sizeof (Operator);
        Operator const * pTheOperator = 0;
        for (int i = 0; i < nOperatorCount; ++i)
            if (aOperator.EqualsIgnoreCaseAscii(aOperators[i].m_pName))
            {
                pTheOperator = aOperators + i;
                break;
            }
        bool bTerm = pTheOperator != 0;

        sal_Int16 nOperatorID;
        uno::Any aTheOperand;
        bool bCaseSensitive = false;
        bool bRegularExpression = false;
        if (bTerm)
        {
            skipWhiteSpace(q, pEnd);
            bool bHasOperand = false;

            // Parse string operand:
            if (!bHasOperand && pTheOperator->m_nText)
            {
                if (q != pEnd && *q == '"')
                {
                    String aString;
                    for (sal_Unicode const * r = q + 1;;)
                    {
                        if (r == pEnd)
                            break;
                        sal_Unicode c = *r++;
                        if (c == '"')
                        {
                            bHasOperand = true;
                            aTheOperand <<= rtl::OUString(aString);
                            nOperatorID = pTheOperator->m_nText;
                            q = r;
                            break;
                        }
                        if (c == '\\')
                        {
                            if (r == pEnd)
                                break;
                            c = *r++;
                            if (c == 'u')
                            {
                                if (pEnd - r < 4)
                                    break;
                                c = 0;
                                bool bBad = false;
                                for (int i = 0; i < 4; ++i)
                                {
                                    int nWeight
                                        = INetMIME::getHexWeight(*r++);
                                    if (nWeight < 0)
                                    {
                                        bBad = false;
                                        break;
                                    }
                                    c = sal_Unicode(c << 4 | nWeight);
                                }
                                if (bBad)
                                    break;
                            }
                            else if (c != '"' && c != '\\')
                                break;
                        }
                        aString += c;
                    }
                }

                // Parse "-C" and "-R":
                if (bHasOperand)
                    for (;;)
                    {
                        skipWhiteSpace(q, pEnd);
                        if (pEnd - q >= 2 && q[0] == '-'
                            && (q[1] == 'C' || q[1] == 'c')
                            && !bCaseSensitive)
                        {
                            bCaseSensitive = true;
                            q += 2;
                        }
                        else if (pEnd - q >= 2 && q[0] == '-'
                                 && (q[1] == 'R' || q[1] == 'r')
                                 && !bRegularExpression)
                        {
                            bRegularExpression = true;
                            q += 2;
                        }
                        else
                            break;
                    }
            }

            // Parse date operand:
            if (!bHasOperand && pTheOperator->m_nDate != 0)
            {
                sal_Unicode const * r = q;
                bool bOK = true;
                USHORT nMonth = 0;
                if (bOK && r != pEnd && INetMIME::isDigit(*r))
                    nMonth = INetMIME::getWeight(*r++);
                else
                    bOK = false;
                if (bOK && r != pEnd && INetMIME::isDigit(*r))
                    nMonth = 10 * nMonth + INetMIME::getWeight(*r++);
                if (!(bOK && r != pEnd && *r++ == '/'))
                    bOK = false;
                USHORT nDay = 0;
                if (bOK && r != pEnd && INetMIME::isDigit(*r))
                    nDay = INetMIME::getWeight(*r++);
                else
                    bOK = false;
                if (bOK && r != pEnd && INetMIME::isDigit(*r))
                    nDay = 10 * nDay + INetMIME::getWeight(*r++);
                if (!(bOK && r != pEnd && *r++ == '/'))
                    bOK = false;
                USHORT nYear = 0;
                for (int i = 0; bOK && i < 4; ++i)
                    if (r != pEnd && INetMIME::isDigit(*r))
                        nYear = 10 * nYear + INetMIME::getWeight(*r++);
                    else
                        bOK = false;
                if (bOK && Date(nDay, nMonth, nYear).IsValid())
                {
                    bHasOperand = true;
                    aTheOperand <<= util::Date(nDay, nMonth, nYear);
                    nOperatorID = pTheOperator->m_nDate;
                    q = r;
                }
            }

            // Parse number operand:
            if (!bHasOperand && pTheOperator->m_nNumeric != 0)
            {
                sal_Unicode const * r = q;
                bool bNegative = false;
                if (*r == '+')
                    ++r;
                else if (*r == '-')
                {
                    bNegative = true;
                    ++r;
                }
                sal_Int64 nNumber = 0;
                bool bDigits = false;
                while (r != pEnd && INetMIME::isDigit(*r))
                {
                    nNumber = 10 * nNumber + INetMIME::getWeight(*r++);
                    if (nNumber > std::numeric_limits< sal_Int32 >::max())
                    {
                        bDigits = false;
                        break;
                    }
                }
                if (bDigits)
                {
                    bHasOperand = true;
                    aTheOperand
                        <<= sal_Int32(bNegative ? -sal_Int32(nNumber) :
                                                  sal_Int32(nNumber));
                    nOperatorID = pTheOperator->m_nNumeric;
                    q = r;
                }
            }

            // Bool operator has no operand:
            if (!bHasOperand && pTheOperator->m_nBool != 0)
            {
                bHasOperand = true;
                nOperatorID = pTheOperator->m_nBool;
            }

            bTerm = bHasOperand;
        }

        bool bEmpty = false;
        if (bTerm)
        {
            aCriterium.Terms.realloc(aCriterium.Terms.getLength() + 1);
            aCriterium.Terms[aCriterium.Terms.getLength() - 1]
                = ucb::RuleTerm(aProperty, aTheOperand, nOperatorID,
                                bCaseSensitive, bRegularExpression);
        }
        else if (aCriterium.Terms.getLength() == 0
                 && aProperty.EqualsIgnoreCaseAscii("empty"))
        {
            bEmpty = true;
            q = pPropertyEnd;
        }

        if (!(bTerm || bEmpty))
            break;

        p = q;
        skipWhiteSpace(p, pEnd);

        q = p;
        String aConnection(scanAtom(q, pEnd));
        if (p == pEnd || aConnection.EqualsIgnoreCaseAscii("or"))
        {
            rInfo.Criteria.realloc(rInfo.Criteria.getLength() + 1);
            rInfo.Criteria[rInfo.Criteria.getLength() - 1] = aCriterium;
            aCriterium = ucb::SearchCriterium();
            p = q;
        }
        else if (bTerm && aConnection.EqualsIgnoreCaseAscii("and"))
            p = q;
        else
            break;
    }

    skipWhiteSpace(p, pEnd);
    return p == pEnd;
}