]>
git.proxmox.com Git - ceph.git/blob - ceph/src/tools/ceph-diff-sorted.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
5 * diffsorted -- a utility to compute a line-by-line diff on two
8 * Copyright © 2019 Red Hat
10 * Author: J. Eric Ivancich
12 * This is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public
14 * License version 2.1, as published by the Free Software
22 * The `diffsorted` utility does a line-by-line diff on two sorted text
23 * files and indicating lines that are in one file but not the other
24 * using diff-style notation (although line numbers are not indicated).
28 * rgw-diff-sorted file1.txt file2.txt
32 * Each files should have its lines in sorted order and should have no
35 * A potential input file can be sorted using the `sort` utility provided
36 * that LANG=C to insure byte lexical order. For example:
38 * LANG=C sort unsorted.txt >sorted.txt
43 * sort unsorted.txt >sorted.txt
49 * 2 : usage problem (e.g., wrong number of command-line arguments)
50 * 3 : problem opening input file
51 * 4 : bad file content (e.g., unsorted order or empty lines)
62 std::string this_line
, prev_line
;
66 FileOfLines(const char* _filename
) :
73 void dump(const std::string
& prefix
) {
75 std::cout
<< prefix
<< this_line
<< std::endl
;
94 prev_line
= this_line
;
95 std::getline(input
, this_line
);
96 if (this_line
.empty()) {
98 std::cerr
<< "Error: " << filename
<< " has an empty line." <<
104 } else if (input
.eof()) {
108 if (this_line
< prev_line
) {
109 std::cerr
<< "Error: " << filename
<< " is not in sorted order; \"" <<
110 this_line
<< "\" follows \"" << prev_line
<< "\"." << std::endl
;
115 const std::string
line() const {
120 int main(int argc
, const char* argv
[]) {
122 std::cerr
<< "Usage: " << argv
[0] << " <file1> <file2>" << std::endl
;
126 FileOfLines
input1(argv
[1]);
127 if (!input1
.good()) {
128 std::cerr
<< "Error opening " << argv
[1] <<
133 FileOfLines
input2(argv
[2]);
134 if (!input2
.good()) {
135 std::cerr
<< "Error opening " << argv
[2] <<
140 bool files_same
= true;
145 while (!input1
.eof() && !input2
.eof()) {
146 if (input1
.line() == input2
.line()) {
149 } else if (input1
.line() < input2
.line()) {
151 std::cout
<< "< " << input1
.line() << std::endl
;
155 std::cout
<< "> " << input2
.line() << std::endl
;
163 } else if (!input2
.eof()) {