1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
|
// SuiteSparse/MATLAB_Tools/waitmex/waitmex.c
// waitmex, Copyright (c) 2007, Timothy A Davis. All Rights Reserved.
// SPDX-License-Identifier: BSD-3-clause
/* -------------------------------------------------------------------------- */
/* waitmex functions */
/* -------------------------------------------------------------------------- */
/* This file includes the following functions for handling waitbars in MATLAB:
waitbar_create: create a waitbar
waitbar_update: update a waitbar
waitbar_destroy: destroy a waitbar
waitbar_return: return a waitbar
in C MATLAB equivalent
---- -----------------
h = waitbar_create (x,msg) h = waitbar (x,msg)
waitbar_update (x,h,NULL) waitbar (x,h)
waitbar_update (x,h,msg) waitbar (x,h,msg)
waitbar_destroy (h) close (h)
waitbar_return (h) for returning h from a mexFunction
Copyright 2007, Timothy A. Davis, http://www.suitesparse.com
*/
#include "waitmex.h"
/* -------------------------------------------------------------------------- */
/* waitbar_create: create a waitbar */
/* -------------------------------------------------------------------------- */
/* Create a waitbar and return a point to the new waitbar. Example:
* h = waitbar_create (x, "Please wait...") in C is just like
* h = waitbar (x, 'Please wait') in MATLAB, where x is a fraction between
* 0 and 1.
*/
waitbar *waitbar_create /* return a pointer to the new waitbar */
(
double fraction, /* fraction from 0 to 1 */
char *message /* message to display */
)
{
int error ;
waitbar *h ;
h = mxMalloc (sizeof (waitbar)) ;
h->fraction = mxCreateDoubleScalar (fraction) ;
h->message = mxCreateString (message) ;
/* h = waitbar (fraction, message) ; */
h->inputs [0] = h->fraction ;
h->inputs [1] = h->message ;
error = mexCallMATLAB (1, h->outputs, 2, h->inputs, "waitbar") ;
if (error)
{
mexErrMsgTxt ("unable to create waitbar") ;
}
/* save the MATLAB handle h in the waitbar struct */
h->handle = h->outputs [0] ;
return (h) ;
}
/* -------------------------------------------------------------------------- */
/* waitbar_update: update a waitbar */
/* -------------------------------------------------------------------------- */
/* Update the length of the bar in an existing waitbar. Example:
* waitbar_update (x, h, NULL) in C is just like waitbar (x,h) in MATLAB,
* where h is the handle to the existing waitbar. The message is not changed.
* To change the message, use waitbar_update (x, h, "new message"), which is
* just like waitbar (x, h, 'new message') in MATLAB.
*/
void waitbar_update
(
double fraction,
waitbar *h,
char *message
)
{
int error ;
if (h == NULL) return ; /* nothing to do */
(* mxGetPr (h->fraction)) = fraction ; /* update the fraction */
h->inputs [0] = h->fraction ; /* define the inputs x and h */
h->inputs [1] = h->handle ;
if (message == NULL)
{
/* use the existing message; waitbar (x,h) in MATLAB */
error = mexCallMATLAB (0, h->outputs, 2, h->inputs, "waitbar") ;
}
else
{
/* define a new message; waitbar (x,h,message) in MATLAB */
mxDestroyArray (h->message) ;
h->message = mxCreateString (message) ;
h->inputs [2] = h->message ;
error = mexCallMATLAB (0, h->outputs, 3, h->inputs, "waitbar") ;
}
if (error)
{
mexErrMsgTxt ("unable to update waitbar") ;
}
}
/* -------------------------------------------------------------------------- */
/* waitbar_destroy: destroy a waitbar */
/* -------------------------------------------------------------------------- */
/* Destroys a waitbar; same as close(h) in MATLAB */
void waitbar_destroy
(
waitbar *h
)
{
int error ;
if (h == NULL) return ; /* nothing to do */
h->inputs [0] = h->handle ;
mxDestroyArray (h->fraction) ; /* free the internal mxArrays */
mxDestroyArray (h->message) ;
error = mexCallMATLAB (0, h->outputs, 1, h->inputs, "close") ;
mxDestroyArray (h->handle) ;
mxFree (h) ;
if (error)
{
mexErrMsgTxt ("error closing waitbar") ;
}
}
/* -------------------------------------------------------------------------- */
/* waitbar_return: return a waitbar handle to the caller */
/* -------------------------------------------------------------------------- */
/* This function frees the space used internally in a mexFunction for managing
* the waitbar, and returns the mxArray handle to the caller. The waitbar still
* exists in MATLAB. Example: pargout [0] = waitbar_return (h) to return the
* MATLAB handle to the caller of your mexFunction.
*/
mxArray *waitbar_return
(
waitbar *h
)
{
mxArray *handle ;
if (h == NULL) return (NULL) ; /* nothing to do */
handle = h->handle ; /* get the MATLAB handle */
mxDestroyArray (h->fraction) ; /* free the internal mxArrays */
mxDestroyArray (h->message) ;
mxFree (h) ;
return (handle) ; /* return the MATLAB handle */
}
|