doc/glpk06.tex
author Alpar Juttner <alpar@cs.elte.hu>
Mon, 06 Dec 2010 13:09:21 +0100
changeset 1 c445c931472f
permissions -rw-r--r--
Import glpk-4.45

- Generated files and doc/notes are removed
alpar@1
     1
%* glpk06.tex *%
alpar@1
     2
alpar@1
     3
\chapter{Miscellaneous API Routines}
alpar@1
     4
alpar@1
     5
\section{GLPK environment routines}
alpar@1
     6
alpar@1
     7
\subsection{glp\_long---64-bit integer data type}
alpar@1
     8
alpar@1
     9
Some GLPK API routines use 64-bit integer data type, which is declared
alpar@1
    10
in the header \verb|glpk.h| as follows:
alpar@1
    11
alpar@1
    12
\begin{verbatim}
alpar@1
    13
typedef struct { int lo, hi; } glp_long;
alpar@1
    14
\end{verbatim}
alpar@1
    15
alpar@1
    16
\noindent
alpar@1
    17
where \verb|lo| contains low 32 bits, and \verb|hi| contains high 32
alpar@1
    18
bits of 64-bit integer value.\footnote{GLPK conforms to ILP32, LLP64,
alpar@1
    19
and LP64 programming models, where the built-in type {\tt int}
alpar@1
    20
corresponds to 32-bit integers.}
alpar@1
    21
alpar@1
    22
\subsection{glp\_init\_env---initialize GLPK environment}
alpar@1
    23
alpar@1
    24
\subsubsection*{Synopsis}
alpar@1
    25
alpar@1
    26
\begin{verbatim}
alpar@1
    27
int glp_init_env(void);
alpar@1
    28
\end{verbatim}
alpar@1
    29
alpar@1
    30
\subsubsection*{Description}
alpar@1
    31
alpar@1
    32
The routine \verb|glp_init_env| initializes the GLPK environment.
alpar@1
    33
Normally the application program does not need to call this routine,
alpar@1
    34
because it is called automatically on the first call to any API routine.
alpar@1
    35
alpar@1
    36
\newpage
alpar@1
    37
alpar@1
    38
\subsubsection*{Returns}
alpar@1
    39
alpar@1
    40
The routine \verb|glp_init_env| returns one of the following codes:
alpar@1
    41
alpar@1
    42
\noindent
alpar@1
    43
0 --- initialization successful;
alpar@1
    44
alpar@1
    45
\noindent
alpar@1
    46
1 --- environment is already initialized;
alpar@1
    47
alpar@1
    48
\noindent
alpar@1
    49
2 --- initialization failed (insufficient memory);
alpar@1
    50
alpar@1
    51
\noindent
alpar@1
    52
3 --- initialization failed (unsupported programming model).
alpar@1
    53
alpar@1
    54
\subsection{glp\_version---determine library version}
alpar@1
    55
alpar@1
    56
\subsubsection*{Synopsis}
alpar@1
    57
alpar@1
    58
\begin{verbatim}
alpar@1
    59
const char *glp_version(void);
alpar@1
    60
\end{verbatim}
alpar@1
    61
alpar@1
    62
\subsubsection*{Returns}
alpar@1
    63
alpar@1
    64
The routine \verb|glp_version| returns a pointer to a null-terminated
alpar@1
    65
character string, which specifies the version of the GLPK library in
alpar@1
    66
the form \verb|"X.Y"|, where `\verb|X|' is the major version number, and
alpar@1
    67
`\verb|Y|' is the minor version number, for example, \verb|"4.16"|.
alpar@1
    68
alpar@1
    69
\subsubsection*{Example}
alpar@1
    70
alpar@1
    71
\begin{footnotesize}
alpar@1
    72
\begin{verbatim}
alpar@1
    73
printf("GLPK version is %s\n", glp_version());
alpar@1
    74
\end{verbatim}
alpar@1
    75
\end{footnotesize}
alpar@1
    76
alpar@1
    77
\subsection{glp\_free\_env---free GLPK environment}
alpar@1
    78
alpar@1
    79
\subsubsection*{Synopsis}
alpar@1
    80
alpar@1
    81
\begin{verbatim}
alpar@1
    82
int glp_free_env(void);
alpar@1
    83
\end{verbatim}
alpar@1
    84
alpar@1
    85
\subsubsection*{Description}
alpar@1
    86
alpar@1
    87
The routine \verb|glp_free_env| frees all resources used by GLPK
alpar@1
    88
routines (memory blocks, etc.) which are currently still in use.
alpar@1
    89
alpar@1
    90
Normally the application program does not need to call this routine,
alpar@1
    91
because GLPK routines always free all unused resources. However, if
alpar@1
    92
the application program even has deleted all problem objects, there
alpar@1
    93
will be several memory blocks still allocated for the internal library
alpar@1
    94
needs. For some reasons the application program may want GLPK to free
alpar@1
    95
this memory, in which case it should call \verb|glp_free_env|.
alpar@1
    96
alpar@1
    97
Note that a call to \verb|glp_free_env| invalidates all problem objects
alpar@1
    98
which still exist.
alpar@1
    99
alpar@1
   100
\subsubsection*{Returns}
alpar@1
   101
alpar@1
   102
The routine \verb|glp_free_env| returns one of the following codes:
alpar@1
   103
alpar@1
   104
\noindent
alpar@1
   105
0 --- termination successful;
alpar@1
   106
alpar@1
   107
\noindent
alpar@1
   108
1 --- environment is inactive (was not initialized).
alpar@1
   109
alpar@1
   110
\subsection{glp\_printf---write formatted output to terminal}
alpar@1
   111
alpar@1
   112
