/* ==================================================================== */ /* SAMPLE_IO.C */ /* */ /* -------------------------------------------------------------------- */ /*************************************************************************** * * Copyright (c) 1989, 1990, 1991, 1992, 1993 * Rector and Visitors of the University of Virginia * All Rights Reserved * * This file is part of the Mentat system software library. This software * is intended for research and is available free of charge for that * purpose; however, all material is the proprietary source of the * University of Virginia, and may not be disclosed or copied in any * form without the explicit written permission of the Univeristy of * Virginia. * * This software is distributed WITHOUT ANY WARRANTY; without even the * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR * PURPOSE. This software hereunder is provided on an "as is" basis, * and the University of Virginia has no obligation to provide maintenance, * support, updates, enhancements, or modifications. * * Send problems & suggestions to: * mentat@Virginia.EDU * ***************************************************************************/ /*-------------------------------------------------------------------------- * * This program gives a simple example of how to use the mstream * library provided with Mentat. * *------------------------------------------------------------------------*/ #include // include for mentat stream library #include "appl.h" #ifdef AS_OBJECT #define wrap sample_io #include #endif #define NUM_INSTANCES 3 // streams are globally available now ... void foo () { int x; float y; char c; mcout << "Enter a number for x > " << flush; mcin >> x; mcout << "x = " << x << endl; mcin >> c; // Gobble up EOL mcout << "Enter a floating point number for y > " << flush; mcin >> y; mcout << "y = " << y << "\n" << flush; mcin >> c; // Gobble up EOL } //void bar(); #ifdef AS_OBJECT my_main(int argc,char **argv) #else main(int argc,char **argv) #endif { int ret, temp_ret[NUM_INSTANCES], i, x; float y; char c; APPL_CLASS* appl_class_inst; APPL_CLASS proto; // If the program desires to do IO, main must make this call // and the MENTAT_CLOSE_FS call at the end mfilesys mfs; MENTAT_OPEN_FS (mfs); // If a class (including main thread) desires to do IO // it must explicitly enable this capability MENTAT_ENABLE_IO (mfs); // gives us mcin, mcout, mcerr mcout << "F" << "\n" << flush; // Using standard streams is easy ... mcout << "Enter a number for x > " << flush; mcin >> x; mcout << "x = " << x << endl; mcin >> c; // Gobble up EOL mcout << "Enter a floating point number for y > " << flush; mcin >> y; mcout << "y = " << y << "\n" << flush; mcin >> c; // Gobble up EOL // Files have be attached to streams in the following way ... mfilebuf in_buf (mfs); mfilebuf out_buf (mfs); // Create file streams in_buf.open ("in_file", in); // mode is one of ios::in,out,app istream instr (&in_buf); out_buf.open ("out_file", out); ostream outstr (&out_buf); // Using file streams ... for (i=1; i <= 100; i++) { instr >> c; // or in_buf.get(c) outstr << c; // or in_buf.put(c) } outstr << flush; in_buf.close (); // close file streams ... out_buf.close (); // Any application classes (objects) that desires to do IO // must be passed the mfs object ... appl_class_inst = (APPL_CLASS*) mentat_vec_create (proto, NUM_INSTANCES); for (i=0; i < NUM_INSTANCES; i++) temp_ret[i] = appl_class_inst[i].do_work_and_io (mfs); for (i=0; i < NUM_INSTANCES; i++) if (temp_ret[i]==1); for (i=0; i < NUM_INSTANCES; i++) appl_class_inst[i].destroy (); // This thread diables IO MENTAT_DISABLE_IO (mfs); // The file system is shut down MENTAT_CLOSE_FS (mfs); }