PageRenderTime 46ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/harbour-3.0.0/src/rtl/hbregexc.c

#
C | 180 lines | 107 code | 22 blank | 51 comment | 17 complexity | 067c003c912e4883a4942f846a9c2f07 MD5 | raw file
Possible License(s): AGPL-1.0, BSD-3-Clause, CC-BY-SA-3.0, LGPL-3.0, GPL-2.0, LGPL-2.0, LGPL-2.1
  1. /*
  2. * $Id: hbregexc.c 15024 2010-07-06 21:32:46Z vszakats $
  3. */
  4. /*
  5. * Harbour Project source code:
  6. *
  7. *
  8. * Copyright 2007 Przemyslaw Czerpak <druzus / at / priv.onet.pl>
  9. * www - http://harbour-project.org
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2, or (at your option)
  14. * any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this software; see the file COPYING. If not, write to
  23. * the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  24. * Boston, MA 02111-1307 USA (or visit the web site http://www.gnu.org/).
  25. *
  26. * As a special exception, the Harbour Project gives permission for
  27. * additional uses of the text contained in its release of Harbour.
  28. *
  29. * The exception is that, if you link the Harbour libraries with other
  30. * files to produce an executable, this does not by itself cause the
  31. * resulting executable to be covered by the GNU General Public License.
  32. * Your use of that executable is in no way restricted on account of
  33. * linking the Harbour library code into it.
  34. *
  35. * This exception does not however invalidate any other reasons why
  36. * the executable file might be covered by the GNU General Public License.
  37. *
  38. * This exception applies only to the code released by the Harbour
  39. * Project under the name Harbour. If you copy code from other
  40. * Harbour Project or Free Software Foundation releases into a copy of
  41. * Harbour, as the General Public License permits, the exception does
  42. * not apply to the code that you add in this way. To avoid misleading
  43. * anyone as to the status of such modified files, you must delete
  44. * this exception notice from them.
  45. *
  46. * If you write modifications of your own for Harbour, it is your choice
  47. * whether to permit this exception to apply to your modifications.
  48. * If you do not wish that, delete this exception notice.
  49. *
  50. */
  51. #define _HB_REGEX_INTERNAL_
  52. #include "hbregex.h"
  53. #include "hbapiitm.h"
  54. #include "hbapierr.h"
  55. static void hb_regfree( PHB_REGEX pRegEx )
  56. {
  57. HB_SYMBOL_UNUSED( pRegEx );
  58. }
  59. static int hb_regcomp( PHB_REGEX pRegEx, const char * szRegEx )
  60. {
  61. HB_SYMBOL_UNUSED( pRegEx );
  62. HB_SYMBOL_UNUSED( szRegEx );
  63. return -1;
  64. }
  65. static int hb_regexec( PHB_REGEX pRegEx, const char * szString, HB_SIZE nLen,
  66. int iMatches, HB_REGMATCH * aMatches )
  67. {
  68. HB_SYMBOL_UNUSED( pRegEx );
  69. HB_SYMBOL_UNUSED( szString );
  70. HB_SYMBOL_UNUSED( nLen );
  71. HB_SYMBOL_UNUSED( iMatches );
  72. HB_SYMBOL_UNUSED( aMatches );
  73. return -1;
  74. }
  75. static HB_REG_FREE s_reg_free = hb_regfree;
  76. static HB_REG_COMP s_reg_comp = hb_regcomp;
  77. static HB_REG_EXEC s_reg_exec = hb_regexec;
  78. void hb_regexInit( HB_REG_FREE pFree, HB_REG_COMP pComp, HB_REG_EXEC pExec )
  79. {
  80. s_reg_free = pFree;
  81. s_reg_comp = pComp;
  82. s_reg_exec = pExec;
  83. }
  84. /* This releases regex when called from the garbage collector */
  85. static HB_GARBAGE_FUNC( hb_regexRelease )
  86. {
  87. ( s_reg_free )( ( PHB_REGEX ) Cargo );
  88. }
  89. static const HB_GC_FUNCS s_gcRegexFuncs =
  90. {
  91. hb_regexRelease,
  92. hb_gcDummyMark
  93. };
  94. HB_BOOL hb_regexIs( PHB_ITEM pItem )
  95. {
  96. return hb_itemGetPtrGC( pItem, &s_gcRegexFuncs ) != NULL;
  97. }
  98. PHB_REGEX hb_regexCompile( const char *szRegEx, HB_SIZE nLen, int iFlags )
  99. {
  100. PHB_REGEX pRegEx;
  101. HB_SYMBOL_UNUSED( nLen );
  102. pRegEx = ( PHB_REGEX ) hb_gcAllocate( sizeof( HB_REGEX ), &s_gcRegexFuncs );
  103. memset( pRegEx, 0, sizeof( HB_REGEX ) );
  104. pRegEx->fFree = HB_TRUE;
  105. pRegEx->iFlags = iFlags;
  106. if( ( s_reg_comp )( pRegEx, szRegEx ) != 0 )
  107. {
  108. hb_gcFree( pRegEx );
  109. pRegEx = NULL;
  110. }
  111. return pRegEx;
  112. }
  113. PHB_REGEX hb_regexGet( PHB_ITEM pRegExItm, int iFlags )
  114. {
  115. PHB_REGEX pRegEx = NULL;
  116. HB_BOOL fArgError = HB_TRUE;
  117. if( pRegExItm )
  118. {
  119. if( HB_IS_POINTER( pRegExItm ) )
  120. {
  121. pRegEx = ( PHB_REGEX ) hb_itemGetPtrGC( pRegExItm, &s_gcRegexFuncs );
  122. if( pRegEx )
  123. fArgError = HB_FALSE;
  124. }
  125. else if( HB_IS_STRING( pRegExItm ) )
  126. {
  127. HB_SIZE nLen = hb_itemGetCLen( pRegExItm );
  128. const char * szRegEx = hb_itemGetCPtr( pRegExItm );
  129. if( nLen > 0 )
  130. {
  131. fArgError = HB_FALSE;
  132. pRegEx = hb_regexCompile( szRegEx, nLen, iFlags );
  133. }
  134. }
  135. }
  136. if( fArgError )
  137. hb_errRT_BASE_SubstR( EG_ARG, 3012, NULL, HB_ERR_FUNCNAME, 1, pRegExItm );
  138. else if( ! pRegEx ) /* hb_regexCompile() failed */
  139. hb_errRT_BASE_SubstR( EG_ARG, 3015, NULL, HB_ERR_FUNCNAME, 1, pRegExItm );
  140. return pRegEx;
  141. }
  142. void hb_regexFree( PHB_REGEX pRegEx )
  143. {
  144. if( pRegEx && pRegEx->fFree )
  145. {
  146. ( s_reg_free )( pRegEx );
  147. hb_gcFree( pRegEx );
  148. }
  149. }
  150. HB_BOOL hb_regexMatch( PHB_REGEX pRegEx, const char *szString, HB_SIZE nLen, HB_BOOL fFull )
  151. {
  152. HB_REGMATCH aMatches[ HB_REGMATCH_SIZE( 1 ) ];
  153. HB_BOOL fMatch;
  154. fMatch = ( s_reg_exec )( pRegEx, szString, nLen, 1, aMatches ) > 0;
  155. return fMatch && ( !fFull ||
  156. ( HB_REGMATCH_SO( aMatches, 0 ) == 0 &&
  157. HB_REGMATCH_EO( aMatches, 0 ) == ( int ) nLen ) );
  158. }