\subsubsection*{Synopsis}
alpar@1
   113
alpar@1
   114
\begin{verbatim}
alpar@1
   115
void glp_printf(const char *fmt, ...);
alpar@1
   116
\end{verbatim}
alpar@1
   117
alpar@1
   118
\subsubsection*{Description}
alpar@1
   119
alpar@1
   120
The routine \verb|glp_printf| uses the format control string
alpar@1
   121
\verb|fmt| to format its parameters and writes the formatted output to
alpar@1
   122
the terminal.
alpar@1
   123
alpar@1
   124
This routine is a replacement of the standard C function
alpar@1
   125
\verb|printf| and used by all GLPK routines to perform terminal
alpar@1
   126
output. The application program may use \verb|glp_printf| for the same
alpar@1
   127
purpose that allows controlling its terminal output with the routines
alpar@1
   128
\verb|glp_term_out| and \verb|glp_term_hook|.
alpar@1
   129
alpar@1
   130
\subsection{glp\_vprintf---write formatted output to terminal}
alpar@1
   131
alpar@1
   132
\subsubsection*{Synopsis}
alpar@1
   133
alpar@1
   134
\begin{verbatim}
alpar@1
   135
void glp_vprintf(const char *fmt, va_list arg);
alpar@1
   136
\end{verbatim}
alpar@1
   137
alpar@1
   138
\subsubsection*{Description}
alpar@1
   139
alpar@1
   140
The routine \verb|glp_vprintf| uses the format control string
alpar@1
   141
\verb|fmt| to format its parameters specified by the list \verb|arg|
alpar@1
   142
and writes the formatted output to the terminal.
alpar@1
   143
alpar@1
   144
This routine is a replacement of the standard C function
alpar@1
   145
\verb|vprintf| and used by all GLPK routines to perform terminal
alpar@1
   146
output. The application program may use \verb|glp_vprintf| for the same
alpar@1
   147
purpose that allows controlling its terminal output with the routines
alpar@1
   148
\verb|glp_term_out| and \verb|glp_term_hook|.
alpar@1
   149
alpar@1
   150
\newpage
alpar@1
   151
alpar@1
   152
\subsection{glp\_term\_out---enable/disable terminal output}
alpar@1
   153
alpar@1
   154
\subsubsection*{Synopsis}
alpar@1
   155
alpar@1
   156
\begin{verbatim}
alpar@1
   157
int glp_term_out(int flag);
alpar@1
   158
\end{verbatim}
alpar@1
   159
alpar@1
   160
\subsubsection*{Description}
alpar@1
   161
alpar@1
   162
Depending on the parameter flag the routine \verb|glp_term_out| enables
alpar@1
   163
or disables terminal output performed by glpk routines:
alpar@1
   164
alpar@1
   165
\verb|GLP_ON | --- enable terminal output;
alpar@1
   166
alpar@1
   167
\verb|GLP_OFF| --- disable terminal output.
alpar@1
   168
alpar@1
   169
\subsubsection*{Returns}
alpar@1
   170
alpar@1
   171
The routine \verb|glp_term_out| returns the previous value of the
alpar@1
   172
terminal output flag (\verb|GLP_ON| or \verb|GLP_OFF|).
alpar@1
   173
alpar@1
   174
\subsection{glp\_term\_hook---intercept terminal output}
alpar@1
   175
alpar@1
   176
\subsubsection*{Synopsis}
alpar@1
   177
alpar@1
   178
\begin{verbatim}
alpar@1
   179
void glp_term_hook(int (*func)(void *info, const char *s),
alpar@1
   180
      void *info);
alpar@1
   181
\end{verbatim}
alpar@1
   182
alpar@1
   183
\subsubsection*{Description}
alpar@1
   184
alpar@1
   185
The routine \verb|glp_term_hook| installs the user-defined hook routine
alpar@1
   186
to intercept all terminal output performed by GLPK routines.
alpar@1
   187
alpar@1
   188
%This feature can be used to redirect the terminal output to other
alpar@1
   189
%destination, for example, to a file or a text window.
alpar@1
   190
alpar@1
   191
The parameter {\it func} specifies the user-defined hook routine. It is
alpar@1
   192
called from an internal printing routine, which passes to it two
alpar@1
   193
parameters: {\it info} and {\it s}. The parameter {\it info} is a
alpar@1
   194
transit pointer specified in corresponding call to the routine
alpar@1
   195
\verb|glp_term_hook|; it may be used to pass some additional information
alpar@1
   196
to the hook routine. The parameter {\it s} is a pointer to the null
alpar@1
   197
terminated character string, which is intended to be written to the
alpar@1
   198
terminal. If the hook routine returns zero, the printing routine writes
alpar@1
   199
the string {\it s} to the terminal in a usual way; otherwise, if the
alpar@1
   200
hook routine returns non-zero, no terminal output is performed.
alpar@1
   201
alpar@1
   202
To uninstall the hook routine both parameters {\it func} and {\it info}
alpar@1
   203
should be specified as \verb|NULL|.
alpar@1
   204
alpar@1
   205
\newpage
alpar@1
   206
alpar@1
   207
\subsubsection*{Example}
alpar@1
   208
alpar@1
   209
\begin{footnotesize}
alpar@1
   210
\begin{verbatim}
alpar@1
   211
static int hook(void *info, const char *s)
alpar@1
   212
{     FILE *foo = info;
alpar@1
   213
      fputs(s, foo);
alpar@1
   214
      return 1;
alpar@1
   215
}
alpar@1
   216
alpar@1
   217
