]>
git.saurik.com Git - apple/icu.git/blob - icuSources/samples/ugrep/ugrep.cpp
1 /*************************************************************************
3 * © 2016 and later: Unicode, Inc. and others.
4 * License & terms of use: http://www.unicode.org/copyright.html#License
6 **************************************************************************
7 **************************************************************************
9 * Copyright (C) 2002-2010, International Business Machines
10 * Corporation and others. All Rights Reserved.
12 ***************************************************************************
16 // ugrep - an ICU sample program illustrating the use of ICU Regular Expressions.
18 // The use of the ICU Regex API all occurs within the main()
19 // function. The rest of the code deals with with opening files,
20 // encoding conversions, printing results, etc.
22 // This is not a full-featured grep program. The command line options
23 // have been kept to a minimum to avoid complicating the sample code.
32 #include "unicode/utypes.h"
33 #include "unicode/ustring.h"
34 #include "unicode/regex.h"
35 #include "unicode/ucnv.h"
36 #include "unicode/uclean.h"
40 // The following variables contain paramters that may be set from the command line.
42 const char *pattern
= NULL
; // The regular expression
43 int firstFileNum
; // argv index of the first file name
44 UBool displayFileName
= FALSE
;
45 UBool displayLineNum
= FALSE
;
49 // Info regarding the file currently being processed
52 int fileLen
; // Length, in UTF-16 Code Units.
54 UChar
*ucharBuf
= 0; // Buffer, holds converted file. (Simple minded program, always reads
55 // the whole file at once.
57 char *charBuf
= 0; // Buffer, for original, unconverted file data.
61 // Info regarding the line currently being processed
63 int lineStart
; // Index of first char of the current line in the file buffer
64 int lineEnd
; // Index of char following the new line sequence for the current line
68 // Converter, used on output to convert Unicode data back to char *
69 // so that it will display in non-Unicode terminal windows.
71 UConverter
*outConverter
= 0;
74 // Function forward declarations
76 void processOptions(int argc
, const char **argv
);
77 void nextLine(int start
);
80 void readFile(const char *name
);
84 //------------------------------------------------------------------------------------------
88 // Structurally, all use of the ICU Regular Expression API is in main(),
89 // and all of the supporting stuff necessary to make a running program, but
90 // not directly related to regular expressions, is factored out into these other
93 //------------------------------------------------------------------------------------------
94 int main(int argc
, const char** argv
) {
95 UBool matchFound
= FALSE
;
98 // Process the commmand line options.
100 processOptions(argc
, argv
);
103 // Create a RegexPattern object from the user supplied pattern string.
105 UErrorCode status
= U_ZERO_ERROR
; // All ICU operations report success or failure
106 // in a status variable.
108 UParseError parseErr
; // In the event of a syntax error in the regex pattern,
109 // this struct will contain the position of the
112 RegexPattern
*rePat
= RegexPattern::compile(pattern
, parseErr
, status
);
113 // Note that C++ is doing an automatic conversion
114 // of the (char *) pattern to a temporary
115 // UnicodeString object.
116 if (U_FAILURE(status
)) {
117 fprintf(stderr
, "ugrep: error in pattern: \"%s\" at position %d\n",
118 u_errorName(status
), parseErr
.offset
);
123 // Create a RegexMatcher from the newly created pattern.
126 RegexMatcher
*matcher
= rePat
->matcher(empty
, status
);
127 if (U_FAILURE(status
)) {
128 fprintf(stderr
, "ugrep: error in creating RegexMatcher: \"%s\"\n",
129 u_errorName(status
));
134 // Loop, processing each of the input files.
136 for (int fileNum
=firstFileNum
; fileNum
< argc
; fileNum
++) {
137 readFile(argv
[fileNum
]);
140 // Loop through the lines of a file, trying to match the regex pattern on each.
142 for (nextLine(0); lineStart
<fileLen
; nextLine(lineEnd
)) {
143 UnicodeString
s(FALSE
, ucharBuf
+lineStart
, lineEnd
-lineStart
);
145 if (matcher
->find()) {
159 ucnv_close(outConverter
);
161 u_cleanup(); // shut down ICU, release any cached data it owns.
163 return matchFound
? 0: 1;
168 //------------------------------------------------------------------------------------------
170 // doOptions Run through the command line options, and set
171 // the global variables accordingly.
173 // exit without returning if an error occured and
174 // ugrep should not proceed further.
176 //------------------------------------------------------------------------------------------
177 void processOptions(int argc
, const char **argv
) {
179 UBool doUsage
= FALSE
;
180 UBool doVersion
= FALSE
;
184 for(optInd
= 1; optInd
< argc
; ++optInd
) {
188 if(strcmp(arg
, "-V") == 0 || strcmp(arg
, "--version") == 0) {
192 else if(strcmp(arg
, "--help") == 0) {
195 else if(strcmp(arg
, "-n") == 0 || strcmp(arg
, "--line-number") == 0) {
196 displayLineNum
= TRUE
;
198 /* POSIX.1 says all arguments after -- are not options */
199 else if(strcmp(arg
, "--") == 0) {
204 /* unrecognized option */
205 else if(strncmp(arg
, "-", strlen("-")) == 0) {
206 printf("ugrep: invalid option -- %s\n", arg
+1);
209 /* done with options */
221 printf("ugrep version 0.01\n");
222 if (optInd
== argc
) {
227 int remainingArgs
= argc
-optInd
; // pattern file ...
228 if (remainingArgs
< 2) {
229 fprintf(stderr
, "ugrep: files or pattern are missing.\n");
234 if (remainingArgs
> 2) {
235 // More than one file to be processed. Display file names with match output.
236 displayFileName
= TRUE
;
239 pattern
= argv
[optInd
];
240 firstFileNum
= optInd
+1;
243 //------------------------------------------------------------------------------------------
247 //------------------------------------------------------------------------------------------
249 printf("ugrep [options] pattern file...\n"
250 " -V or --version display version information\n"
251 " --help display this help and exit\n"
252 " -- stop further option processing\n"
253 "-n, --line-number Prefix each line of output with the line number within its input file.\n"
258 //------------------------------------------------------------------------------------------
260 // readFile Read a file into memory, and convert it to Unicode.
262 // Since this is just a demo program, take the simple minded approach
263 // of always reading the whole file at once. No intelligent buffering
266 //------------------------------------------------------------------------------------------
267 void readFile(const char *name
) {
270 // Initialize global file variables
273 fileLen
= 0; // zero length prevents processing in case of errors.
277 // Open the file and determine its size.
279 FILE *file
= fopen(name
, "rb");
281 fprintf(stderr
, "ugrep: Could not open file \"%s\"\n", fileName
);
284 fseek(file
, 0, SEEK_END
);
285 int rawFileLen
= ftell(file
);
286 fseek(file
, 0, SEEK_SET
);
292 charBuf
= (char *)realloc(charBuf
, rawFileLen
+1); // Need error checking...
293 int t
= fread(charBuf
, 1, rawFileLen
, file
);
294 if (t
!= rawFileLen
) {
295 fprintf(stderr
, "Error reading file \"%s\"\n", fileName
);
299 charBuf
[rawFileLen
]=0;
303 // Look for a Unicode Signature (BOM) in the data
305 int32_t signatureLength
;
306 const char * charDataStart
= charBuf
;
307 UErrorCode status
= U_ZERO_ERROR
;
308 const char* encoding
= ucnv_detectUnicodeSignature(
309 charDataStart
, rawFileLen
, &signatureLength
, &status
);
310 if (U_FAILURE(status
)) {
311 fprintf(stderr
, "ugrep: ICU Error \"%s\" from ucnv_detectUnicodeSignature()\n",
312 u_errorName(status
));
316 charDataStart
+= signatureLength
;
317 rawFileLen
-= signatureLength
;
321 // Open a converter to take the file to UTF-16
324 conv
= ucnv_open(encoding
, &status
);
325 if (U_FAILURE(status
)) {
326 fprintf(stderr
, "ugrep: ICU Error \"%s\" from ucnv_open()\n", u_errorName(status
));
331 // Convert the file data to UChar.
332 // Preflight first to determine required buffer size.
334 uint32_t destCap
= ucnv_toUChars(conv
,
340 if (status
!= U_BUFFER_OVERFLOW_ERROR
) {
341 fprintf(stderr
, "ugrep: ucnv_toUChars: ICU Error \"%s\"\n", u_errorName(status
));
345 status
= U_ZERO_ERROR
;
346 ucharBuf
= (UChar
*)realloc(ucharBuf
, (destCap
+1) * sizeof(UChar
));
353 if (U_FAILURE(status
)) {
354 fprintf(stderr
, "ugrep: ucnv_toUChars: ICU Error \"%s\"\n", u_errorName(status
));
360 // Successful conversion. Set the global size variables so that
361 // the rest of the processing will proceed for this file.
370 //------------------------------------------------------------------------------------------
372 // nextLine Advance the line index variables, starting at the
373 // specified position in the input file buffer, by
374 // scanning forwrd until the next end-of-line.
376 // Need to take into account all of the possible Unicode
377 // line ending sequences.
379 //------------------------------------------------------------------------------------------
380 void nextLine(int startPos
) {
386 lineStart
= lineEnd
= startPos
;
389 if (lineEnd
>= fileLen
) {
392 UChar c
= ucharBuf
[lineEnd
];
394 if (c
== 0x0a || // Line Feed
395 c
== 0x0c || // Form Feed
396 c
== 0x0d || // Carriage Return
397 c
== 0x85 || // Next Line
398 c
== 0x2028 || // Line Separator
399 c
== 0x2029) // Paragraph separator
405 // Check for CR/LF sequence, and advance over the LF if we're in the middle of one.
406 if (lineEnd
< fileLen
&&
407 ucharBuf
[lineEnd
-1] == 0x0d &&
408 ucharBuf
[lineEnd
] == 0x0a)
415 //------------------------------------------------------------------------------------------
417 // printMatch Called when a matching line has been located.
418 // Print out the line from the file with the match, after
419 // converting it back to the default code page.
421 //------------------------------------------------------------------------------------------
424 UErrorCode status
= U_ZERO_ERROR
;
426 // If we haven't already created a converter for output, do it now.
427 if (outConverter
== 0) {
428 outConverter
= ucnv_open(NULL
, &status
);
429 if (U_FAILURE(status
)) {
430 fprintf(stderr
, "ugrep: Error opening default converter: \"%s\"\n",
431 u_errorName(status
));
436 // Convert the line to be printed back to the default 8 bit code page.
437 // If the line is too long for our buffer, just truncate it.
438 ucnv_fromUChars(outConverter
,
439 buf
, // destination buffer for conversion
440 sizeof(buf
), // capacity of destination buffer
441 &ucharBuf
[lineStart
], // Input to conversion
442 lineEnd
-lineStart
, // number of UChars to convert
444 buf
[sizeof(buf
)-1] = 0; // Add null for use in case of too long lines.
445 // The converter null-terminates its output unless
446 // the buffer completely fills.
448 if (displayFileName
) {
449 printf("%s:", fileName
);
451 if (displayLineNum
) {
452 printf("%d:", lineNum
);