// Copyright 2000
// College of Computer Science
// Northeastern University Boston MA 02115

// This software may be used for educational purposes as long as this copyright
// notice is retained at the top of all files

// Should this software be modified, the words "Modified from Original" must be
// included as a comment below this notice

// All publication rights are retained.  This software or its documentation may
// not be published in any media either in whole or in part.

///////////////////////////////////////////////////////////////////////////////

// FileTool.h

///////////////////////////////////////////////////////////////////////////////

// January 2000: Preliminary Version for Windows

///////////////////////////////////////////////////////////////////////////////

#ifndef FILETOOL_H_
#define FILETOOL_H_

#include "FileTmpl.h"

// Select file names using a dialog box


// SelectOldFileName puts up file dialog to select an existing file name
//
// If user selects the name then full name including path is returned in
// FullName and function returns true
//
// If user cancels then function returns false

EXPORT bool SelectOldFileName(string& FullName);


// SelectNewFileName puts up file dialog to create a new file name
//
// If user creates the name then full name including path is returned in
// FullName and function returns true
//
// If user cancels then function returns false
//
// The parameter FileName is the default suggestion for the file name in
// the dialog box

EXPORT bool SelectNewFileName(string& FullName, const string& FileName = "");


// Open files

// See P. J. Plauger, The Draft Standard C++ Library, Prentice Hall, Englewood
// Cliffs, NJ, 1995, Chapters 6 and 13, especially, pp 120-128 and pp 320-325.


// Mode constants
//
// Do not use the "inout" modes without reading Plauger!  It is necessary to
// create both an istream and an ostream and then "link" them so they use the
// same internal buffer.  Plauger illustrates how this is supposed to be done.


const ios::openmode		textread		= ios::in;

const ios::openmode		textwrite		= ios::out | ios::trunc;

const ios::openmode		textappend		= ios::out | ios::app;

const ios::openmode		textinout		= ios::in  | ios::out;


const ios::openmode		binaryread		= ios::binary | ios::in;

const ios::openmode		binarywrite		= ios::binary | ios::out | ios::trunc;

const ios::openmode		binaryappend	= ios::binary | ios::out | ios::app;

const ios::openmode		binaryinout		= ios::binary | ios::in  | ios::out;


// OpenFile(In, Name, Mode)
// Open a file stream for input
// Mode must a "read" or "inout" mode

EXPORT bool OpenFile(
	ifstream& In,
	const char* Name,
	const ios::openmode Mode = textread);

EXPORT bool OpenFile(
	ifstream& In,
	const string& Name,
	const ios::openmode Mode = textread);


// OpenFile(Out, Name, Mode)
// Open a file stream for output
// Mode must be a "write" or "append" or "inout" mode

EXPORT bool OpenFile(
	ofstream& Out,
	const char* Name,
	const ios::openmode Mode = textwrite);

EXPORT bool OpenFile(
	ofstream& Out,
	const string& Name,
	const ios::openmode Mode = textwrite);


// Lookup size of an existing file
// FileSize requires that stream be open for input
// Will return zero size otherwise

EXPORT long FileSize(ifstream& In);


// ShowTextFile
//     displays a text file N lines at a time to console
//     default for N is 20
//
//     if N <= 0 then entire file will be displayed
//     if file is not open then function terminates
//
//     summary is written with #lines-printed and #total-lines-in-the-file

EXPORT void ShowTextFile(ifstream& In, int N = 20);

#endif // FILETOOL_H_