int main(void)
alpar@1
   218
{     FILE *foo;
alpar@1
   219
      . . .
alpar@1
   220
      /* redirect terminal output */
alpar@1
   221
      glp_term_hook(hook, foo);
alpar@1
   222
      . . .
alpar@1
   223
      /* resume terminal output */
alpar@1
   224
      glp_term_hook(NULL, NULL);
alpar@1
   225
      . . .
alpar@1
   226
}
alpar@1
   227
\end{verbatim}
alpar@1
   228
\end{footnotesize}
alpar@1
   229
alpar@1
   230
\subsection{glp\_open\_tee---start copying terminal output}
alpar@1
   231
alpar@1
   232
\subsubsection*{Synopsis}
alpar@1
   233
alpar@1
   234
\begin{verbatim}
alpar@1
   235
int glp_open_tee(const char *fname);
alpar@1
   236
\end{verbatim}
alpar@1
   237
alpar@1
   238
\subsubsection*{Description}
alpar@1
   239
alpar@1
   240
The routine \verb|glp_open_tee| starts copying all the terminal output
alpar@1
   241
to an output text file, whose name is specified by the character string
alpar@1
   242
\verb|fname|.
alpar@1
   243
alpar@1
   244
\subsubsection*{Returns}
alpar@1
   245
alpar@1
   246
The routine \verb|glp_open_tee| returns one of the following codes:
alpar@1
   247
alpar@1
   248
\noindent
alpar@1
   249
0 --- operation successful;
alpar@1
   250
alpar@1
   251
\noindent
alpar@1
   252
1 --- copying terminal output is already active;
alpar@1
   253
alpar@1
   254
\noindent
alpar@1
   255
2 --- unable to create output file.
alpar@1
   256
alpar@1
   257
\newpage
alpar@1
   258
alpar@1
   259
\subsection{glp\_close\_tee---stop copying terminal output}
alpar@1
   260
alpar@1
   261
\subsubsection*{Synopsis}
alpar@1
   262
alpar@1
   263
\begin{verbatim}
alpar@1
   264
int glp_close_tee(void);
alpar@1
   265
\end{verbatim}
alpar@1
   266
alpar@1
   267
\subsubsection*{Description}
alpar@1
   268
alpar@1
   269
The routine \verb|glp_close_tee| stops copying the terminal output to
alpar@1
   270
the output text file previously open by the routine \verb|glp_open_tee|
alpar@1
   271
closing that file.
alpar@1
   272
alpar@1
   273
\subsubsection*{Returns}
alpar@1
   274
alpar@1
   275
The routine \verb|glp_close_tee| returns one of the following codes:
alpar@1
   276
alpar@1
   277
\noindent
alpar@1
   278
0 --- operation successful;
alpar@1
   279
alpar@1
   280
\noindent
alpar@1
   281
1 --- copying terminal output was not started.
alpar@1
   282
alpar@1
   283
\subsection{glp\_error---display error message and terminate execution}
alpar@1
   284
alpar@1
   285
\subsubsection*{Synopsis}
alpar@1
   286
alpar@1
   287
\begin{verbatim}
alpar@1
   288
void glp_error(const char *fmt, ...);
alpar@1
   289
\end{verbatim}
alpar@1
   290
alpar@1
   291
\subsubsection*{Description}
alpar@1
   292
alpar@1
   293
The routine \verb|glp_error| (implemented as a macro) formats its
alpar@1
   294
parameters using the format control string \verb|fmt|, writes the
alpar@1
   295
formatted message to the terminal, and then abnormally terminates the
alpar@1
   296
program.
alpar@1
   297
alpar@1
   298
\subsection{glp\_assert---check logical condition}
alpar@1
   299
alpar@1
   300
\subsubsection*{Synopsis}
alpar@1
   301
alpar@1
   302
\begin{verbatim}
alpar@1
   303
void glp_assert(int expr);
alpar@1
   304
\end{verbatim}
alpar@1
   305
alpar@1
   306
\subsubsection*{Description}
alpar@1
   307
alpar@1
   308
The routine \verb|glp_assert| (implemented as a macro) checks
alpar@1
   309
a logical condition specified by the expression \verb|expr|. If the
alpar@1
   310
condition is true (non-zero), the routine does nothing; otherwise, if
alpar@1
   311
the condition is false (zero), the routine prints an error message and
alpar@1
   312
abnormally terminates the program.
alpar@1
   313
alpar@1
   314
This routine is a replacement of the standard C function \verb|assert|
alpar@1
   315
and used by all GLPK routines to check program logic. The application
alpar@1
   316
program may use \verb|glp_assert| for the same purpose.
alpar@1
   317
alpar@1
   318
\subsection{glp\_error\_hook---install hook to intercept abnormal
alpar@1
   319
termination}
alpar@1
   320
alpar@1
   321
\subsubsection*{Synopsis}
alpar@1
   322
alpar@1
   323
\begin{verbatim}
alpar@1
   324
void glp_error_hook(void (*func)(void *info), void *info);
alpar@1
   325
\end{verbatim}
alpar@1
   326
alpar@1
   327
\subsubsection*{Description}
alpar@1
   328
alpar@1
   329
The routine \verb|glp_error_hook| installs a user-defined hook routine
alpar@1
   330
to intercept abnormal termination.
alpar@1
   331
alpar@1
   332
The parameter \verb|func| specifies the user-defined hook routine. It
alpar@1
   333
is called from the routine \verb|glp_error| before the latter calls the
alpar@1
   334
abort function to abnormally terminate the application program because
alpar@1
   335
of fatal error. The parameter \verb|info| is a transit pointer,
alpar@1
   336
specified in the corresponding call to the routine
alpar@1
   337
\verb|glp_error_hook|; it may be used to pass some information to the
alpar@1
   338
hook routine.
alpar@1
   339
alpar@1
   340
To uninstall the hook routine the parameters \verb|func| and \verb|info|
alpar@1
   341
should be specified as \verb|NULL|.
alpar@1
   342
alpar@1
   343
\subsubsection*{Usage note}
alpar@1
   344
alpar@1
   345
If the hook routine returns, the application program is abnormally
alpar@1
   346
terminated. To prevent abnormal termnation the hook routine may perform
alpar@1
   347
a global jump using the standard function \verb|longjmp|, in which case
alpar@1
   348
the application program {\it must} call the routine \verb|glp_free_env|.
alpar@1
   349
alpar@1
   350
\subsection{glp\_malloc---allocate memory block}
alpar@1
   351
alpar@1
   352
\subsubsection*{Synopsis}
alpar@1
   353
alpar@1
   354
\begin{verbatim}
alpar@1
   355
void *glp_malloc(int size);
alpar@1
   356
\end{verbatim}
alpar@1
   357
alpar@1
   358
\subsubsection*{Description}
alpar@1
   359
alpar@1
   360
The routine \verb|glp_malloc| dynamically allocates a memory block of
alpar@1
   361
\verb|size| bytes long. Should note that:
alpar@1
   362
alpar@1
   363
1) the parameter \verb|size| must be positive;
alpar@1
   364
alpar@1
   365
2) being allocated the memory block contains arbitrary data, that is,
alpar@1
   366
it is {\it not} initialized by binary zeros;
alpar@1
   367
alpar@1
   368
3) if the block cannot be allocated due to insufficient memory, the
alpar@1
   369
routine prints an error message and abnormally terminates the program.
alpar@1
   370
alpar@1
   371
This routine is a replacement of the standard C function \verb|malloc|
alpar@1
   372
and used by all GLPK routines for dynamic memory allocation. The
alpar@1
   373
application program may use \verb|glp_malloc| for the same purpose.
alpar@1
   374
alpar@1
   375
\subsubsection*{Returns}
alpar@1
   376
alpar@1
   377
The routine \verb|glp_malloc| returns a pointer to the memory block
alpar@1
   378
allocated. To free this block the routine \verb|glp_free| (not the
alpar@1
   379
standard C function \verb|free|!) must be used.
alpar@1
   380
alpar@1
   381
\subsection{glp\_calloc---allocate memory block}
alpar@1
   382
alpar@1
   383
\subsubsection*{Synopsis}
alpar@1
   384
alpar@1
   385
\begin{verbatim}
alpar@1
   386
void *glp_calloc(int n, int size);
alpar@1
   387
\end{verbatim}
alpar@1
   388
alpar@1
   389
\subsubsection*{Description}
alpar@1
   390
alpar@1
   391
The routine \verb|glp_calloc| dynamically allocates a memory block of
alpar@1
   392
\verb|n|$\times$\verb|size| bytes long. Should note that:
alpar@1
   393
alpar@1
   394
1) both parameters \verb|n| and \verb|size| must be positive;
alpar@1
   395
alpar@1
   396
2) being allocated the memory block contains arbitrary data, that is,
alpar@1
   397
it is {\it not} initialized by binary zeros;
alpar@1
   398
alpar@1
   399
3) if the block cannot be allocated due to insufficient memory, the
alpar@1
   400
routine prints an error message and abnormally terminates the program.
alpar@1
   401
alpar@1
   402
This routine is a replacement of the standard C function \verb|calloc|
alpar@1
   403
(with exception that the block is not cleaned) and used by all GLPK
alpar@1
   404
routines for dynamic memory allocation. The application program may use
alpar@1
   405
\verb|glp_calloc| for the same purpose.
alpar@1
   406
alpar@1
   407
\subsubsection*{Returns}
alpar@1
   408
alpar@1
   409
The routine \verb|glp_calloc| returns a pointer to the memory block
alpar@1
   410
allocated. To free this block the routine \verb|glp_free| (not the
alpar@1
   411
standard C function \verb|free|!) must be used.
alpar@1
   412
alpar@1
   413
\subsection{glp\_free---free memory block}
alpar@1
   414
alpar@1
   415
\subsubsection*{Synopsis}
alpar@1
   416
alpar@1
   417
\begin{verbatim}
alpar@1
   418
void glp_free(void *ptr);
alpar@1
   419
\end{verbatim}
alpar@1
   420
alpar@1
   421
\subsubsection*{Description}
alpar@1
   422
alpar@1
   423
The routine \verb|glp_free| frees (deallocates) a memory block pointed
alpar@1
   424
to by \verb|ptr|, which was previously allocated by the routine
alpar@1
   425
\verb|glp_malloc| or \verb|glp_calloc|. Note that the pointer \verb|ptr|
alpar@1
   426
must valid and must not be \verb|NULL|.
alpar@1
   427
alpar@1
   428
This routine is a replacement of the standard C function \verb|free|
alpar@1
   429
and used by all GLPK routines for dynamic memory allocation. The
alpar@1
   430
application program may use \verb|glp_free| for the same purpose.
alpar@1
   431
alpar@1
   432
\subsection{glp\_mem\_usage---get memory usage information}
alpar@1
   433
alpar@1
   434
\subsubsection*{Synopsis}
alpar@1
   435
alpar@1
   436
\begin{verbatim}
alpar@1
   437
void glp_mem_usage(int *count, int *cpeak, glp_long *total,
alpar@1
   438
      glp_long *tpeak);
alpar@1
   439
\end{verbatim}
alpar@1
   440
alpar@1
   441
\subsubsection*{Description}
alpar@1
   442
alpar@1
   443
The routine \verb|glp_mem_usage| reports some information about
alpar@1
   444
utilization of the memory by the routines \verb|glp_malloc|,
alpar@1
   445
\verb|glp_calloc|, and \verb|glp_free|. Information is stored to
alpar@1
   446
locations specified by corresponding parameters (see below). Any
alpar@1
   447
parameter can be specified as \verb|NULL|, in which case corresponding
alpar@1
   448
information is not stored.
alpar@1
   449
alpar@1
   450
\verb|*count| is the number of currently allocated memory blocks.
alpar@1
   451
alpar@1
   452
\verb|*cpeak| is the peak value of \verb|*count| reached since the
alpar@1
   453
initialization of the GLPK library environment.
alpar@1
   454
alpar@1
   455
\verb|*total| is the total amount, in bytes, of currently allocated
alpar@1
   456
memory blocks.
alpar@1
   457
alpar@1
   458
\verb|*tpeak| is the peak value of \verb|*total| reached since the
alpar@1
   459
initialization of the GLPK library envirionment.
alpar@1
   460
alpar@1
   461
\subsubsection*{Example}
alpar@1
   462
alpar@1
   463
\begin{footnotesize}
alpar@1
   464
\begin{verbatim}
alpar@1
   465
glp_mem_usage(&count, NULL, NULL, NULL);
alpar@1
   466
printf("%d memory block(s) are still allocated\n", count);
alpar@1
   467
\end{verbatim}
alpar@1
   468
\end{footnotesize}
alpar@1
   469
alpar@1
   470
\subsection{glp\_mem\_limit---set memory usage limit}
alpar@1
   471
alpar@1
   472
\subsubsection*{Synopsis}
alpar@1
   473
alpar@1
   474
\begin{verbatim}
alpar@1
   475
void glp_mem_limit(int limit);
alpar@1
   476
\end{verbatim}
alpar@1
   477
alpar@1
   478
\subsubsection*{Description}
alpar@1
   479
alpar@1
   480
The routine \verb|glp_mem_limit| limits the amount of memory available
alpar@1
   481
for dynamic allocation (with the routines \verb|glp_malloc| and
alpar@1
   482
\verb|glp_calloc|) to \verb|limit| megabytes.
alpar@1
   483
alpar@1
   484
\subsection{glp\_time---determine current universal time}
alpar@1
   485
alpar@1
   486
\subsubsection*{Synopsis}
alpar@1
   487
alpar@1
   488
\begin{verbatim}
alpar@1
   489
glp_long glp_time(void);
alpar@1
   490
\end{verbatim}
alpar@1
   491
alpar@1
   492
\subsection*{Returns}
alpar@1
   493
alpar@1
   494
The routine \verb|glp_time| returns the current universal time (UTC),
alpar@1
   495
in milliseconds, elapsed since 00:00:00 GMT January 1, 1970.
alpar@1
   496
alpar@1
   497
\subsection{glp\_difftime---compute difference between two time values}
alpar@1
   498
alpar@1
   499
\subsubsection*{Synopsis}
alpar@1
   500
alpar@1
   501
\begin{verbatim}
alpar@1
   502
double glp_difftime(glp_long t1, glp_long t0);
alpar@1
   503
\end{verbatim}
alpar@1
   504
alpar@1
   505
\subsection*{Returns}
alpar@1
   506
alpar@1
   507
The routine \verb|glp_difftime| returns the difference between two time
alpar@1
   508
values \verb|t1| and \verb|t0|, expressed in seconds.
alpar@1
   509
alpar@1
   510
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
alpar@1
   511
alpar@1
   512
\newpage
alpar@1
   513
alpar@1
   514
\section{Plain data file reading routines}
alpar@1
   515
alpar@1
   516
\subsection{Introduction}
alpar@1
   517
alpar@1
   518
On developing simple applications to solve optimization problems it is
alpar@1
   519
often needed to read data from plain text files. To do this the standard
alpar@1
   520
C function \verb|fscanf| may be used, however, it is not convenient; for
alpar@1
   521
example, if it scans an integer number according to the format
alpar@1
   522
