mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 08:18:11 +00:00
Utilities: cut: Split up into file and line processing
This commit is contained in:
parent
4d18b21fb5
commit
96ea4f71bd
1 changed files with 17 additions and 12 deletions
|
@ -120,7 +120,7 @@ static bool expand_list(String& list, Vector<Range>& ranges)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void cut_file(const String& file, const Vector<Range>& byte_vector)
|
static void cut_file(const String& file, const Vector<Range>& ranges, void (*process_line)(char*, size_t, const Vector<Range>&))
|
||||||
{
|
{
|
||||||
FILE* fp = stdin;
|
FILE* fp = stdin;
|
||||||
if (!file.is_null()) {
|
if (!file.is_null()) {
|
||||||
|
@ -142,16 +142,7 @@ static void cut_file(const String& file, const Vector<Range>& byte_vector)
|
||||||
line[line_length - 1] = '\0';
|
line[line_length - 1] = '\0';
|
||||||
line_length--;
|
line_length--;
|
||||||
|
|
||||||
for (auto& i : byte_vector) {
|
process_line(line, line_length, ranges);
|
||||||
if (i.m_from >= (size_t) line_length)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
|
|
||||||
auto to = min(i.m_to, line_length);
|
|
||||||
auto sub_string = String(line).substring(i.m_from - 1, to - i.m_from + 1);
|
|
||||||
out("{}", sub_string);
|
|
||||||
}
|
|
||||||
outln();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (line)
|
if (line)
|
||||||
|
@ -161,6 +152,20 @@ static void cut_file(const String& file, const Vector<Range>& byte_vector)
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void process_line_bytes(char* line, size_t length, const Vector<Range>& ranges)
|
||||||
|
{
|
||||||
|
for (auto& i : ranges) {
|
||||||
|
if (i.m_from >= length)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
|
||||||
|
auto to = min(i.m_to, length);
|
||||||
|
auto sub_string = String(line).substring(i.m_from - 1, to - i.m_from + 1);
|
||||||
|
out("{}", sub_string);
|
||||||
|
}
|
||||||
|
outln();
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv)
|
int main(int argc, char** argv)
|
||||||
{
|
{
|
||||||
String byte_list = "";
|
String byte_list = "";
|
||||||
|
@ -210,7 +215,7 @@ int main(int argc, char** argv)
|
||||||
|
|
||||||
/* Process each file */
|
/* Process each file */
|
||||||
for (auto& file : files)
|
for (auto& file : files)
|
||||||
cut_file(file, disjoint_ranges);
|
cut_file(file, disjoint_ranges, process_line_bytes);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue