|
1 // Grep.cpp |
|
2 // |
|
3 // Copyright (c) 2009 - 2010 Accenture. All rights reserved. |
|
4 // |
|
5 // Redistribution and use in source and binary forms, with or without |
|
6 // modification, are permitted provided that the following conditions are |
|
7 // met: |
|
8 // |
|
9 // * Redistributions of source code must retain the above copyright |
|
10 // notice, this list of conditions and the following disclaimer. |
|
11 // * Redistributions in binary form must reproduce the above |
|
12 // copyright notice, this list of conditions and the following disclaimer |
|
13 // in the documentation and/or other materials provided with the |
|
14 // distribution. |
|
15 // * Neither the name of Accenture nor the names of its |
|
16 // contributors may be used to endorse or promote products derived from |
|
17 // this software without specific prior written permission. |
|
18 // |
|
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
30 // |
|
31 #include <fshell/ioutils.h> |
|
32 #include <fshell/common.mmh> |
|
33 #include <fshell/descriptorutils.h> |
|
34 #include <fshell/pcre/cregex.h> |
|
35 |
|
36 using namespace IoUtils; |
|
37 using namespace LtkUtils; |
|
38 |
|
39 class CCmdGrep : public CCommandBase |
|
40 { |
|
41 public: |
|
42 static CCommandBase* NewLC(); |
|
43 ~CCmdGrep(); |
|
44 private: |
|
45 CCmdGrep(); |
|
46 private: // From CCommandBase. |
|
47 virtual const TDesC& Name() const; |
|
48 virtual void DoRunL(); |
|
49 virtual void ArgumentsL(RCommandArgumentList& aArguments); |
|
50 virtual void OptionsL(RCommandOptionList& aOptions); |
|
51 private: |
|
52 HBufC* iPattern; |
|
53 TBool iIgnoreCase; |
|
54 TBool iInvertMatch; |
|
55 TBool iCount; |
|
56 TBool iUnicode; |
|
57 |
|
58 RLtkBuf8 iNarrowBuf; |
|
59 CRegEx* iRegex; |
|
60 }; |
|
61 |
|
62 EXE_BOILER_PLATE(CCmdGrep) |
|
63 |
|
64 CCommandBase* CCmdGrep::NewLC() |
|
65 { |
|
66 CCmdGrep* self = new(ELeave) CCmdGrep(); |
|
67 CleanupStack::PushL(self); |
|
68 self->BaseConstructL(); |
|
69 return self; |
|
70 } |
|
71 |
|
72 CCmdGrep::~CCmdGrep() |
|
73 { |
|
74 delete iPattern; |
|
75 delete iRegex; |
|
76 iNarrowBuf.Close(); |
|
77 } |
|
78 |
|
79 CCmdGrep::CCmdGrep() |
|
80 { |
|
81 } |
|
82 |
|
83 const TDesC& CCmdGrep::Name() const |
|
84 { |
|
85 _LIT(KName, "grep"); |
|
86 return KName; |
|
87 } |
|
88 |
|
89 void CCmdGrep::ArgumentsL(RCommandArgumentList& aArguments) |
|
90 { |
|
91 aArguments.AppendStringL(iPattern, _L("pattern")); |
|
92 } |
|
93 |
|
94 void CCmdGrep::OptionsL(RCommandOptionList& aOptions) |
|
95 { |
|
96 _LIT(KCmdGrepOptIgnoreCase, "ignore-case"); |
|
97 _LIT(KCmdGrepOptInvertMatch, "invert-match"); |
|
98 _LIT(KCmdGrepOptCount, "count"); |
|
99 _LIT(KCmdGrepOptUnicode, "unicode"); |
|
100 aOptions.AppendBoolL(iIgnoreCase, KCmdGrepOptIgnoreCase); |
|
101 aOptions.AppendBoolL(iInvertMatch, KCmdGrepOptInvertMatch); |
|
102 aOptions.AppendBoolL(iCount, KCmdGrepOptCount); |
|
103 aOptions.AppendBoolL(iUnicode, KCmdGrepOptUnicode); |
|
104 } |
|
105 |
|
106 void CCmdGrep::DoRunL() |
|
107 { |
|
108 TRegExOptions options(EPcreExtended|EPcreNewlineAny); |
|
109 if (iUnicode) |
|
110 { |
|
111 iNarrowBuf.CopyAsUtf8L(*iPattern); |
|
112 options.SetUtf8(ETrue); |
|
113 } |
|
114 else |
|
115 { |
|
116 iNarrowBuf.AppendL(*iPattern); |
|
117 } |
|
118 if (iIgnoreCase) options.SetCaseless(ETrue); |
|
119 |
|
120 iRegex = CRegEx::NewL(iNarrowBuf, options); |
|
121 |
|
122 Stdin().SetReadMode(RIoReadHandle::ELine); |
|
123 TBuf<0x100> line; |
|
124 TInt count = 0; |
|
125 while (Stdin().Read(line) == KErrNone) |
|
126 { |
|
127 iNarrowBuf.Zero(); |
|
128 if (iUnicode) |
|
129 { |
|
130 iNarrowBuf.CopyAsUtf8L(line); |
|
131 } |
|
132 else |
|
133 { |
|
134 iNarrowBuf.AppendL(line); |
|
135 } |
|
136 |
|
137 TBool matches = iRegex->PartialMatchL(iNarrowBuf); |
|
138 |
|
139 if (iInvertMatch) |
|
140 { |
|
141 matches = !matches; |
|
142 } |
|
143 if (matches) |
|
144 { |
|
145 if (iCount) |
|
146 { |
|
147 count++; |
|
148 } |
|
149 else |
|
150 { |
|
151 Write(line); |
|
152 } |
|
153 } |
|
154 } |
|
155 |
|
156 if (iCount) |
|
157 { |
|
158 Printf(_L("Count = %d"), count); |
|
159 } |
|
160 } |