specification `\verb|%d|', and that number is coded incorrectly,
alpar@1
   523
no diagnostics is provided.
alpar@1
   524
alpar@1
   525
This section describes a set of GLPK API routines, which may be used in
alpar@1
   526
application programs to simplify reading data from plain text files.
alpar@1
   527
alpar@1
   528
\subsubsection*{Example 1}
alpar@1
   529
alpar@1
   530
The following main program reads ten integer numbers from plain text
alpar@1
   531
file \verb|data.txt| and prints their sum.
alpar@1
   532
alpar@1
   533
\begin{footnotesize}
alpar@1
   534
\begin{verbatim}
alpar@1
   535
/* sdfsamp1.c */
alpar@1
   536
alpar@1
   537
#include <stdio.h>
alpar@1
   538
#include <stdlib.h>
alpar@1
   539
#include <glpk.h>
alpar@1
   540
alpar@1
   541
int main(void)
alpar@1
   542
{     glp_data *data;
alpar@1
   543
      int j, num, sum;
alpar@1
   544
      /* open plain data file */
alpar@1
   545
      data = glp_sdf_open_file("data.txt");
alpar@1
   546
      if (data == NULL) exit(EXIT_FAILURE);
alpar@1
   547
      sum = 0;
alpar@1
   548
      for (j = 1; j <= 10; j++)
alpar@1
   549
      {  /* read next integer number */
alpar@1
   550
         num = glp_sdf_read_int(data);
alpar@1
   551
         sum += num;
alpar@1
   552
      }
alpar@1
   553
      printf("sum = %d\n", sum);
alpar@1
   554
      /* close plain data file */
alpar@1
   555
      glp_sdf_close_file(data);
alpar@1
   556
      return 0;
alpar@1
   557
}
alpar@1
   558
alpar@1
   559
/* eof */
alpar@1
   560
\end{verbatim}
alpar@1
   561
\end{footnotesize}
alpar@1
   562
alpar@1
   563
The input data are coded in free format. For example, the file
alpar@1
   564
\verb|data.txt| may look like this:
alpar@1
   565
alpar@1
   566
\begin{footnotesize}
alpar@1
   567
\begin{verbatim}
alpar@1
   568
123 65 432 890 -12 743 895 -7 111 326
alpar@1
   569
\end{verbatim}
alpar@1
   570
\end{footnotesize}
alpar@1
   571
alpar@1
   572
\noindent
alpar@1
   573
or like this:
alpar@1
   574
alpar@1
   575
\begin{footnotesize}
alpar@1
   576
\begin{verbatim}
alpar@1
   577
123   65  432  890  -12
alpar@1
   578
743  895   -7  111  326
alpar@1
   579
\end{verbatim}
alpar@1
   580
\end{footnotesize}
alpar@1
   581
alpar@1
   582
\noindent
alpar@1
   583
If the input data file contains incorrect data, the routine
alpar@1
   584
\verb|glp_sdf_read_int| prints an error message and, if no error
alpar@1
   585
handling is provided by the application program, abnormally terminates
alpar@1
   586
program execution. For example, the file \verb|data.txt| could contain
alpar@1
   587
the following data:
alpar@1
   588
alpar@1
   589
\begin{footnotesize}
alpar@1
   590
\begin{verbatim}
alpar@1
   591
123   65  432  890  -12
alpar@1
   592
743  895   =7  111  326
alpar@1
   593
\end{verbatim}
alpar@1
   594
\end{footnotesize}
alpar@1
   595
alpar@1
   596
\noindent
alpar@1
   597
in which case the error message would be the following:
alpar@1
   598
alpar@1
   599
\begin{footnotesize}
alpar@1
   600
\begin{verbatim}
alpar@1
   601
data.txt:2: cannot convert `=7' to integer
alpar@1
   602
\end{verbatim}
alpar@1
   603
\end{footnotesize}
alpar@1
   604
alpar@1
   605
\subsubsection*{Example 2}
alpar@1
   606
alpar@1
   607
As it was said above, by default any attempt to read incorrect data
alpar@1
   608
leads to abnormal termination. However, sometimes it is desirable to
alpar@1
   609
catch such errors. This feature is illustrated by the following main
alpar@1
   610
program, which does the same job as in the previous example.
alpar@1
   611
alpar@1
   612
\begin{footnotesize}
alpar@1
   613
\begin{verbatim}
alpar@1
   614
/* sdfsamp2.c */
alpar@1
   615
alpar@1
   616
#include <setjmp.h>
alpar@1
   617
#include <stdio.h>
alpar@1
   618
#include <stdlib.h>
alpar@1
   619
#include <glpk.h>
alpar@1
   620
alpar@1
   621
int main(void)
alpar@1
   622
{     glp_data *data;
alpar@1
   623
      jmp_buf jump;
alpar@1
   624
      int j, num, sum, ret;
alpar@1
   625
      /* open plain data file */
alpar@1
   626
      data = glp_sdf_open_file("data.txt");
alpar@1
   627
      if (data == NULL)
alpar@1
   628
      {  ret = EXIT_FAILURE;
alpar@1
   629
         goto done;
alpar@1
   630
      }
alpar@1
   631
      /* set up error handling */
alpar@1
   632
      if (setjmp(jump))
alpar@1
   633
      {  ret = EXIT_FAILURE;
alpar@1
   634
         goto done;
alpar@1
   635
      }
alpar@1
   636
      glp_sdf_set_jump(data, jump);
alpar@1
   637
      /* read and process data */
alpar@1
   638
      sum = 0;
alpar@1
   639
      for (j = 1; j <= 10; j++)
alpar@1
   640
      {  /* read next integer number */
alpar@1
   641
         num = glp_sdf_read_int(data);
alpar@1
   642
         if (abs(num) > 1000)
alpar@1
   643
            glp_sdf_error(data, "integer %d too big\n", num);
alpar@1
   644
         if (num < 0)
alpar@1
   645
            glp_sdf_warning(data, "integer %d is negative\n", num);
alpar@1
   646
         sum += num;
alpar@1
   647
      }
alpar@1
   648
      printf("sum = %d\n", sum);
alpar@1
   649
      ret = EXIT_SUCCESS;
alpar@1
   650
done: /* close plain data file */
alpar@1
   651
      if (data != NULL) glp_sdf_close_file(data);
alpar@1
   652
      return ret;
alpar@1
   653
}
alpar@1
   654
alpar@1
   655
/* eof */
alpar@1
   656
\end{verbatim}
alpar@1
   657
\end{footnotesize}
alpar@1
   658
alpar@1
   659
\subsection{glp\_sdf\_open\_file---open plain data file}
alpar@1
   660
alpar@1
   661
\subsubsection*{Synopsis}
alpar@1
   662
alpar@1
   663
\begin{verbatim}
alpar@1
   664
glp_data *glp_sdf_open_file(const char *fname);
alpar@1
   665
\end{verbatim}
alpar@1
   666
alpar@1
   667
\subsubsection*{Description}
alpar@1
   668
alpar@1
   669
The routine \verb|glp_sdf_open_file| opens a plain data file, whose
alpar@1
   670
name is specified by the character string \verb|fname|.
alpar@1
   671
alpar@1
   672
\subsubsection*{Returns}
alpar@1
   673
alpar@1
   674
If the operation was successful, the routine \verb|glp_sdf_open_file|
alpar@1
   675
