Commit 739de4cee381d5890ccddfbb3692d5e1fa9d3a31
- Diff rendering mode:
- inline
- side by side
bs_grep.h
(16 / 0)
|   | |||
| 1 | #pragma once | ||
| 2 | /* | ||
| 3 | Copyright (c) 2009, Mendeley Limited <fred.emmott@mendeley.com> | ||
| 4 | |||
| 5 | Permission to use, copy, modify, and/or distribute this software for any | ||
| 6 | purpose with or without fee is hereby granted, provided that the above | ||
| 7 | copyright notice and this permission notice appear in all copies. | ||
| 8 | |||
| 9 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| 10 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| 11 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| 12 | */ | ||
| 13 | |||
| 14 | #include <inttypes.h> | ||
| 15 | |||
| 16 | int bs_grep(const char* fileName, int fieldNumber, int64_t startValue, int64_t endValue, bool reverse); |
main.cpp
(73 / 0)
|   | |||
| 1 | /* | ||
| 2 | Copyright (c) 2009, Mendeley Limited <fred.emmott@mendeley.com> | ||
| 3 | |||
| 4 | Permission to use, copy, modify, and/or distribute this software for any | ||
| 5 | purpose with or without fee is hereby granted, provided that the above | ||
| 6 | copyright notice and this permission notice appear in all copies. | ||
| 7 | |||
| 8 | THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
| 9 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
| 10 | OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
| 11 | */ | ||
| 12 | |||
| 13 | #include "bs_grep.h" | ||
| 14 | |||
| 15 | #include <iostream> | ||
| 16 | |||
| 17 | #include <stdlib.h> | ||
| 18 | #include <unistd.h> | ||
| 19 | |||
| 20 | using namespace std; | ||
| 21 | |||
| 22 | int main(int argc, char** argv) | ||
| 23 | { | ||
| 24 | int argument; | ||
| 25 | int fieldNumber = -1; | ||
| 26 | int64_t startValue = - 1; | ||
| 27 | int64_t endValue = -1; | ||
| 28 | bool reverse = false; | ||
| 29 | bool badArgument = false; | ||
| 30 | |||
| 31 | while((argument = ::getopt(argc, argv, "f:s:e:r")) != -1) | ||
| 32 | { | ||
| 33 | switch(argument) | ||
| 34 | { | ||
| 35 | case 'f': | ||
| 36 | fieldNumber = atoi(::optarg); | ||
| 37 | break; | ||
| 38 | case 's': | ||
| 39 | startValue = atoll(::optarg); | ||
| 40 | break; | ||
| 41 | case 'e': | ||
| 42 | endValue = atoll(::optarg); | ||
| 43 | break; | ||
| 44 | case 'r': | ||
| 45 | reverse = true; | ||
| 46 | break; | ||
| 47 | case '?': | ||
| 48 | badArgument = true; | ||
| 49 | break; | ||
| 50 | default: | ||
| 51 | break; | ||
| 52 | } | ||
| 53 | } | ||
| 54 | if(fieldNumber == -1 || (startValue == -1 && endValue == -1) || argc - ::optind < 1 || badArgument) | ||
| 55 | { | ||
| 56 | cerr << "Syntax: " << argv[0] << " [-r] -f FIELDNUMBER [-s start [-e end] | -e end] FILE1 [FILE2 [FILE3 ...] ]" << endl; | ||
| 57 | cerr << "\t-r Count FIELDNUMBER from the end of each line, not the start" << endl; | ||
| 58 | cerr << "\t-f The field number to use for the binary search" << endl; | ||
| 59 | cerr << "\t-s Start value" << endl; | ||
| 60 | cerr << "\t-e End value" << endl; | ||
| 61 | return 1; | ||
| 62 | } | ||
| 63 | for(int i = ::optind; i < argc; ++i) | ||
| 64 | { | ||
| 65 | const char* const filename = argv[i]; | ||
| 66 | const int result = bs_grep(filename, fieldNumber, startValue, endValue, reverse); | ||
| 67 | if(result != 0) | ||
| 68 | { | ||
| 69 | cerr << "Error while processing file " << filename << endl; | ||
| 70 | return result; | ||
| 71 | } | ||
| 72 | } | ||
| 73 | } |

