1998-12-08 05:22:54 +03:00
|
|
|
#include "nsFileSpec.h"
|
|
|
|
#include "nsFileStream.h"
|
|
|
|
|
1998-12-22 23:06:56 +03:00
|
|
|
#ifdef NS_USING_STL
|
|
|
|
using std::endl;
|
|
|
|
using std::cout;
|
|
|
|
#endif
|
|
|
|
|
1998-12-09 11:47:30 +03:00
|
|
|
NS_NAMESPACE FileTest
|
1998-12-08 05:22:54 +03:00
|
|
|
{
|
1998-12-19 02:06:54 +03:00
|
|
|
NS_NAMESPACE_PROTOTYPE void WriteStuff(nsOutputFileStream& s);
|
1998-12-09 11:47:30 +03:00
|
|
|
} NS_NAMESPACE_END
|
1998-12-08 05:22:54 +03:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
1998-12-19 02:06:54 +03:00
|
|
|
void FileTest::WriteStuff(nsOutputFileStream& s)
|
1998-12-08 05:22:54 +03:00
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
// Initialize a URL from a string without suffix. Change the path to suit your machine.
|
|
|
|
nsFileURL fileURL("file:///Development/MPW/MPW%20Shell");
|
1998-12-19 02:06:54 +03:00
|
|
|
s << "File URL initialized to: \"" << fileURL << "\""<< nsEndl;
|
1998-12-08 05:22:54 +03:00
|
|
|
|
|
|
|
// Initialize a Unix path from a URL
|
1998-12-09 01:45:42 +03:00
|
|
|
nsFilePath filePath(fileURL);
|
1998-12-19 02:06:54 +03:00
|
|
|
s << "As a unix path: \"" << (const char*)filePath << "\""<< nsEndl;
|
1998-12-08 05:22:54 +03:00
|
|
|
|
|
|
|
// Initialize a native file spec from a URL
|
|
|
|
nsNativeFileSpec fileSpec(fileURL);
|
1998-12-19 02:06:54 +03:00
|
|
|
s << "As a file spec: " << fileSpec << nsEndl;
|
1998-12-08 05:22:54 +03:00
|
|
|
|
|
|
|
// Make the spec unique (this one has no suffix).
|
|
|
|
fileSpec.MakeUnique();
|
1998-12-19 02:06:54 +03:00
|
|
|
s << "Unique file spec: " << fileSpec << nsEndl;
|
1998-12-08 05:22:54 +03:00
|
|
|
|
|
|
|
// Assign the spec to a URL
|
|
|
|
fileURL = fileSpec;
|
1998-12-19 02:06:54 +03:00
|
|
|
s << "File URL assigned from spec: \"" << fileURL << "\""<< nsEndl;
|
1998-12-08 05:22:54 +03:00
|
|
|
|
|
|
|
// Assign a unix path using a string with a suffix.
|
|
|
|
filePath = "/Development/MPW/SysErrs.err";
|
1998-12-19 02:06:54 +03:00
|
|
|
s << "File path reassigned to: \"" << (const char*)filePath << "\""<< nsEndl;
|
1998-12-08 05:22:54 +03:00
|
|
|
|
|
|
|
// Assign to a file spec using a unix path.
|
|
|
|
fileSpec = filePath;
|
1998-12-19 02:06:54 +03:00
|
|
|
s << "File spec reassigned to: " << fileSpec << nsEndl;
|
1998-12-08 05:22:54 +03:00
|
|
|
|
|
|
|
// Make this unique (this one has a suffix).
|
|
|
|
fileSpec.MakeUnique();
|
1998-12-19 02:06:54 +03:00
|
|
|
s << "File spec made unique: " << fileSpec << nsEndl;
|
1998-12-08 05:22:54 +03:00
|
|
|
} // WriteStuff
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
void main()
|
|
|
|
// For use with DEBUG defined.
|
|
|
|
//----------------------------------------------------------------------------------------
|
|
|
|
{
|
|
|
|
|
|
|
|
// Test of nsFileSpec
|
|
|
|
|
1998-12-19 02:06:54 +03:00
|
|
|
nsOutputFileStream nsOut(cout);
|
|
|
|
nsOut << "WRITING TEST OUTPUT TO cout" << nsEndl << nsEndl;
|
|
|
|
|
|
|
|
FileTest::WriteStuff(nsOut);
|
|
|
|
nsOut << nsEndl << nsEndl;
|
1998-12-08 05:22:54 +03:00
|
|
|
|
|
|
|
// Test of nsOutputFileStream
|
|
|
|
|
1998-12-22 23:06:56 +03:00
|
|
|
nsFilePath myTextFilePath("iotest.txt");
|
1998-12-09 01:45:42 +03:00
|
|
|
|
|
|
|
{
|
1998-12-19 02:06:54 +03:00
|
|
|
nsOut << "WRITING IDENTICAL OUTPUT TO " << (const char*)myTextFilePath << nsEndl << nsEndl;
|
1998-12-09 01:45:42 +03:00
|
|
|
nsOutputFileStream testStream(myTextFilePath);
|
1998-12-19 02:06:54 +03:00
|
|
|
NS_ASSERTION(testStream.is_open(), "File could not be opened");
|
1998-12-09 01:45:42 +03:00
|
|
|
FileTest::WriteStuff(testStream);
|
|
|
|
} // <-- Scope closes the stream (and the file).
|
1998-12-08 05:22:54 +03:00
|
|
|
|
|
|
|
// Test of nsInputFileStream
|
|
|
|
|
|
|
|
{
|
1998-12-19 02:06:54 +03:00
|
|
|
nsOut << "READING BACK DATA FROM " << (const char*)myTextFilePath << nsEndl << nsEndl;
|
1998-12-09 01:45:42 +03:00
|
|
|
nsInputFileStream testStream2(myTextFilePath);
|
1998-12-19 02:06:54 +03:00
|
|
|
NS_ASSERTION(testStream2.is_open(), "File could not be opened");
|
1998-12-09 01:45:42 +03:00
|
|
|
char line[1000];
|
1998-12-12 02:52:31 +03:00
|
|
|
|
1998-12-19 02:06:54 +03:00
|
|
|
testStream2.seek(0); // check that the seek compiles
|
1998-12-09 01:45:42 +03:00
|
|
|
while (!testStream2.eof())
|
|
|
|
{
|
1998-12-19 02:06:54 +03:00
|
|
|
testStream2.readline(line, sizeof(line));
|
|
|
|
nsOut << line << nsEndl;
|
1998-12-09 01:45:42 +03:00
|
|
|
}
|
1998-12-08 05:22:54 +03:00
|
|
|
}
|
|
|
|
} // main
|