returns a pointer to the opaque program object of the type
alpar@1
   676
\verb|glp_data|\footnote{This data structure is declared in the header
alpar@1
   677
file {\tt glpk.h}.} associated with the plain data file. Otherwise, if
alpar@1
   678
the operation failed, the routine prints an error message and returns
alpar@1
   679
\verb|NULL|.
alpar@1
   680
alpar@1
   681
\subsubsection*{Note}
alpar@1
   682
alpar@1
   683
The application program should use the pointer returned by the routine
alpar@1
   684
\verb|glp_sdf_open_file| to perform all subsequent operations on the
alpar@1
   685
data file.
alpar@1
   686
alpar@1
   687
\newpage
alpar@1
   688
alpar@1
   689
\subsection{glp\_sdf\_set\_jump---set up error handling}
alpar@1
   690
alpar@1
   691
\subsubsection*{Synopsis}
alpar@1
   692
alpar@1
   693
\begin{verbatim}
alpar@1
   694
void glp_sdf_set_jump(glp_data *data, jmp_buf jump);
alpar@1
   695
\end{verbatim}
alpar@1
   696
alpar@1
   697
\subsubsection*{Description}
alpar@1
   698
alpar@1
   699
The routine \verb|glp_sdf_set_jump| sets up error handling for the
alpar@1
   700
plain data file specified by the parameter \verb|data|.
alpar@1
   701
alpar@1
   702
The parameter \verb|jump| specifies the environment buffer, which must
alpar@1
   703
be initialized with the standard C function \verb|setjmp| prior to call
alpar@1
   704
to the routine \verb|glp_sdf_set_jump|. Detecting any incorrect data in
alpar@1
   705
the corresponding plain data file will cause non-local ``go to'' by
alpar@1
   706
a call to the standard C function \verb|longjmp|.
alpar@1
   707
alpar@1
   708
The parameter \verb|jump| can be specified as \verb|NULL|, in which
alpar@1
   709
case the routine \verb|glp_sdf_set_jump| restores the default behavior,
alpar@1
   710
in which case detecting incorrect data leads to abnormal termination.
alpar@1
   711
alpar@1
   712
\subsection{glp\_sdf\_error---print error message}
alpar@1
   713
alpar@1
   714
\subsubsection*{Synopsis}
alpar@1
   715
alpar@1
   716
\begin{verbatim}
alpar@1
   717
void glp_sdf_error(glp_data *data, const char *fmt, ...);
alpar@1
   718
\end{verbatim}
alpar@1
   719
alpar@1
   720
\subsubsection*{Description}
alpar@1
   721
alpar@1
   722
The routine \verb|glp_sdf_error| prints an error message related to the
alpar@1
   723
plain data file specified by the parameter \verb|data|. If error handing
alpar@1
   724
was not previously provided, the routine then abnormally terminates
alpar@1
   725
execution of the application program. Otherwise, it signals about the
alpar@1
   726
error by a call to the standard C function \verb|longjmp|.
alpar@1
   727
alpar@1
   728
The character string \verb|fmt| and optional parameters following it
alpar@1
   729
have the same meaning as for the standard C function \verb|printf|.
alpar@1
   730
alpar@1
   731
The message produced by the routine \verb|glp_sdf_error| looks like
alpar@1
   732
follows:
alpar@1
   733
alpar@1
   734
\medskip
alpar@1
   735
alpar@1
   736
{\it file}{\tt :}{\it line}{\tt :} {\it message text}
alpar@1
   737
alpar@1
   738
\medskip
alpar@1
   739
alpar@1
   740
\noindent
alpar@1
   741
where {\it file} is the filename passed to the routine
alpar@1
   742
\verb|glp_sdf_open| and {\it line} is the current line number.
alpar@1
   743
alpar@1
   744
\newpage
alpar@1
   745
alpar@1
   746
\subsection{glp\_sdf\_warning---print warning message}
alpar@1
   747
alpar@1
   748
\subsubsection*{Synopsis}
alpar@1
   749
alpar@1
   750
\begin{verbatim}
alpar@1
   751
void glp_sdf_warning(glp_data *data, const char *fmt, ...);
alpar@1
   752
\end{verbatim}
alpar@1
   753
alpar@1
   754
\subsubsection*{Description}
alpar@1
   755
alpar@1
   756
The routine \verb|glp_sdf_warning| prints a warning message related to
alpar@1
   757
the plain data file specified by the parameter \verb|data|.
alpar@1
   758
alpar@1
   759
The character string \verb|fmt| and optional parameters following it
alpar@1
   760
have the same meaning as for the standard C function \verb|printf|.
alpar@1
   761
alpar@1
   762
The message produced by the routine \verb|glp_sdf_warning| looks like
alpar@1
   763
follows:
alpar@1
   764
alpar@1
   765
\medskip
alpar@1
   766
alpar@1
   767
{\it file}{\tt :}{\it line}\verb|: warning:| {\it message text}
alpar@1
   768
alpar@1
   769
\medskip
alpar@1
   770
alpar@1
   771
\noindent
alpar@1
   772
where {\it file} is the filename passed to the routine
alpar@1
   773
\verb|glp_sdf_open| and {\it line} is the current line number.
alpar@1
   774
alpar@1
   775
\subsection{glp\_sdf\_read\_int---read integer number}
alpar@1
   776
alpar@1
   777
\subsubsection*{Synopsis}
alpar@1
   778
alpar@1
   779
\begin{verbatim}
alpar@1
   780
int glp_sdf_read_int(glp_data *data);
alpar@1
   781
\end{verbatim}
alpar@1
   782
alpar@1
   783
\subsubsection*{Description}
alpar@1
   784
alpar@1
   785
