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
|
/*
Copyright (C) <2009> <Karl Phillip Buhr>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "fpeek.h"
#include <string.h>
#include <stdlib.h>
#include <string.h>
#include <fstream>
#include <iostream>
#include <algorithm>
bool _display_line_num = false;
int main(int argc, char **argv)
{
if (argc < 2)
{
std::cerr << "Usage: " << argv[0] << " <options> <file>\n" <<
"Try '" << argv[0]<< " --help' for more information." << std::endl;
return -1;
}
// Parse command line options
std::vector<std::string> args;
args = cmd_line_handle(argc, argv);
// Checking is file exists
char* filename = argv[argc-1];
std::ifstream file(filename);
if (!file.is_open())
{
std::cerr << filename << ": No such file" << std::endl;
exit(0);
}
// We do not print lines of binary files
char c = 0;
while (file)
{
file.get(c);
if (c < -96)
{
std::cerr << filename << ": Cannot open binary file" << std::endl;
exit(0);
}
}
file.close();
display_lines(filename, args);
return 0;
}
void cmd_line_help(char* invocation)
{
std::cerr << "Usage: "<< invocation <<" [options] <file>\n\n" <<
"Options:\n" <<
" -d, Print line numbers before the result.\n" <<
" -l, --line N Print line N from file. Multiple lines must be separated by the symbol ','.\n" <<
" -r, --range N-M Print range of lines from N to M. Must be separated by '-'.\n" <<
" You can also use ',' when working with multiple ranges.\n" <<
" -h Display this message\n" <<
"\n" <<
"You can visit fpeek homepage at: https://sourceforge.net/projects/fpeek/\n" << std::endl;
exit(0);
return;
}
std::vector<std::string> cmd_line_handle(int argc, char **argv)
{
std::vector<std::string> args;
for(int i=1; i<argc; i++)
{
if (argv[i][0] != '-') continue;
if (!strcmp(argv[i], "--help") ||
!strcmp(argv[i], "-help" ) ||
!strcmp(argv[i], "-h" ))
{
cmd_line_help(argv[0]);
return args;
}
else if(!strcmp(argv[i], "-d"))
{
_display_line_num = true;
}
else if( (!strcmp(argv[i], "-l") || !strcmp(argv[i], "--line") ) && i+1 < argc)
{
args.push_back(argv[i+1]);
i++;
}
else if( (!strcmp(argv[i], "-r") || !strcmp(argv[i], "--range") ) && i+1 < argc)
{
args.push_back(argv[i+1]);
i++;
}
else
{
std::cerr << "Invalid parameter: " << argv[i] << ".\n" << std::endl;
cmd_line_help(argv[0]);
return args;
}
}
if (args.size() == 0)
cmd_line_help(argv[0]);
return args;
}
void display_lines(char* filename, std::vector<std::string> args)
{
std::vector<int> line_vec;
if (args.size() > 0)
{
for (size_t i = 0; i < args.size(); i++)
{
// Parse single lines
char* token = strtok(const_cast<char *>(args.at(i).c_str()), "," );
while (token != NULL)
{
line_vec.push_back(atoi(token));
token = strtok(NULL, ",");
}
}
for (size_t i = 0; i < args.size(); i++)
{
// Parse range of lines
int range_beg = 0;
int range_end = 0;
char* token = strtok(const_cast<char *>(args.at(i).c_str()), "-" );
while (token != NULL)
{
if (!range_beg)
range_beg = atoi(token);
else
range_end = atoi(token);
token = strtok(NULL, ",");
}
for (int i = range_beg; i <= range_end; i++)
line_vec.push_back(i);
}
}
// Magic: search through file and display lines
std::ifstream file(filename);
std::string line;
int pos = 1;
while (std::getline(file, line))
{
// Find if current line should be displayed
std::vector<int>::iterator iter = std::find(line_vec.begin(), line_vec.end(), pos);
if (*iter == pos)
{
if (_display_line_num) // If -d was requested, should print the line number too
{
std::cout << pos << ": ";
}
std::cout << line << std::endl;
}
pos++;
}
return;
}
|