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
|
#include <algorithm>
#include <cctype>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
//----------------------------------------------------------------------------
bool ci_equal( char a, char b )
{
return tolower( a ) == tolower( b );
}
bool ci_begins( const string& pattern, const string& s )
{
return (pattern.length() <= s.length())
&& equal( pattern.begin(), pattern.end(), s.begin(), ci_equal );
}
//----------------------------------------------------------------------------
#ifdef __WIN32__
#include <windows.h>
bool cin_is_a_human()
{
DWORD mode;
return GetConsoleMode( GetStdHandle( STD_INPUT_HANDLE ), &mode );
}
#else
#include <unistd.h>
bool cin_is_a_human()
{
return isatty( 0 );
}
#endif
//----------------------------------------------------------------------------
string substr( const string& s, string::size_type pos )
{
string result = s.substr( pos );
result.erase( 0, result.find_first_not_of( " \n" ) );
return result;
}
//----------------------------------------------------------------------------
int main( int argc, char** argv )
{
if (cin_is_a_human())
{
cerr << "usage:\n"
" " << argv[ 0 ] << " < combined.txt > output.txt\n\n";
return 1;
}
cout << left;
string s;
while (getline( cin, s ))
{
if (ci_begins( "PartID:", s ))
try
{
cout << "PartID: " << setw( 7 ) << substr( s, 7 ) << " ";
getline( cin, s );
if (!ci_begins( "NumAvailable:", s )) throw 0;
cout << "NumAvailable: " << setw( 9 ) << substr( s, 13 ) << " ";
getline( cin, s );
if (!ci_begins( "ExpectedDate:", s )) throw 0;
cout << "ExpectedDate: " << substr( s, 13 ) << "\n";
}
catch (...)
{
cout << "...incomplete record\n";
}
}
return 0;
}
|