The routine \verb|glp_sdf_read_int| skips optional white-space
alpar@1
   786
characters and then reads an integer number from the plain data file
alpar@1
   787
specified by the parameter \verb|data|. If the operation failed, the
alpar@1
   788
routine \verb|glp_sdf_read_int| calls the routine \verb|glp_sdf_error|
alpar@1
   789
(see above).
alpar@1
   790
alpar@1
   791
\subsubsection*{Returns}
alpar@1
   792
alpar@1
   793
The routine \verb|glp_sdf_read_int| returns the integer number read.
alpar@1
   794
alpar@1
   795
\newpage
alpar@1
   796
alpar@1
   797
\subsection{glp\_sdf\_read\_num---read floating-point number}
alpar@1
   798
alpar@1
   799
\subsubsection*{Synopsis}
alpar@1
   800
alpar@1
   801
\begin{verbatim}
alpar@1
   802
double glp_sdf_read_num(glp_data *data);
alpar@1
   803
\end{verbatim}
alpar@1
   804
alpar@1
   805
\subsubsection*{Description}
alpar@1
   806
alpar@1
   807
The routine \verb|glp_sdf_read_num| skips optional white-space
alpar@1
   808
characters and then reads a floating-point number from the plain data
alpar@1
   809
file specified by the parameter \verb|data|. If the operation failed,
alpar@1
   810
the routine \verb|glp_sdf_num| calls the routine \verb|glp_sdf_error|
alpar@1
   811
(see above).
alpar@1
   812
alpar@1
   813
\subsubsection*{Returns}
alpar@1
   814
alpar@1
   815
The routine \verb|glp_sdf_read_num| returns the floating-point number
alpar@1
   816
read.
alpar@1
   817
alpar@1
   818
\subsection{glp\_sdf\_read\_item---read data item}
alpar@1
   819
alpar@1
   820
\subsubsection*{Synopsis}
alpar@1
   821
alpar@1
   822
\begin{verbatim}
alpar@1
   823
const char *glp_sdf_read_item(glp_data *data);
alpar@1
   824
\end{verbatim}
alpar@1
   825
alpar@1
   826
\subsubsection*{Description}
alpar@1
   827
alpar@1
   828
The routine \verb|glp_sdf_read_item| skips optional white-space
alpar@1
   829
characters and then reads a data item from the plain data file specified
alpar@1
   830
by the parameter \verb|data|. If the operation failed, the routine
alpar@1
   831
\verb|glp_sdf_read_item| calls the routine \verb|glp_sdf_error| (see
alpar@1
   832
above).
alpar@1
   833
alpar@1
   834
{\it Data item} is a sequence of 1 to 255 arbitrary graphic characters
alpar@1
   835
delimited by white-space characters. Data items may be used to represent
alpar@1
   836
symbolic names, identifiers, etc.
alpar@1
   837
alpar@1
   838
\subsubsection*{Returns}
alpar@1
   839
alpar@1
   840
The routine \verb|glp_sdf_read_item| returns a pointer to the internal
alpar@1
   841
buffer, which contains the data item read in the form of a
alpar@1
   842
null-terminated character string.
alpar@1
   843
alpar@1
   844
\newpage
alpar@1
   845
alpar@1
   846
\subsection{glp\_sdf\_read\_text---read text until end of line}
alpar@1
   847
alpar@1
   848
\subsubsection*{Synopsis}
alpar@1
   849
alpar@1
   850
\begin{verbatim}
alpar@1
   851
const char *glp_sdf_read_text(glp_data *data);
alpar@1
   852
\end{verbatim}
alpar@1
   853
alpar@1
   854
\subsubsection*{Description}
alpar@1
   855
alpar@1
   856
The routine \verb|glp_sdf_read_text| reads a text from the plain data
alpar@1
   857
file specified by the parameter \verb|data|.
alpar@1
   858
alpar@1
   859
Reading starts from the current position and extends until end of the
alpar@1
   860
current line. Initial and trailing white-space characters as well as
alpar@1
   861
the newline character are not included in the text.
alpar@1
   862
alpar@1
   863
\subsubsection*{Returns}
alpar@1
   864
alpar@1
   865
The routine \verb|glp_sdf_read_text| returns a pointer to the internal
alpar@1
   866
buffer, which contains the text read in the form of a null-terminated
alpar@1
   867
character string.
alpar@1
   868
alpar@1
   869
\subsection{glp\_sdf\_line---determine current line number}
alpar@1
   870
alpar@1
   871
\subsubsection*{Synopsis}
alpar@1
   872
alpar@1
   873
\begin{verbatim}
alpar@1
   874
int glp_sdf_line(glp_data *data);
alpar@1
   875
\end{verbatim}
alpar@1
   876
alpar@1
   877
\subsubsection*{Returns}
alpar@1
   878
alpar@1
   879
The routine \verb|glp_sdf_line| returns the current line number for the
alpar@1
   880
plain data file specified by the parameter \verb|data|.
alpar@1
   881
alpar@1
   882
\subsection{glp\_sdf\_close\_file---close plain data file}
alpar@1
   883
alpar@1
   884
\subsubsection*{Synopsis}
alpar@1
   885
alpar@1
   886
\begin{verbatim}
alpar@1
   887
void glp_sdf_close_file(glp_data *data);
alpar@1
   888
\end{verbatim}
alpar@1
   889
alpar@1
   890
\subsubsection*{Description}
alpar@1
   891
alpar@1
   892
The routine \verb|glp_sdf_close_file| closes the plain data file
alpar@1
   893
specified by the parameter \verb|data| and frees all the resources
alpar@1
   894
allocated to this program object.
alpar@1
   895
alpar@1
   896
%* eof *%