'Dosify' a file by converting all instances of newline (with or without the preceeding carriage return) into the carriage return newline sequence.
| Return Type | Function name | Arguments |
|---|---|---|
| hzEcode | DosifyFile | (hzString&,hzString&,) |
Declared in file: hzTextproc.h
Defined in file : hzTextproc.cpp
Function Logic:
Function body:
hzEcode DosifyFile (hzString& tgt)hzString& src,
{
// Category: Text Processing
//
// 'Dosify' a file by converting all instances of newline (with or without the preceeding carriage return) into the carriage return newline sequence.
//
// Arguments: 1) tgt The pathname of the target file
// 2) src The pathname of the source file
//
// Returns: E_NODATA If the source file has not been supplied
// E_ARGUMENT If the target file has not been supplied
// E_NOTFOUND If the source file cannot be found
// E_OPENFAIL If the source file cannot be opened
// E_READFAIL If the source file cannot be read
// E_WRITEFAIL If the target file cannot be created or written to
// E_OK If the operation was successful
_hzfunc(__func__) ;
ifstream is ; // Input stream
ofstream os ; // Output stream
hzString target ; // Intermeadiate filename
char buf [1024]; // Working buffer
bool bSame ; // Target/Source match indicator
hzEcode rc = E_OK ; // Return code
if (!src)
{
hzerr(E_ARGUMENT, "No source file specified") ;
return E_ARGUMENT ;
}
if (!tgt)
{
hzerr(E_ARGUMENT, "No target file specified") ;
return E_ARGUMENT ;
}
// If source and target file are the same
if (tgt == src)
{ bSame = true ; target = tgt + ".x" ; }
else
{ bSame = false ; target = tgt ; }
// Seek to open source file
rc = OpenInputStrm(is, src) ;
if (rc != E_OK)
return rc ;
// Open target for writing
os.open(*target) ;
if (os.fail())
return E_WRITEFAIL ;
for (; rc == E_OK ;)
{
is.getline(buf, 1024);
if (!is.gcount())
break ;
if (is.fail())
{ rc = E_READFAIL ; break ; }
if (!buf[0])
continue ;
if (is.gcount() == 1024)
{
if (buf[1023]==CHAR_CR)
{
buf[1023]=0;
os << buf << "\r\n" ;
continue ;
}
os.write(buf, 1024);
continue ;
}
os << buf << "\r\n" ;
if (os.fail())
rc = E_WRITEFAIL ;
}
is.close() ;
os.close() ;
if (bSame)
{
// Lose the source file and rename the target back to the original
unlink(*src) ;
rename(*target, *tgt) ;
}
return rc ;
}