plib.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. ///////////////////////////////////////////////////////////////////////////////////////
  2. /// \file plib.h
  3. /// \brief PLIB Version 2.1 (Fully portable version)
  4. ///
  5. /// PLIB is a utility library for reading input from a profile script. This is a
  6. /// text file consisting of commands and data, in a format defined in part by PLIB,
  7. /// and in part by the calling program. Language elements supported are:
  8. ///
  9. /// - COMMANDS: statements beginning with an identifier (a string of non-white
  10. /// space characters commencing with an alphabetic character and not including
  11. /// any of the following: - + . " ' ( ) !. The identifier may be followed by
  12. /// a string (enclosed in single or double quotation marks) with a specified
  13. /// maximum significant length, a specified number of integers or real numbers
  14. /// in a specified range, a specified number of bool values (numerals
  15. /// interpreted such that 0=false, non-zero=true) or no parameters at all.
  16. /// Identifiers are non-case-sensitive.
  17. /// e.g.
  18. /// \code
  19. /// title "Kiruna"
  20. /// mtemp -14.2 -13.5 -9.5 -3.5 2.9 9.3 12.6 10.3 4.8 -1.5 -8.1 -12.1
  21. /// include 1
  22. /// exit
  23. /// \endcode
  24. ///
  25. /// - SETS: named blocks of script enclosed in parentheses. Sets may be nested.
  26. /// e.g.
  27. /// \code
  28. /// taxon "picea" ( ... )
  29. /// \endcode
  30. ///
  31. /// - GROUPS: named blocks of script enclosed in parentheses which are interpreted
  32. /// as "macros" when the script is interpreted. The contents of the group are
  33. /// implicitly inserted in the script wherever the name of the group appears
  34. /// following the group definition. Groups may be nested, but always have
  35. /// 'global' scope. Groups are identified by the keyword group followed by the
  36. /// group name in single or double quotation marks.
  37. /// e.g.
  38. /// \code
  39. /// group 'woody' ( tree 1 wooddens 250 leaftoroot 1.0 )
  40. /// taxon "picea" ( woody )
  41. /// \endcode
  42. ///
  43. /// - COMMENTS: strings of characters beginning with an exclamation mark (!). They
  44. /// are ignored to the end of the line they appear on.
  45. /// e.g.
  46. /// \code
  47. /// !a comment example
  48. /// \endcode
  49. ///
  50. /// \section using_plib Using PLIB
  51. ///
  52. /// The recommended way of building an executable incorporating the functionality
  53. /// of PLIB is to include plib.h and plib.cpp into your project as regular source
  54. /// code files. If you place plib.h somewhere in your include path, include PLIB
  55. /// with:
  56. ///
  57. /// \code
  58. /// #include <plib.h>
  59. /// \endcode
  60. ///
  61. /// Otherwise, you may have to specify a relative or full path name, such as:
  62. ///
  63. /// \code
  64. /// #include "include/plib.h"
  65. /// \endcode
  66. ///
  67. /// Character strings are represented in PLIB as xtring objects. String arguments
  68. /// to PLIB library functions are also of type xtring. The xtring class is included
  69. /// in the GUTIL library.
  70. ///
  71. ///
  72. /// \section reading Reading information from a PLIB script
  73. ///
  74. /// To process a PLIB script, call function plib, specifying the full pathname of
  75. /// the script file to process:
  76. ///
  77. /// bool plib(xtring filename)
  78. ///
  79. /// Function plib returns TRUE if there were no errors, otherwise FALSE (zero).
  80. /// In the event of an error, an explanatory message is sent to function
  81. /// plib_receivemessage in the calling program.
  82. ///
  83. ///
  84. /// \section communication Communication between PLIB and the calling program
  85. ///
  86. /// Communication is via PLIB library functions, accessible to the calling program.
  87. /// The calling program must also implement two, and in some cases, three, special
  88. /// functions accessible to PLIB:
  89. ///
  90. /// - plib_declarations
  91. /// - plib_receivemessage
  92. /// - plib_callback
  93. ///
  94. /// The calling program must supply functions plib_declarations and
  95. /// plib_receivemessage and, if the callback feature is implemented for any command
  96. /// function plib_callback.
  97. ///
  98. ///
  99. /// Enquiries to: Joe Siltberg, Lund University: joe.siltberg@nateko.lu.se
  100. /// All rights reserved, copyright retained by the author.
  101. ///
  102. /// \author Ben Smith, University of Lund
  103. /// $Date: 2014-09-09 10:49:13 +0200 (mar, 09 sep 2014) $
  104. ///
  105. ///////////////////////////////////////////////////////////////////////////////////////
  106. #ifndef PLIB_H
  107. #define PLIB_H
  108. #include <gutil.h>
  109. #include <string>
  110. // PLIB LIBRARY FUNCTIONS
  111. /// Starts the processing of a PLIB script
  112. /** This function will call the function plib_declarations to declare
  113. * all items.
  114. *
  115. * This function may also call plib_callback if the callback feature
  116. * is used.
  117. *
  118. * In the event of an error, an explanatory message is sent to function
  119. * plib_receivemessage in the calling program.
  120. *
  121. * It is the responsibility of the calling program to implement these
  122. * functions.
  123. *
  124. * \returns TRUE if there were no errors, otherwise FALSE (zero).
  125. */
  126. bool plib(xtring filename);
  127. /// Declares a string (xtring) parameter
  128. /** When the string specified by identifier is encountered, expect the next item
  129. * to be a string and write it to 'param'. 'maxlen' specifies the maximum
  130. * allowable length of the string, not including the terminating null character;
  131. * the string is truncated, if necessary, before being written to 'param'. If
  132. * 'callback' is non-zero, function plib_callback is called with the
  133. * integer value of 'callback' as a parameter, AFTER the assignment to param.
  134. * e.g.
  135. * \code
  136. * title "kiruna"
  137. * \endcode
  138. */
  139. bool declareitem(xtring identifier,xtring* param,int maxlen,int callback,
  140. xtring help = "");
  141. /// Declares a string (std::string) parameter
  142. /** Works like the corresponding function for xtring
  143. */
  144. bool declareitem(xtring identifier, std::string* param, int maxlen, int callback,
  145. xtring help = "");
  146. /// Declares an integer parameter
  147. /** Here the number of integers specified in 'nparam' are expected after the
  148. * identifier and assigned to the array (or simple variable) pointed to by
  149. * 'param'. Floating-point numbers are rounded to the nearest integer. The
  150. * parameters should be in the range 'min'-'max', otherwise a PLIB error
  151. * results. e.g.
  152. * \code
  153. * npat 1000
  154. * \endcode
  155. */
  156. bool declareitem(xtring identifier,int* param,int min,int max,int nparam,
  157. int callback, xtring help = "");
  158. /// Declares a floating-point parameter
  159. /** Here the number of floating-point numbers specified in 'nparam' are expected
  160. * after the identifier and assigned to the array (or simple variable) pointed
  161. * to by 'param'. The parameters should be in the range 'min'-'max', otherwise
  162. * a PLIB error results. e.g.
  163. * \code
  164. * mtemp -14.2 -13.5 -9.5 -3.5 2.9 9.3 12.6 10.3 4.8 -1.5 -8.1 -12.1
  165. * \endcode
  166. */
  167. bool declareitem(xtring identifier,double* param,double min,double max,int nparam,
  168. int callback,xtring help = "");
  169. /// Declares a boolean parameter
  170. /** Here the number of numbers (nominally integers 0 or 1) specified in 'nparam'
  171. * are expected after the identifier and are reinterpreted as bools (0=false,
  172. * non-zero=true) and assigned to the array (or simple variable) pointed to by
  173. * 'param'. e.g.
  174. * \code
  175. * include 1
  176. * \endcode
  177. */
  178. bool declareitem(xtring identifier,bool* param,int nparam,int callback,
  179. xtring help = "");
  180. /// Declares a boolean parameter
  181. /** If this identifier is encountered, the variable pointed to by 'param' is
  182. * assigned the value true. e.g.
  183. * \code
  184. * exit
  185. * \endcode
  186. */
  187. bool declareitem(xtring identifier,bool* param,int callback,xtring help = "");
  188. /// Declares a set header
  189. /** Specifies the identifier and associated id-code for a set header. If the
  190. * specified identifier is encountered, it is interpreted as a set header and
  191. * should be followed by the set name as a string and an open parenthesis.
  192. * Function plib_declarations is then called, passing 'id' from this call to
  193. * declareitem and the set name encountered in the script. e.g.
  194. * \code
  195. * taxon "picea" (
  196. * \endcode
  197. */
  198. bool declareitem(xtring identifier,int id,int callback,xtring help = "");
  199. /// Call this if you want to be notified when the PLIB parsing is done
  200. /** Function callwhendone declares a callback code which, if non-zero, is passed in
  201. * a call to function plib_callback following input of the last recognised PLIB
  202. * item in the current set. This can provide the calling program with an
  203. * opportunity to query, via function itemparsed, which of the items declared for
  204. * this set were actually encountered in the script.
  205. */
  206. void callwhendone(int callback);
  207. /// Checks whether a certain item has been read in from the script
  208. /** Function itemparsed should be called from function plib_callback following
  209. * input of data for a particular set (see function callwhendone above). It
  210. * returns true if the string 'identifier' corresponds to one of the PLIB
  211. * commands declared by calls to function declareitem for this set, AND this
  212. * command was encountered in processing this set in the script. If 'identifier'
  213. * is not recognised, or was not encountered in the script, the return value is
  214. * false.
  215. */
  216. bool itemparsed(xtring identifier);
  217. /// Send a message to the user during parsing
  218. /** Function sendmessage may be called by the calling program to output warning or
  219. * informational messages to the user during processing of a PLIB script.
  220. * Typically sendmessage would be called from within function plib_callback.
  221. * Parameter 'heading' should consist of a keyword identifying the type of message
  222. * sent (e.g. "Warning"). More detailed information may be given within parameter
  223. * 'message'.
  224. */
  225. void sendmessage(xtring heading,xtring message);
  226. /// Gives the user documentation on all declared keywords
  227. /** Documentation of a calling module's native keywords (as defined in function
  228. * plib_declarations) is possible using the 'xtring help' parameter of function
  229. * declareitem. A list of all keywords and their associated help text (if
  230. * provided) is sent as sequential calls (one per line of output text) to function
  231. * plib_receivemessage, when function plibhelp is called from the calling program.
  232. */
  233. void plibhelp();
  234. /// Aborts the processing of a PLIB script
  235. /** This function is provided to allow processing of a PLIB script to be forcibly
  236. * aborted. Typically, plibabort would be called from within function
  237. * plib_callback.
  238. */
  239. void plibabort();
  240. // FUNCTIONS TO BE IMPLEMENTED BY THE CALLING PROGRAM
  241. /// This is where the calling program configures PLIB
  242. /** This function is called by PLIB just before interpretation of a script
  243. * begins, and subsequently whenever a set header is encountered. It should
  244. * include calls to the functions declareitem which define the identifiers and
  245. * formats for commands and sets in the script (or this set), and what action
  246. * should be taken when a particular command or set identifier is encountered.
  247. */
  248. void plib_declarations(int id,xtring setname);
  249. /// This function can be called by PLIB when certain commands are read
  250. /** Implement this function in the calling program to handle callbacks from
  251. * PLIB. To get a call when a command is read in, specify a non-zero
  252. * integer value as the callback parameter when declaring the item
  253. * with declareitem. This integer value is then sent into plib_callback
  254. * when the parameter is read.
  255. */
  256. void plib_callback(int callback);
  257. /// Called by PLIB whenever output should be conveyed to the user.
  258. /** Plib sends output in the form of an xtring (character string) object. It
  259. * is the responsibility of the calling program to send this string to an
  260. * output stream or device (typically the screen, or a text file). Output
  261. * generated by PLIB includes error messages, help text and messages
  262. * originating from the calling program and sent to PLIB via function
  263. * sendmessage.
  264. */
  265. void plib_receivemessage(xtring text);
  266. #endif // PLIB_H