#include #include #include #include "FileDescriptor.hh" #include "Exception.hh" #include "endian.hh" using namespace std; using namespace pbe; static void usage(void) { cerr << "Usage: sercommhdr [filename]\n" << "If no filename is given, standard input is read.\n" << "A 16-byte header containing the length of the remaining\n" << "data in its first 4 bytes (big endian) is first output.\n" << "Then the input is read and endian-swapped until end-of-file.\n" << "Output is sent to standard output.\n" << "If the input terminates on a non-word boundary the\n" << "output will be padded to the word boundary with zeros\n" << "and a warning will be output to standard error.\n"; } static void sercommhdr(FileDescriptor& infd) { string s = infd.readall(); while (s.length()%4 > 0) { s += '\0'; } const uint32_t* p = reinterpret_cast(s.data()); unsigned int words = s.length()/4; uint32_t swapped[words]; for (unsigned int w=0; w(hdr),sizeof(hdr)); outfd.writeall(reinterpret_cast(swapped),sizeof(swapped)); } int main(int argc, char* argv[]) { try { try { if (argc>2) { usage(); exit(1); } if (argc==2) { string arg(argv[1]); if (arg=="--help") { usage(); exit(0); } FileDescriptor infd(arg,FileDescriptor::read_only); sercommhdr(infd); exit(0); } else { FileDescriptor infd(0); sercommhdr(infd); exit(0); } } RETHROW_MISC_EXCEPTIONS } catch (Exception& E) { E.report(cerr); exit(1); } }