1 | /* |
---|
2 | * ---------------------------------------------------------------------- |
---|
3 | * INTERFACE: Octave Rappture Library Source |
---|
4 | * |
---|
5 | * [retStr,err] = rpUnitsConvert(fromVal, toUnitsName, showUnits) |
---|
6 | * |
---|
7 | * ====================================================================== |
---|
8 | * AUTHOR: Derrick Kearney, Purdue University |
---|
9 | * Copyright (c) 2005 |
---|
10 | * Purdue Research Foundation, West Lafayette, IN |
---|
11 | * ====================================================================== |
---|
12 | */ |
---|
13 | |
---|
14 | #include "RpOctaveInterface.h" |
---|
15 | |
---|
16 | /**********************************************************************/ |
---|
17 | // METHOD: [retStr,err] = rpUnitsConvert(fromVal,toUnitsName,showUnits) |
---|
18 | /// Convert between RpUnits return a string value with or without units |
---|
19 | /** |
---|
20 | * Convert the value and units in the string @var{fromVal} to units specified |
---|
21 | * in string @var{toUnitsName}. If @var{showUnits} is set to 1, then show the |
---|
22 | * units in the returned string @var{retStr}, else leave the units off. |
---|
23 | * The second return value @var{err} specifies whether there was an error |
---|
24 | * during conversion. |
---|
25 | * Error code, err=0 on success, anything else is failure. |
---|
26 | */ |
---|
27 | |
---|
28 | DEFUN_DLD (rpUnitsConvert, args, , |
---|
29 | "-*- texinfo -*-\n\ |
---|
30 | [retStr,err] = rpUnitsConvert(@var{fromVal},@var{toUnitsName},@var{showUnits})\n\ |
---|
31 | \n\ |
---|
32 | Convert the value and units in the string @var{fromVal} to units specified\n\ |
---|
33 | in string @var{toUnitsName}. If @var{showUnits} is set to 1, then show the\n\ |
---|
34 | units in the returned string @var{retStr}, else leave the units off.\n\ |
---|
35 | The second return value @var{err} specifies whether there was an error\n\ |
---|
36 | during conversion. \n\ |
---|
37 | Error code, err=0 on success, anything else is failure.") |
---|
38 | { |
---|
39 | static std::string who = "rpUnitsConvert"; |
---|
40 | |
---|
41 | // The list of values to return. |
---|
42 | octave_value_list retval; |
---|
43 | int err = 1; |
---|
44 | int nargin = args.length (); |
---|
45 | std::string fromVal = ""; |
---|
46 | std::string toUnitsName = ""; |
---|
47 | int showUnits = 0; |
---|
48 | std::string retStr = ""; |
---|
49 | |
---|
50 | if (nargin == 3) { |
---|
51 | |
---|
52 | if ( args(0).is_string () && |
---|
53 | args(1).is_string () && |
---|
54 | args(2).is_real_scalar () ) { |
---|
55 | |
---|
56 | fromVal = args(0).string_value (); |
---|
57 | toUnitsName = args(1).string_value (); |
---|
58 | showUnits = args(2).int_value (); |
---|
59 | |
---|
60 | // Call the C++ subroutine. |
---|
61 | // we allow toUnitsName to be an empty string |
---|
62 | // to let the user not change the units of fromVal |
---|
63 | // but still remove the units from being shown. |
---|
64 | if ( !fromVal.empty() ) { |
---|
65 | |
---|
66 | retStr = RpUnits::convert(fromVal,toUnitsName,showUnits,&err); |
---|
67 | } |
---|
68 | else { |
---|
69 | print_usage (who.c_str()); |
---|
70 | } |
---|
71 | } |
---|
72 | else { |
---|
73 | print_usage (who.c_str()); |
---|
74 | } |
---|
75 | } |
---|
76 | else { |
---|
77 | print_usage (who.c_str()); |
---|
78 | } |
---|
79 | |
---|
80 | retval(0) = retStr; |
---|
81 | retval(1) = err; |
---|
82 | return retval; |
---|
83 | } |
---|