mmsystem
The mmsystem module provides a set of procedures and functions related to the operating system. Note that the behavior of these operators may vary between systems. To use this module, the following line must be included in the header of the Mosel model file:
uses 'mmsystem'New functionality for the Mosel language
The type text
This module provides the type text for text manipulation, . Like the Mosel basic type string, this new type may be generated from all objects that can be converted to a text representation and supports the usual string operations (like concatenation or formatting). In addition, text objects can be altered (one can get and change a single as well as a sequence of characters in a text); offer a wider set of operations (like insertion/deletion/search of substrings) and, as all module types, are passed by reference to subroutines.
The type date
As the name suggests, the type date is used to represent a calendar date. Internally, a date is stored as three independent integers for representing the year (-32768 to 32767), the month (-128 to 127) and the day in the month (-128 to 127). The validity of a date can be checked using the function isvalid. A date object can be initialized by a text string, a single or three numerical values. In the first case, the conversion is processed using a predefined date format (see datefmt); in the second case, the integer is interpreted as the number of days elapsed since 1/1/1970; finally, if three integers are used, they are respectively interpreted as the year, month and day for the date. The constant SYS_NOW may also be used to initialize a date: date(SYS_NOW) is the current date. This type also supports assignment, comparison as well as difference (returned in number of days) and addition/subtraction of an integer (number of days).
The type time
The type time is used to represent a time during the day. Internally, a time object is stored as an integer representing a number of milliseconds. A time object can be initialized by a text string or one to four numerical values. In the first case, the conversion is processed using a predefined time format (see timefmt); in the second case, the integer is interpreted as a number of milliseconds. When two to four integers are used, they are understood as the hours, minutes, seconds and milliseconds. The constant SYS_NOW may also be used to initialize a time: time(SYS_NOW) is the current time. This type also supports assignment, comparison as well as difference (returned in number of milliseconds) and addition/subtraction of an integer (number of milliseconds).
The type datetime
The type datetime is used to represent a timestamp by combining a date and a time. A datetime object can be initialized by a text string, a pair date and time or a numerical value. In the first case, the conversion is processed using a predefined time format (see datetimefmt); in the third case, the number is interpreted as the number of seconds elapsed since 1/1/1970 at midnight. If the provided number is a real value, the fractional part is stored as a number of milliseconds. The constant SYS_NOW may also be used to initialize a datetime: datetime(SYS_NOW) is the current date and time. This type also supports assignment, comparison as well as difference (returned in number of seconds) and addition/subtraction of a numerical value (number of seconds).
Control parameters
Via the getparam function and the setparam procedure it is possible to access the following control parameters of module mmsystem (the reader is reminded that parameters may be spelled with lower or upper case letters or a mix of both):
datefmt Date text format.datetimefmt Date and time text format.sys_endparse End of parsing position.sys_fillchar Padding character for text resize.timefmt Time text format.Procedures and functions
In general, the procedures and functions of mmsystem do not fail but set a status variable that can be read with getsysstat. To make sure the operation has been performed correctly, check the value of this variable after each system call.
copytext Copy a part of a text or string.cuttext Cut a part of a text returning a copy of the deleted string.deltext Delete a part of a text.fcopy Copy a file.fdelete Delete a file.findtext Search for a string in a text or string.fmove Rename or move a file.getasnumber Convert a date, time or datetime into a number.getchar Get a character in a string or text.getcwd Get the current working directory.getdate Get the date part of a datetime.getday Get the day number of a date or datetime.getenv Get the value of an environment variable.getfsize Get the size of a file.getfstat Get the status of a file or directory.getftime Get time information of a file.gethour Get the hour part of a time or datetime.getminute Get the minute part of a time or datetime.getmonth Get the month number of a date or datetime.getmsec Get the millisecond part of a time or datetime.getsecond Get the second part of a time or datetime.getsize Get the size of a text.getsysstat Get the system status.gettime Get a time measure or the time part of a datetime.getweekday Compute the day of the week for a date or datetime.getyear Get the year part of a date or datetime.inserttext Paste a text or string into a text.isvalid Check whether a date, time or datetime is valid.makedir Create a new directory in the given file system.openpipe Start an external process for bidirectional communication.parseextn Initialise an object of a module type from a text.parseint Convert a text into an integer.parsereal Convert a text into a real.pastetext Paste a text or string into a text.qsort Sort a list or an array or (a subset of) the indices of an array.readtextline Read a line of text from the current input stream.removedir Remove a directory.setchar Set a character in a text.setdate Set the date part of a datetime.setday Set the day number of a date or datetime.setenv Set the value of an environment variable.sethour Set the hour part of a time or datetime.setminute Set the minute part of a time or datetime.setmonth Set the month number of a date or datetime.setmsec Set the millisecond part of a time or datetime.setsecond Set the second part of a time or datetime.settime Set the time part of a datetime.setyear Set the year part of a date or datetime.sleep Suspend execution for a fixed amount of time.system Execute an external program.textfmt Create a formatted text from a string, a text or a number.I/O drivers
The mmsystem module provides an IO driver to connect a Mosel input or output stream to a program started in a different process. Using this driver, it is possible to get the output of an external program (for instance the result of a preprocessor to feed the Mosel compiler) or implement a basic bidirectional inter process communication thanks to the openpipe procedure (which relies on this IO driver).
Driver pipe
pipe:program [options...]The file name for this driver is an external program with its options. Options are separated by spaces or tabulations and may be quoted using either single or double quotes. A quoted option may contain any kind of character except the quote used to delimit the string.
When the system opens a pipe, a new process is started for executing the given program and default input and output streams are directed to system pipes. If the file is open for reading (resp. writing), the default ouput stream (resp. input stream) of the new process becomes the current input stream (resp. output stream) of the model. To locate the program to be executed, the system relies on the PATH environment variable. Detection of error (typically the program cannot be found or is not executable) differs depending on the operating system: under Windows, the error is reported immediately and the pipe is not open. With Posix systems, no error is reported but following IO operations fail.
When the file is closed, both input and output streams of the external process are closed then the system waits for its termination: in order to avoid a lock up of the Mosel program one must make sure that the external program ends its execution when default input and output streams are closed.
Example: the following command could be used with Mosel Console for compiling the model mymod.mos after it has been processed by the C preprocessor. Note that we have to provide an output file name since the compiler cannot deduce it from the source file name.
For a Posix systems:
compile 'mmsystem.pipe:cpp mymod.mos' '' mymod.bimFor Windows (with MSVC):
compile 'mmsystem.pipe:cl /E mymod.mos' '' mymod.bimPublished library functions
The module mmsystem publishes its implementation of getenv, setenv and system as well as the functions gettxtsize, gettxtbuf and txtresize for text access via the service IMCI for use by other modules (see the Mosel Native Interface Reference Manual for more detail about services). The list of published functions is contained in the interface structure mmsystem_imci that is defined in the module header file mmsystem.h.
From another module, the context of mmsystem and its communication interface can be obtained using functions of the Mosel Native Interface as shown in the following example.
static XPRMnifct mm; XPRMcontext mmctx; XPRMdsolib dso; mmsystem_imci mmsys; void *sysctx; dso=mm->finddso("mmsystem"); /* Retrieve the mmsystem module*/ sysctx=*(mm->getdsoctx(mmctx, dso, (void **)(&mmsys))); /* Get the module context and the communication interface of mmsystem */Typically, a module calling functions that are provided by mmsystem will include this module into its list of dependencies in order to make sure that mmsystem will be loaded by Mosel at the same time as the calling module. The ``dependency'' service of the Mosel Native Interface has to be used to set the list of module dependencies:
static const char *deplist[]={"mmsystem",NULL}; /* Module dependency list */ static XPRMdsoserv tabserv[]= /* Table of services */ { {XPRM_SRV_DEPLST, (void *)deplist} };Using these functions a module may access and modify the environment of the calling model and execute an external program with automatic redirection of default streams:
mmsys->setenv(ctx,sysctx,"MYVAR","A_VALUE"); rts=mmsys->system(ctx,sysctx,"myprogram arg1 arg2");Description of the library functions
gettxtbuf Get a reference to the character buffer of a text object.gettxtsize Get the size of a text object.txtresize Resize a text object.
If you have any comments or suggestions about these pages, please send mail to docs@dashoptimization.com.