25 Years of Programming
An open source source for C, C++, OWL, BASIC, MDB, XLS, DOT, and more...
Home   Projects   Up   Sitemap   Search   Blog   Forum+Chat   About Us   Privacy   Terms of Use   Feedback   FAQ   Images   Services   Payments   Humor   Music

Borland C++ MSDOS program to zero the parity bit of all bytes in a file

Mapdown.cpp is yet another of my programs that strips (zeroes) the parity bit of all bytes in a file.

The parity bit is the highest bit of an 8-bit (1-byte) character. The true ASCII character set, which computers in the early 1980's used, only goes from 0-127, leaving the characters 128-255 unused.

1980's modem programs used the 8th bit as part of its error checking method. Text files captured during modem sessions would have a mixture of normal ASCII chars and illegitimate high-bit ASCII chars, with the result that the text wouldn't display properly in text editing programs. 

The solution was to "map down" the text so that all the high-bit ASCII characters were mapped down to their legitimate true ASCII equivalents.

The difference about this version from my others is that the last time I ran it, it didn't work properly. See notes below. It should be easily fixable, but it's not on my to do list.

If you have Word 2003 (and probably some earlier versions), you don't need this program because if you import your file into Word as "encoded in US-ASCII", it will do the conversion for you.

/*	MAPDOWN.CPP	  			1-24-99
	Copyright (C)1993-99, 2006 Steven Whitney.
	Initially published by http://25yearsofprogramming.com.
	Published under GNU GPL (General Public License) Version 3, with ABSOLUTELY NO WARRANTY.

	Strips (zeroes) the parity bits from the bytes in a file.
	Optionally strips all control codes except CR,LF,TAB from file.

----
Notes:

11/23/05
--I ran this on a file in Windows XP, and the end result was not correct. I didn't investigate
  it because I quickly discovered that if you import a file into Word 2003 as
  "encoded in US-ASCII", it will do the conversion for you, so this program is no longer necessary.
--This doesn't work properly on binary files, maybe because it quits when the first ^Z is encountered.

Previous user questions are now handled on command line, for batch use.
Haven't tested its control code handling, but didn't change it, either.

==> moved parity strip to BEFORE control-code test.  untested.
1-24-99 minor changes & project options changes.  untested.
note it's still small model (ok) and an old project (maybe rebuild).

Previous version left the input file as filename.bak, and gave the output file
the former name of the input file.  Unfortunately, when you do this, the
DOS command line: for %f in (*.ext) do mapdown %f doesn't work properly:
Seems like the file (or the first file, in batch processing) is processed twice.
So this program now leaves the new file with a .$$$ extension.

*/
#include "c:\bcs\my.h"
#pragma hdrstop

#include "c:\bcs\mylib.cpp"

int main(int argc, char **argv)
{
int ctlflag = TRUE;			// whether to strip control codes
string buf;                 // reused for various things

if(argc < 2)                        // min 2 arguments
{
	cout << "\nMAPDOWN strips the parity bits from a file." << endl;
	cout << "Usage: MAPDOWN inputfilename.ext [/+c]" << endl;
	cout << endl;
	cout << "The program also STRIPS OUT all control codes except CR,LF,TAB." << endl;
	cout << "You must use option /+C to leave control codes IN." << endl;
	cout << "The output file will have the same name as the input file," << endl;
	cout << "but with a .$$$ extension." << endl;
	return(1);
}
if(argc == 3)                       // third argument, if present, is "/+c"
{
	buf = argv[2];
	buf.to_lower();
	if(buf == "/+c")
		ctlflag = FALSE;
}
string outfilename(argv[1]);
unsigned i = outfilename.find(".");
if(i != NPOS)
	outfilename.remove(i);
outfilename += ".$$$";
										// open the files
ifstream infile(argv[1]);				// 1/19/06: maybe should be in binary mode?
if(!infile)
	aborts("Input file not found.");
ofstream outfile(outfilename.c_str());	// 1/19/06: maybe should be in binary mode?
if(!outfile)
	aborts("Couldn't create output file.");

unsigned char c;			// file input char
while(infile.get(c))		// process the files
{
	c &= 127;				// must map down before control-code check
	if(ctlflag)				// filter ctrl codes
		if((c < 32) && (c != 10) && (c != 13) && (c != 9))
			continue;
	outfile.put(c);    		// how to check for error?
}
infile.close();				// infile must also be closed before move
outfile.close();			// This IS necessary before DOS-move command.
if(outfile.fail())
	aborts("File write error.");
if(ctlflag)
	cout << "Any control codes in the file were STRIPPED.\n";
else
	cout << "Control-codes, if any, were LEFT IN the file.\n";
cout << "Output file is called: " << outfilename << "." << endl;
cout << "If you wish it to replace the input file, you should copy it manually." << endl;
return(0);					// return() makes it easier to turn a program
}							// into a function later.

 

 

Valid HTML 4.01 Transitional Valid CSS
Yahoo! Search
Search the web Search this site
View content labeling at ICRA.