2005-04-04

CR, LF, LF CR

It is very unfortunate that DOS and Windows use a different line break format than UNIX and thus the rest of the Internet. Here's a crude loop to take a UNIX-style file and convert it to DOS-style. The vim editor does this automatically, as does Wordpad, as does Emacs. But guess what? If it "looks right", you would be amazed how easily one kind of line break will break something when you or your software are expecting another kind. This code won't compile, but it will give you the right ideas.

FileInfo fi = null;
FileStream fsin = null;
FileStream fsout = null;
string tmpfn = "";
int ch = 0;
Int64 i = 0;

if (args.Length != 1)
  throw new Exception(die_usage);
fi = new FileInfo(args[0]);

if (!fi.Exists)
  throw new Exception(die_nofile);

using (fsin = new FileStream(fi.FullName,FileMode.Open)) {
  if (null == fsin) throw new Exception(die_read + fi.FullName);

  tmpfn = Path.GetTempFileName();
  using (fsout = new FileStream(tmpfn,FileMode.Truncate)) {

    for(i = 0; i < fi.Length; ++i) {
      ch = fsin.ReadByte(); // I don't know why this doesn't return a byte
      if (-1 == ch) {
        // error
        break;
      }
      if (0xA == ch)
        fsout.WriteByte(0x0D);
      fsout.WriteByte(Convert.ToByte(ch));
    }

    fsout.Flush();
  }
}

No comments: