fld_newftyp.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /****************************************************************************
  2. * Copyright (c) 1998-2004,2007 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, 1995,1997 *
  30. ****************************************************************************/
  31. #include "form.priv.h"
  32. MODULE_ID("$Id: fld_newftyp.c,v 1.15 2007/10/13 19:30:55 tom Exp $")
  33. static FIELDTYPE const default_fieldtype =
  34. {
  35. 0, /* status */
  36. 0L, /* reference count */
  37. (FIELDTYPE *)0, /* pointer to left operand */
  38. (FIELDTYPE *)0, /* pointer to right operand */
  39. NULL, /* makearg function */
  40. NULL, /* copyarg function */
  41. NULL, /* freearg function */
  42. NULL, /* field validation function */
  43. NULL, /* Character check function */
  44. NULL, /* enumerate next function */
  45. NULL /* enumerate previous function */
  46. };
  47. NCURSES_EXPORT_VAR(const FIELDTYPE *)
  48. _nc_Default_FieldType = &default_fieldtype;
  49. /*---------------------------------------------------------------------------
  50. | Facility : libnform
  51. | Function : FIELDTYPE *new_fieldtype(
  52. | bool (* const field_check)(FIELD *,const void *),
  53. | bool (* const char_check) (int, const void *) )
  54. |
  55. | Description : Create a new fieldtype. The application programmer must
  56. | write a field_check and a char_check function and give
  57. | them as input to this call.
  58. | If an error occurs, errno is set to
  59. | E_BAD_ARGUMENT - invalid arguments
  60. | E_SYSTEM_ERROR - system error (no memory)
  61. |
  62. | Return Values : Fieldtype pointer or NULL if error occurred
  63. +--------------------------------------------------------------------------*/
  64. NCURSES_EXPORT(FIELDTYPE *)
  65. new_fieldtype(bool (*const field_check) (FIELD *, const void *),
  66. bool (*const char_check) (int, const void *))
  67. {
  68. FIELDTYPE *nftyp = (FIELDTYPE *)0;
  69. T((T_CALLED("new_fieldtype(%p,%p)"), field_check, char_check));
  70. if ((field_check) || (char_check))
  71. {
  72. nftyp = typeMalloc(FIELDTYPE, 1);
  73. if (nftyp)
  74. {
  75. T((T_CREATE("fieldtype %p"), nftyp));
  76. *nftyp = default_fieldtype;
  77. nftyp->fcheck = field_check;
  78. nftyp->ccheck = char_check;
  79. }
  80. else
  81. {
  82. SET_ERROR(E_SYSTEM_ERROR);
  83. }
  84. }
  85. else
  86. {
  87. SET_ERROR(E_BAD_ARGUMENT);
  88. }
  89. returnFieldType(nftyp);
  90. }
  91. /*---------------------------------------------------------------------------
  92. | Facility : libnform
  93. | Function : int free_fieldtype(FIELDTYPE *typ)
  94. |
  95. | Description : Release the memory associated with this fieldtype.
  96. |
  97. | Return Values : E_OK - success
  98. | E_CONNECTED - there are fields referencing the type
  99. | E_BAD_ARGUMENT - invalid fieldtype pointer
  100. +--------------------------------------------------------------------------*/
  101. NCURSES_EXPORT(int)
  102. free_fieldtype(FIELDTYPE *typ)
  103. {
  104. T((T_CALLED("free_fieldtype(%p)"), typ));
  105. if (!typ)
  106. RETURN(E_BAD_ARGUMENT);
  107. if (typ->ref != 0)
  108. RETURN(E_CONNECTED);
  109. if (typ->status & _RESIDENT)
  110. RETURN(E_CONNECTED);
  111. if (typ->status & _LINKED_TYPE)
  112. {
  113. if (typ->left)
  114. typ->left->ref--;
  115. if (typ->right)
  116. typ->right->ref--;
  117. }
  118. free(typ);
  119. RETURN(E_OK);
  120. }
  121. /* fld_newftyp.c ends here */