3 \chapter{Miscellaneous API Routines}
5 \section{GLPK environment routines}
7 \subsection{glp\_long---64-bit integer data type}
9 Some GLPK API routines use 64-bit integer data type, which is declared
10 in the header \verb|glpk.h| as follows:
13 typedef struct { int lo, hi; } glp_long;
17 where \verb|lo| contains low 32 bits, and \verb|hi| contains high 32
18 bits of 64-bit integer value.\footnote{GLPK conforms to ILP32, LLP64,
19 and LP64 programming models, where the built-in type {\tt int}
20 corresponds to 32-bit integers.}
22 \subsection{glp\_init\_env---initialize GLPK environment}
24 \subsubsection*{Synopsis}
27 int glp_init_env(void);
30 \subsubsection*{Description}
32 The routine \verb|glp_init_env| initializes the GLPK environment.
33 Normally the application program does not need to call this routine,
34 because it is called automatically on the first call to any API routine.
38 \subsubsection*{Returns}
40 The routine \verb|glp_init_env| returns one of the following codes:
43 0 --- initialization successful;
46 1 --- environment is already initialized;
49 2 --- initialization failed (insufficient memory);
52 3 --- initialization failed (unsupported programming model).
54 \subsection{glp\_version---determine library version}
56 \subsubsection*{Synopsis}
59 const char *glp_version(void);
62 \subsubsection*{Returns}
64 The routine \verb|glp_version| returns a pointer to a null-terminated
65 character string, which specifies the version of the GLPK library in
66 the form \verb|"X.Y"|, where `\verb|X|' is the major version number, and
67 `\verb|Y|' is the minor version number, for example, \verb|"4.16"|.
69 \subsubsection*{Example}
73 printf("GLPK version is %s\n", glp_version());
77 \subsection{glp\_free\_env---free GLPK environment}
79 \subsubsection*{Synopsis}
82 int glp_free_env(void);
85 \subsubsection*{Description}
87 The routine \verb|glp_free_env| frees all resources used by GLPK
88 routines (memory blocks, etc.) which are currently still in use.
90 Normally the application program does not need to call this routine,
91 because GLPK routines always free all unused resources. However, if
92 the application program even has deleted all problem objects, there
93 will be several memory blocks still allocated for the internal library
94 needs. For some reasons the application program may want GLPK to free
95 this memory, in which case it should call \verb|glp_free_env|.
97 Note that a call to \verb|glp_free_env| invalidates all problem objects
100 \subsubsection*{Returns}
102 The routine \verb|glp_free_env| returns one of the following codes:
105 0 --- termination successful;
108 1 --- environment is inactive (was not initialized).
110 \subsection{glp\_printf---write formatted output to terminal}
112 \subsubsection*{Synopsis}
115 void glp_printf(const char *fmt, ...);
118 \subsubsection*{Description}
120 The routine \verb|glp_printf| uses the format control string
121 \verb|fmt| to format its parameters and writes the formatted output to
124 This routine is a replacement of the standard C function
125 \verb|printf| and used by all GLPK routines to perform terminal
126 output. The application program may use \verb|glp_printf| for the same
127 purpose that allows controlling its terminal output with the routines
128 \verb|glp_term_out| and \verb|glp_term_hook|.
130 \subsection{glp\_vprintf---write formatted output to terminal}
132 \subsubsection*{Synopsis}
135 void glp_vprintf(const char *fmt, va_list arg);
138 \subsubsection*{Description}
140 The routine \verb|glp_vprintf| uses the format control string
141 \verb|fmt| to format its parameters specified by the list \verb|arg|
142 and writes the formatted output to the terminal.
144 This routine is a replacement of the standard C function
145 \verb|vprintf| and used by all GLPK routines to perform terminal
146 output. The application program may use \verb|glp_vprintf| for the same
147 purpose that allows controlling its terminal output with the routines
148 \verb|glp_term_out| and \verb|glp_term_hook|.
152 \subsection{glp\_term\_out---enable/disable terminal output}
154 \subsubsection*{Synopsis}
157 int glp_term_out(int flag);
160 \subsubsection*{Description}
162 Depending on the parameter flag the routine \verb|glp_term_out| enables
163 or disables terminal output performed by glpk routines:
165 \verb|GLP_ON | --- enable terminal output;
167 \verb|GLP_OFF| --- disable terminal output.
169 \subsubsection*{Returns}
171 The routine \verb|glp_term_out| returns the previous value of the
172 terminal output flag (\verb|GLP_ON| or \verb|GLP_OFF|).
174 \subsection{glp\_term\_hook---intercept terminal output}
176 \subsubsection*{Synopsis}
179 void glp_term_hook(int (*func)(void *info, const char *s),
183 \subsubsection*{Description}
185 The routine \verb|glp_term_hook| installs the user-defined hook routine
186 to intercept all terminal output performed by GLPK routines.
188 %This feature can be used to redirect the terminal output to other
189 %destination, for example, to a file or a text window.
191 The parameter {\it func} specifies the user-defined hook routine. It is
192 called from an internal printing routine, which passes to it two
193 parameters: {\it info} and {\it s}. The parameter {\it info} is a
194 transit pointer specified in corresponding call to the routine
195 \verb|glp_term_hook|; it may be used to pass some additional information
196 to the hook routine. The parameter {\it s} is a pointer to the null
197 terminated character string, which is intended to be written to the
198 terminal. If the hook routine returns zero, the printing routine writes
199 the string {\it s} to the terminal in a usual way; otherwise, if the
200 hook routine returns non-zero, no terminal output is performed.
202 To uninstall the hook routine both parameters {\it func} and {\it info}
203 should be specified as \verb|NULL|.
207 \subsubsection*{Example}
211 static int hook(void *info, const char *s)
220 /* redirect terminal output */
221 glp_term_hook(hook, foo);
223 /* resume terminal output */
224 glp_term_hook(NULL, NULL);
230 \subsection{glp\_open\_tee---start copying terminal output}
232 \subsubsection*{Synopsis}
235 int glp_open_tee(const char *fname);
238 \subsubsection*{Description}
240 The routine \verb|glp_open_tee| starts copying all the terminal output
241 to an output text file, whose name is specified by the character string
244 \subsubsection*{Returns}
246 The routine \verb|glp_open_tee| returns one of the following codes:
249 0 --- operation successful;
252 1 --- copying terminal output is already active;
255 2 --- unable to create output file.
259 \subsection{glp\_close\_tee---stop copying terminal output}
261 \subsubsection*{Synopsis}
264 int glp_close_tee(void);
267 \subsubsection*{Description}
269 The routine \verb|glp_close_tee| stops copying the terminal output to
270 the output text file previously open by the routine \verb|glp_open_tee|
273 \subsubsection*{Returns}
275 The routine \verb|glp_close_tee| returns one of the following codes:
278 0 --- operation successful;
281 1 --- copying terminal output was not started.
283 \subsection{glp\_error---display error message and terminate execution}
285 \subsubsection*{Synopsis}
288 void glp_error(const char *fmt, ...);
291 \subsubsection*{Description}
293 The routine \verb|glp_error| (implemented as a macro) formats its
294 parameters using the format control string \verb|fmt|, writes the
295 formatted message to the terminal, and then abnormally terminates the
298 \subsection{glp\_assert---check logical condition}
300 \subsubsection*{Synopsis}
303 void glp_assert(int expr);
306 \subsubsection*{Description}
308 The routine \verb|glp_assert| (implemented as a macro) checks
309 a logical condition specified by the expression \verb|expr|. If the
310 condition is true (non-zero), the routine does nothing; otherwise, if
311 the condition is false (zero), the routine prints an error message and
312 abnormally terminates the program.
314 This routine is a replacement of the standard C function \verb|assert|
315 and used by all GLPK routines to check program logic. The application
316 program may use \verb|glp_assert| for the same purpose.
318 \subsection{glp\_error\_hook---install hook to intercept abnormal
321 \subsubsection*{Synopsis}
324 void glp_error_hook(void (*func)(void *info), void *info);
327 \subsubsection*{Description}
329 The routine \verb|glp_error_hook| installs a user-defined hook routine
330 to intercept abnormal termination.
332 The parameter \verb|func| specifies the user-defined hook routine. It
333 is called from the routine \verb|glp_error| before the latter calls the
334 abort function to abnormally terminate the application program because
335 of fatal error. The parameter \verb|info| is a transit pointer,
336 specified in the corresponding call to the routine
337 \verb|glp_error_hook|; it may be used to pass some information to the
340 To uninstall the hook routine the parameters \verb|func| and \verb|info|
341 should be specified as \verb|NULL|.
343 \subsubsection*{Usage note}
345 If the hook routine returns, the application program is abnormally
346 terminated. To prevent abnormal termnation the hook routine may perform
347 a global jump using the standard function \verb|longjmp|, in which case
348 the application program {\it must} call the routine \verb|glp_free_env|.
350 \subsection{glp\_malloc---allocate memory block}
352 \subsubsection*{Synopsis}
355 void *glp_malloc(int size);
358 \subsubsection*{Description}
360 The routine \verb|glp_malloc| dynamically allocates a memory block of
361 \verb|size| bytes long. Should note that:
363 1) the parameter \verb|size| must be positive;
365 2) being allocated the memory block contains arbitrary data, that is,
366 it is {\it not} initialized by binary zeros;
368 3) if the block cannot be allocated due to insufficient memory, the
369 routine prints an error message and abnormally terminates the program.
371 This routine is a replacement of the standard C function \verb|malloc|
372 and used by all GLPK routines for dynamic memory allocation. The
373 application program may use \verb|glp_malloc| for the same purpose.
375 \subsubsection*{Returns}
377 The routine \verb|glp_malloc| returns a pointer to the memory block
378 allocated. To free this block the routine \verb|glp_free| (not the
379 standard C function \verb|free|!) must be used.
381 \subsection{glp\_calloc---allocate memory block}
383 \subsubsection*{Synopsis}
386 void *glp_calloc(int n, int size);
389 \subsubsection*{Description}
391 The routine \verb|glp_calloc| dynamically allocates a memory block of
392 \verb|n|$\times$\verb|size| bytes long. Should note that:
394 1) both parameters \verb|n| and \verb|size| must be positive;
396 2) being allocated the memory block contains arbitrary data, that is,
397 it is {\it not} initialized by binary zeros;
399 3) if the block cannot be allocated due to insufficient memory, the
400 routine prints an error message and abnormally terminates the program.
402 This routine is a replacement of the standard C function \verb|calloc|
403 (with exception that the block is not cleaned) and used by all GLPK
404 routines for dynamic memory allocation. The application program may use
405 \verb|glp_calloc| for the same purpose.
407 \subsubsection*{Returns}
409 The routine \verb|glp_calloc| returns a pointer to the memory block
410 allocated. To free this block the routine \verb|glp_free| (not the
411 standard C function \verb|free|!) must be used.
413 \subsection{glp\_free---free memory block}
415 \subsubsection*{Synopsis}
418 void glp_free(void *ptr);
421 \subsubsection*{Description}
423 The routine \verb|glp_free| frees (deallocates) a memory block pointed
424 to by \verb|ptr|, which was previously allocated by the routine
425 \verb|glp_malloc| or \verb|glp_calloc|. Note that the pointer \verb|ptr|
426 must valid and must not be \verb|NULL|.
428 This routine is a replacement of the standard C function \verb|free|
429 and used by all GLPK routines for dynamic memory allocation. The
430 application program may use \verb|glp_free| for the same purpose.
432 \subsection{glp\_mem\_usage---get memory usage information}
434 \subsubsection*{Synopsis}
437 void glp_mem_usage(int *count, int *cpeak, glp_long *total,
441 \subsubsection*{Description}
443 The routine \verb|glp_mem_usage| reports some information about
444 utilization of the memory by the routines \verb|glp_malloc|,
445 \verb|glp_calloc|, and \verb|glp_free|. Information is stored to
446 locations specified by corresponding parameters (see below). Any
447 parameter can be specified as \verb|NULL|, in which case corresponding
448 information is not stored.
450 \verb|*count| is the number of currently allocated memory blocks.
452 \verb|*cpeak| is the peak value of \verb|*count| reached since the
453 initialization of the GLPK library environment.
455 \verb|*total| is the total amount, in bytes, of currently allocated
458 \verb|*tpeak| is the peak value of \verb|*total| reached since the
459 initialization of the GLPK library envirionment.
461 \subsubsection*{Example}
465 glp_mem_usage(&count, NULL, NULL, NULL);
466 printf("%d memory block(s) are still allocated\n", count);
470 \subsection{glp\_mem\_limit---set memory usage limit}
472 \subsubsection*{Synopsis}
475 void glp_mem_limit(int limit);
478 \subsubsection*{Description}
480 The routine \verb|glp_mem_limit| limits the amount of memory available
481 for dynamic allocation (with the routines \verb|glp_malloc| and
482 \verb|glp_calloc|) to \verb|limit| megabytes.
484 \subsection{glp\_time---determine current universal time}
486 \subsubsection*{Synopsis}
489 glp_long glp_time(void);
492 \subsection*{Returns}
494 The routine \verb|glp_time| returns the current universal time (UTC),
495 in milliseconds, elapsed since 00:00:00 GMT January 1, 1970.
497 \subsection{glp\_difftime---compute difference between two time values}
499 \subsubsection*{Synopsis}
502 double glp_difftime(glp_long t1, glp_long t0);
505 \subsection*{Returns}
507 The routine \verb|glp_difftime| returns the difference between two time
508 values \verb|t1| and \verb|t0|, expressed in seconds.
510 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
514 \section{Plain data file reading routines}
516 \subsection{Introduction}
518 On developing simple applications to solve optimization problems it is
519 often needed to read data from plain text files. To do this the standard
520 C function \verb|fscanf| may be used, however, it is not convenient; for
521 example, if it scans an integer number according to the format
522 specification `\verb|%d|', and that number is coded incorrectly,
523 no diagnostics is provided.
525 This section describes a set of GLPK API routines, which may be used in
526 application programs to simplify reading data from plain text files.
528 \subsubsection*{Example 1}
530 The following main program reads ten integer numbers from plain text
531 file \verb|data.txt| and prints their sum.
544 /* open plain data file */
545 data = glp_sdf_open_file("data.txt");
546 if (data == NULL) exit(EXIT_FAILURE);
548 for (j = 1; j <= 10; j++)
549 { /* read next integer number */
550 num = glp_sdf_read_int(data);
553 printf("sum = %d\n", sum);
554 /* close plain data file */
555 glp_sdf_close_file(data);
563 The input data are coded in free format. For example, the file
564 \verb|data.txt| may look like this:
568 123 65 432 890 -12 743 895 -7 111 326
583 If the input data file contains incorrect data, the routine
584 \verb|glp_sdf_read_int| prints an error message and, if no error
585 handling is provided by the application program, abnormally terminates
586 program execution. For example, the file \verb|data.txt| could contain
597 in which case the error message would be the following:
601 data.txt:2: cannot convert `=7' to integer
605 \subsubsection*{Example 2}
607 As it was said above, by default any attempt to read incorrect data
608 leads to abnormal termination. However, sometimes it is desirable to
609 catch such errors. This feature is illustrated by the following main
610 program, which does the same job as in the previous example.
624 int j, num, sum, ret;
625 /* open plain data file */
626 data = glp_sdf_open_file("data.txt");
628 { ret = EXIT_FAILURE;
631 /* set up error handling */
633 { ret = EXIT_FAILURE;
636 glp_sdf_set_jump(data, jump);
637 /* read and process data */
639 for (j = 1; j <= 10; j++)
640 { /* read next integer number */
641 num = glp_sdf_read_int(data);
643 glp_sdf_error(data, "integer %d too big\n", num);
645 glp_sdf_warning(data, "integer %d is negative\n", num);
648 printf("sum = %d\n", sum);
650 done: /* close plain data file */
651 if (data != NULL) glp_sdf_close_file(data);
659 \subsection{glp\_sdf\_open\_file---open plain data file}
661 \subsubsection*{Synopsis}
664 glp_data *glp_sdf_open_file(const char *fname);
667 \subsubsection*{Description}
669 The routine \verb|glp_sdf_open_file| opens a plain data file, whose
670 name is specified by the character string \verb|fname|.
672 \subsubsection*{Returns}
674 If the operation was successful, the routine \verb|glp_sdf_open_file|
675 returns a pointer to the opaque program object of the type
676 \verb|glp_data|\footnote{This data structure is declared in the header
677 file {\tt glpk.h}.} associated with the plain data file. Otherwise, if
678 the operation failed, the routine prints an error message and returns
681 \subsubsection*{Note}
683 The application program should use the pointer returned by the routine
684 \verb|glp_sdf_open_file| to perform all subsequent operations on the
689 \subsection{glp\_sdf\_set\_jump---set up error handling}
691 \subsubsection*{Synopsis}
694 void glp_sdf_set_jump(glp_data *data, jmp_buf jump);
697 \subsubsection*{Description}
699 The routine \verb|glp_sdf_set_jump| sets up error handling for the
700 plain data file specified by the parameter \verb|data|.
702 The parameter \verb|jump| specifies the environment buffer, which must
703 be initialized with the standard C function \verb|setjmp| prior to call
704 to the routine \verb|glp_sdf_set_jump|. Detecting any incorrect data in
705 the corresponding plain data file will cause non-local ``go to'' by
706 a call to the standard C function \verb|longjmp|.
708 The parameter \verb|jump| can be specified as \verb|NULL|, in which
709 case the routine \verb|glp_sdf_set_jump| restores the default behavior,
710 in which case detecting incorrect data leads to abnormal termination.
712 \subsection{glp\_sdf\_error---print error message}
714 \subsubsection*{Synopsis}
717 void glp_sdf_error(glp_data *data, const char *fmt, ...);
720 \subsubsection*{Description}
722 The routine \verb|glp_sdf_error| prints an error message related to the
723 plain data file specified by the parameter \verb|data|. If error handing
724 was not previously provided, the routine then abnormally terminates
725 execution of the application program. Otherwise, it signals about the
726 error by a call to the standard C function \verb|longjmp|.
728 The character string \verb|fmt| and optional parameters following it
729 have the same meaning as for the standard C function \verb|printf|.
731 The message produced by the routine \verb|glp_sdf_error| looks like
736 {\it file}{\tt :}{\it line}{\tt :} {\it message text}
741 where {\it file} is the filename passed to the routine
742 \verb|glp_sdf_open| and {\it line} is the current line number.
746 \subsection{glp\_sdf\_warning---print warning message}
748 \subsubsection*{Synopsis}
751 void glp_sdf_warning(glp_data *data, const char *fmt, ...);
754 \subsubsection*{Description}
756 The routine \verb|glp_sdf_warning| prints a warning message related to
757 the plain data file specified by the parameter \verb|data|.
759 The character string \verb|fmt| and optional parameters following it
760 have the same meaning as for the standard C function \verb|printf|.
762 The message produced by the routine \verb|glp_sdf_warning| looks like
767 {\it file}{\tt :}{\it line}\verb|: warning:| {\it message text}
772 where {\it file} is the filename passed to the routine
773 \verb|glp_sdf_open| and {\it line} is the current line number.
775 \subsection{glp\_sdf\_read\_int---read integer number}
777 \subsubsection*{Synopsis}
780 int glp_sdf_read_int(glp_data *data);
783 \subsubsection*{Description}
785 The routine \verb|glp_sdf_read_int| skips optional white-space
786 characters and then reads an integer number from the plain data file
787 specified by the parameter \verb|data|. If the operation failed, the
788 routine \verb|glp_sdf_read_int| calls the routine \verb|glp_sdf_error|
791 \subsubsection*{Returns}
793 The routine \verb|glp_sdf_read_int| returns the integer number read.
797 \subsection{glp\_sdf\_read\_num---read floating-point number}
799 \subsubsection*{Synopsis}
802 double glp_sdf_read_num(glp_data *data);
805 \subsubsection*{Description}
807 The routine \verb|glp_sdf_read_num| skips optional white-space
808 characters and then reads a floating-point number from the plain data
809 file specified by the parameter \verb|data|. If the operation failed,
810 the routine \verb|glp_sdf_num| calls the routine \verb|glp_sdf_error|
813 \subsubsection*{Returns}
815 The routine \verb|glp_sdf_read_num| returns the floating-point number
818 \subsection{glp\_sdf\_read\_item---read data item}
820 \subsubsection*{Synopsis}
823 const char *glp_sdf_read_item(glp_data *data);
826 \subsubsection*{Description}
828 The routine \verb|glp_sdf_read_item| skips optional white-space
829 characters and then reads a data item from the plain data file specified
830 by the parameter \verb|data|. If the operation failed, the routine
831 \verb|glp_sdf_read_item| calls the routine \verb|glp_sdf_error| (see
834 {\it Data item} is a sequence of 1 to 255 arbitrary graphic characters
835 delimited by white-space characters. Data items may be used to represent
836 symbolic names, identifiers, etc.
838 \subsubsection*{Returns}
840 The routine \verb|glp_sdf_read_item| returns a pointer to the internal
841 buffer, which contains the data item read in the form of a
842 null-terminated character string.
846 \subsection{glp\_sdf\_read\_text---read text until end of line}
848 \subsubsection*{Synopsis}
851 const char *glp_sdf_read_text(glp_data *data);
854 \subsubsection*{Description}
856 The routine \verb|glp_sdf_read_text| reads a text from the plain data
857 file specified by the parameter \verb|data|.
859 Reading starts from the current position and extends until end of the
860 current line. Initial and trailing white-space characters as well as
861 the newline character are not included in the text.
863 \subsubsection*{Returns}
865 The routine \verb|glp_sdf_read_text| returns a pointer to the internal
866 buffer, which contains the text read in the form of a null-terminated
869 \subsection{glp\_sdf\_line---determine current line number}
871 \subsubsection*{Synopsis}
874 int glp_sdf_line(glp_data *data);
877 \subsubsection*{Returns}
879 The routine \verb|glp_sdf_line| returns the current line number for the
880 plain data file specified by the parameter \verb|data|.
882 \subsection{glp\_sdf\_close\_file---close plain data file}
884 \subsubsection*{Synopsis}
887 void glp_sdf_close_file(glp_data *data);
890 \subsubsection*{Description}
892 The routine \verb|glp_sdf_close_file| closes the plain data file
893 specified by the parameter \verb|data| and frees all the resources
894 allocated to this program object.