fld_def.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /****************************************************************************
  2. * Copyright (c) 1998 Free Software Foundation, Inc. *
  3. * *
  4. * Permission is hereby granted, free of charge, to any person obtaining a *
  5. * copy of this software and associated documentation files (the *
  6. * "Software"), to deal in the Software without restriction, including *
  7. * without limitation the rights to use, copy, modify, merge, publish, *
  8. * distribute, distribute with modifications, sublicense, and/or sell *
  9. * copies of the Software, and to permit persons to whom the Software is *
  10. * furnished to do so, subject to the following conditions: *
  11. * *
  12. * The above copyright notice and this permission notice shall be included *
  13. * in all copies or substantial portions of the Software. *
  14. * *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
  16. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
  18. * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
  19. * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
  20. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
  21. * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
  22. * *
  23. * Except as contained in this notice, the name(s) of the above copyright *
  24. * holders shall not be used in advertising or otherwise to promote the *
  25. * sale, use or other dealings in this Software without prior written *
  26. * authorization. *
  27. ****************************************************************************/
  28. /****************************************************************************
  29. * Author: Juergen Pfeifer <juergen.pfeifer@gmx.net> 1995,1997 *
  30. ****************************************************************************/
  31. #include "form.priv.h"
  32. MODULE_ID("$Id$")
  33. /* this can't be readonly */
  34. static FIELD default_field = {
  35. 0, /* status */
  36. 0, /* rows */
  37. 0, /* cols */
  38. 0, /* frow */
  39. 0, /* fcol */
  40. 0, /* drows */
  41. 0, /* dcols */
  42. 0, /* maxgrow*/
  43. 0, /* nrow */
  44. 0, /* nbuf */
  45. NO_JUSTIFICATION, /* just */
  46. 0, /* page */
  47. 0, /* index */
  48. (int)' ', /* pad */
  49. A_NORMAL, /* fore */
  50. A_NORMAL, /* back */
  51. ALL_FIELD_OPTS, /* opts */
  52. (FIELD *)0, /* snext */
  53. (FIELD *)0, /* sprev */
  54. (FIELD *)0, /* link */
  55. (FORM *)0, /* form */
  56. (FIELDTYPE *)0, /* type */
  57. (char *)0, /* arg */
  58. (char *)0, /* buf */
  59. (char *)0 /* usrptr */
  60. };
  61. FIELD *_nc_Default_Field = &default_field;
  62. /*---------------------------------------------------------------------------
  63. | Facility : libnform
  64. | Function : TypeArgument *_nc_Make_Argument(
  65. | const FIELDTYPE *typ,
  66. | va_list *ap,
  67. | int *err )
  68. |
  69. | Description : Create an argument structure for the specified type.
  70. | Use the type-dependent argument list to construct
  71. | it.
  72. |
  73. | Return Values : Pointer to argument structure. Maybe NULL.
  74. | In case of an error in *err an errorcounter is increased.
  75. +--------------------------------------------------------------------------*/
  76. TypeArgument*
  77. _nc_Make_Argument(const FIELDTYPE *typ, va_list *ap, int *err)
  78. {
  79. TypeArgument *res = (TypeArgument *)0;
  80. TypeArgument *p;
  81. if (typ && (typ->status & _HAS_ARGS))
  82. {
  83. assert(err && ap);
  84. if (typ->status & _LINKED_TYPE)
  85. {
  86. p = (TypeArgument *)malloc(sizeof(TypeArgument));
  87. if (p)
  88. {
  89. p->left = _nc_Make_Argument(typ->left ,ap,err);
  90. p->right = _nc_Make_Argument(typ->right,ap,err);
  91. return p;
  92. }
  93. else
  94. *err += 1;
  95. } else
  96. {
  97. assert(typ->makearg != 0);
  98. if ( !(res=(TypeArgument *)typ->makearg(ap)) )
  99. *err += 1;
  100. }
  101. }
  102. return res;
  103. }
  104. /*---------------------------------------------------------------------------
  105. | Facility : libnform
  106. | Function : TypeArgument *_nc_Copy_Argument(const FIELDTYPE *typ,
  107. | const TypeArgument *argp,
  108. | int *err )
  109. |
  110. | Description : Create a copy of an argument structure for the specified
  111. | type.
  112. |
  113. | Return Values : Pointer to argument structure. Maybe NULL.
  114. | In case of an error in *err an errorcounter is increased.
  115. +--------------------------------------------------------------------------*/
  116. TypeArgument*
  117. _nc_Copy_Argument(const FIELDTYPE *typ,
  118. const TypeArgument *argp, int *err)
  119. {
  120. TypeArgument *res = (TypeArgument *)0;
  121. TypeArgument *p;
  122. if ( typ && (typ->status & _HAS_ARGS) )
  123. {
  124. assert(err && argp);
  125. if (typ->status & _LINKED_TYPE)
  126. {
  127. p = (TypeArgument *)malloc(sizeof(TypeArgument));
  128. if (p)
  129. {
  130. p->left = _nc_Copy_Argument(typ,argp->left ,err);
  131. p->right = _nc_Copy_Argument(typ,argp->right,err);
  132. return p;
  133. }
  134. *err += 1;
  135. }
  136. else
  137. {
  138. if (typ->copyarg)
  139. {
  140. if (!(res = (TypeArgument *)(typ->copyarg((const void *)argp))))
  141. *err += 1;
  142. }
  143. else
  144. res = (TypeArgument *)argp;
  145. }
  146. }
  147. return res;
  148. }
  149. /*---------------------------------------------------------------------------
  150. | Facility : libnform
  151. | Function : void _nc_Free_Argument(const FIELDTYPE *typ,
  152. | TypeArgument * argp )
  153. |
  154. | Description : Release memory associated with the argument structure
  155. | for the given fieldtype.
  156. |
  157. | Return Values : -
  158. +--------------------------------------------------------------------------*/
  159. void
  160. _nc_Free_Argument(const FIELDTYPE * typ, TypeArgument * argp)
  161. {
  162. if (!typ || !(typ->status & _HAS_ARGS))
  163. return;
  164. if (typ->status & _LINKED_TYPE)
  165. {
  166. assert(argp != 0);
  167. _nc_Free_Argument(typ->left ,argp->left );
  168. _nc_Free_Argument(typ->right,argp->right);
  169. free(argp);
  170. }
  171. else
  172. {
  173. if (typ->freearg)
  174. typ->freearg((void *)argp);
  175. }
  176. }
  177. /*---------------------------------------------------------------------------
  178. | Facility : libnform
  179. | Function : bool _nc_Copy_Type( FIELD *dst, FIELD const *src )
  180. |
  181. | Description : Copy argument structure of field src to field dst
  182. |
  183. | Return Values : TRUE - copy worked
  184. | FALSE - error occurred
  185. +--------------------------------------------------------------------------*/
  186. bool
  187. _nc_Copy_Type(FIELD *dst, FIELD const *src)
  188. {
  189. int err = 0;
  190. assert(dst && src);
  191. dst->type = src->type;
  192. dst->arg = (void *)_nc_Copy_Argument(src->type,(TypeArgument *)(src->arg),&err);
  193. if (err)
  194. {
  195. _nc_Free_Argument(dst->type,(TypeArgument *)(dst->arg));
  196. dst->type = (FIELDTYPE *)0;
  197. dst->arg = (void *)0;
  198. return FALSE;
  199. }
  200. else
  201. {
  202. if (dst->type)
  203. dst->type->ref++;
  204. return TRUE;
  205. }
  206. }
  207. /*---------------------------------------------------------------------------
  208. | Facility : libnform
  209. | Function : void _nc_Free_Type( FIELD *field )
  210. |
  211. | Description : Release Argument structure for this field
  212. |
  213. | Return Values : -
  214. +--------------------------------------------------------------------------*/
  215. void
  216. _nc_Free_Type(FIELD *field)
  217. {
  218. assert(field != 0);
  219. if (field->type)
  220. field->type->ref--;
  221. _nc_Free_Argument(field->type,(TypeArgument *)(field->arg));
  222. }
  223. /*---------------------------------------------------------------------------
  224. | Facility : libnform
  225. | Function : FIELD *new_field( int rows, int cols,
  226. | int frow, int fcol,
  227. | int nrow, int nbuf )
  228. |
  229. | Description : Create a new field with this many 'rows' and 'cols',
  230. | starting at 'frow/fcol' in the subwindow of the form.
  231. | Allocate 'nrow' off-screen rows and 'nbuf' additional
  232. | buffers. If an error occurs, errno is set to
  233. |
  234. | E_BAD_ARGUMENT - invalid argument
  235. | E_SYSTEM_ERROR - system error
  236. |
  237. | Return Values : Pointer to the new field or NULL if failure.
  238. +--------------------------------------------------------------------------*/
  239. FIELD *new_field(int rows, int cols, int frow, int fcol, int nrow, int nbuf)
  240. {
  241. FIELD *New_Field = (FIELD *)0;
  242. int err = E_BAD_ARGUMENT;
  243. if (rows>0 &&
  244. cols>0 &&
  245. frow>=0 &&
  246. fcol>=0 &&
  247. nrow>=0 &&
  248. nbuf>=0 &&
  249. ((err = E_SYSTEM_ERROR) != 0) && /* trick: this resets the default error */
  250. (New_Field=(FIELD *)malloc(sizeof(FIELD))) )
  251. {
  252. *New_Field = default_field;
  253. New_Field->rows = rows;
  254. New_Field->cols = cols;
  255. New_Field->drows = rows + nrow;
  256. New_Field->dcols = cols;
  257. New_Field->frow = frow;
  258. New_Field->fcol = fcol;
  259. New_Field->nrow = nrow;
  260. New_Field->nbuf = nbuf;
  261. New_Field->link = New_Field;
  262. if (_nc_Copy_Type(New_Field,&default_field))
  263. {
  264. size_t len;
  265. len = Total_Buffer_Size(New_Field);
  266. if ((New_Field->buf = (char *)malloc(len)))
  267. {
  268. /* Prefill buffers with blanks and insert terminating zeroes
  269. between buffers */
  270. int i;
  271. memset(New_Field->buf,' ',len);
  272. for(i=0;i<=New_Field->nbuf;i++)
  273. {
  274. New_Field->buf[(New_Field->drows*New_Field->cols+1)*(i+1)-1]
  275. = '\0';
  276. }
  277. return New_Field;
  278. }
  279. }
  280. }
  281. if (New_Field)
  282. free_field(New_Field);
  283. SET_ERROR( err );
  284. return (FIELD *)0;
  285. }
  286. /*---------------------------------------------------------------------------
  287. | Facility : libnform
  288. | Function : int free_field( FIELD *field )
  289. |
  290. | Description : Frees the storage allocated for the field.
  291. |
  292. | Return Values : E_OK - success
  293. | E_BAD_ARGUMENT - invalid field pointer
  294. | E_CONNECTED - field is connected
  295. +--------------------------------------------------------------------------*/
  296. int free_field(FIELD * field)
  297. {
  298. if (!field)
  299. RETURN(E_BAD_ARGUMENT);
  300. if (field->form)
  301. RETURN(E_CONNECTED);
  302. if (field == field->link)
  303. {
  304. if (field->buf)
  305. free(field->buf);
  306. }
  307. else
  308. {
  309. FIELD *f;
  310. for(f=field;f->link != field;f = f->link)
  311. {}
  312. f->link = field->link;
  313. }
  314. _nc_Free_Type(field);
  315. free(field);
  316. RETURN(E_OK);
  317. }
  318. /* fld_def.c ends here */