fld_newftyp.c 5.4 KB

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