Re: c++ conversion files

From: Jonathan Turkanis (technews_at_kangaroologic.com)
Date: 12/06/04


Date: Mon, 6 Dec 2004 13:04:29 -0700


<jcoffin@taeus.com> wrote in message
news:1102360105.134738.14420@z14g2000cwz.googlegroups.com...
> Hi Johathan,

Hi.,

> To me, your code

Which code?

> looks far more impenetratable than self-explanatory.
> If I had to guess at what it did, I'd have guessed at it treating
> UNIX-style new-lines as the default. It associates Windows-style
> new-lines with the input, and nothing in particular with the output, so
> from reading the code, I'd have guessed this was supposd to do
> Windows->UNIX conversion.

I assume you mean this:

> using namespace std;
> using namespace boost::io;
>
> ifstream in("src", ios::binary | ios::in);
> ofstream out("dest", ios::binary | ios::out);
>
> filtering_istream fin;
> fin.push(newline_filter(newline::windows));
> fin.push(in);
> boost::io::copy(fin, out);

> I've reread your code a number of times, and I'm _utterly_ lost as to
> what you think would give the reader even the faintest hint that it
> does anything related to MacOS at all.

Well,

- filtering_istream is an input stream which can zero or have one or more
filters added to it.
- newline_filter is a filter which, quoting from the documentation I cited,
"converts between the text file formats used by various operating systems. Its
sole constructor takes an integral flag parameter used to specify the source and
target formats "
- newline::windows is an integral flag, which is "Useful for converting data to
the Windows format"

Maybe you would be happier if the constant newline::windows were renamed
newline::convert_to_windows_format or newline::convert_to_crlf? Perhaps that's
not a bad idea, but I like shorter names.

> By contrast, looking at:
>
> FILE *fin = fopen("src", "rb");
> FILE *fout = fopen("dest", "wb");
> int ch;
>
> while ( EOF!=(ch=getc(fin))) {
> putc(ch, fout);
> if (ch == '\r')
> putc('\n', fout);
> }
>
> The actual conversion seems (to me) almost impossible to miss. The only
> part open to any question at all is whether the reader realizes what
> OSes are associated with CR-only vs. CR/LF line-ends. I'd have to
> guess, however, that any programmer who cares to read Mac text files
> under Windows would immediately recognize what it does.

First, your code is hard-wired to use FILEs. My library works for any types
modeling the concepts Source and Sink.

   http://home.comcast.net/~jturkanis/iostreams/libs/io/doc/?path=5.1

Second, converting line endings is common enough that it's worth encapsulating.

> As far as extensibilty goes, consider transferring a document from text
> format into a word processor. For the sake of discussion, assume we
> want to convert each paragraph into (in essence) one long line so the
> word processor can change line breaks as needed for the margins being
> used. If there are two or more new-lines in a row, one is retained to
> mark the end of the paragraph, but all other new-lines are deleted.

Right, newline filter doesn't do this, and doesn't pretend to.

> Now, I don't understand your code well enough to say it can't do this
> job. OTOH, if it takes more than about a minute to figure out how to
> get your code to do it, doing so is more trouble than directly writing
> the code to do so.

Basically, you can take whatever code you would write in your "while (
EOF!=(ch=getc(fin)))" loop, replace getc, putc, etc. with their generic
counterparts boost::io::get, boost::io::put, etc., stick the code into the body
of a filter member function, and Voila! --you have a reusable component.

E.g., for converting from mac to windows, based on your code:

   using namespace boost::io;

   struct convert_to_windows : boost::io::output_filter {
       template<typename Sink>
       void put(Sink& sink, char c)
       {
            boost::io::put(Sink, c);
            if (ch == '\r')
                boost::io::put(Sink, '\n');
       }
   };

For the word-processor example, assuming all newlines have been converted to
'\n' using the (chainable) newline_filter, we can write:

   struct remove_excess_newlines : boost::io::output_filter {
       remove_excess_newlines() : last_char_was_newline_(false) { }

       template<typename Sink>
       void put(Sink& sink, char c)
       {
            bool current_char_is_newline = c == '\n';
            if (!current_char_is_newline || !last_char_was_newline_)
                boost::io::put(Sink, c);
            last_char_was_newline_ = current_char_is_newline;
       }

       bool last_char_was_newline_;
   };

This is untested, but I'm sure something equally trivial will do.

> Later,
> Jerry.

Jonathan



Relevant Pages

  • Re: Adding color space converter filter to WINCE project
    ... Keep in mind that the Windows CE color space converter only supports a very ... the various YUV formats that camera drivers use to RGB for rendering the ... You may need to write your own filter for the conversion. ... Windows CE Multimedia and Graphics ...
    (microsoft.public.windowsce.app.development)
  • Re: Office 2007 data loss on flash drives
    ... I also tried safely removing the flash drives by closing everything ... Windows should prevent dismounting the flash drive until Office is done ... converting the file. ... converter for the formats. ...
    (microsoft.public.office.misc)
  • Re: Simple, yet effective Windows login hack.
    ... the primary HDD attached to the system and highlight the Windows ... closely at how we use USB thumb drives. ... the CLI and converting the drive to NTFS. ... One thing to watch for when converting to NTFS is the permissions. ...
    (alt.2600)
  • Re: Converting x-axis in terms of frequency
    ... I'm having difficulty labelling and converting my x-axis. ... You remove the low frequency terms by applying a high pass filter to ... If you have the signal processing toolbox, you can design ...
    (comp.soft-sys.matlab)
  • Re: Simple, yet effective Windows login hack.
    ... the primary HDD attached to the system and highlight the Windows ... closely at how we use USB thumb drives. ... the CLI and converting the drive to NTFS. ... One thing to watch for when converting to NTFS is the permissions. ...
    (alt.2600)