pjs/xpcom/tests/FilesTest.cpp

92 строки
2.8 KiB
C++
Исходник Обычный вид История

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
NS_NAMESPACE FileTest
1998-12-08 05:22:54 +03:00
{
NS_NAMESPACE_PROTOTYPE void WriteStuff(nsOutputFileStream& s);
} NS_NAMESPACE_END
1998-12-08 05:22: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");
s << "File URL initialized to: \"" << fileURL << "\""<< nsEndl;
1998-12-08 05:22:54 +03:00
// Initialize a Unix path from a URL
nsFilePath filePath(fileURL);
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);
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();
s << "Unique file spec: " << fileSpec << nsEndl;
1998-12-08 05:22:54 +03:00
// Assign the spec to a URL
fileURL = fileSpec;
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";
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;
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();
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
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");
{
nsOut << "WRITING IDENTICAL OUTPUT TO " << (const char*)myTextFilePath << nsEndl << nsEndl;
nsOutputFileStream testStream(myTextFilePath);
NS_ASSERTION(testStream.is_open(), "File could not be opened");
FileTest::WriteStuff(testStream);
} // <-- Scope closes the stream (and the file).
1998-12-08 05:22:54 +03:00
// Test of nsInputFileStream
{
nsOut << "READING BACK DATA FROM " << (const char*)myTextFilePath << nsEndl << nsEndl;
nsInputFileStream testStream2(myTextFilePath);
NS_ASSERTION(testStream2.is_open(), "File could not be opened");
char line[1000];
1998-12-12 02:52:31 +03:00
testStream2.seek(0); // check that the seek compiles
while (!testStream2.eof())
{
testStream2.readline(line, sizeof(line));
nsOut << line << nsEndl;
}
1998-12-08 05:22:54 +03:00
}
} // main