PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/tags/beta1/harbour/source/rtl/hbregexc.c

#
C | 164 lines | 91 code | 21 blank | 52 comment | 13 complexity | a1a0e7333f6be11653eefb112fe1f6e5 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 7488 2007-05-31 13:20:46Z druzus $
  3. */
  4. /*
  5. * Harbour Project source code:
  6. *
  7. *
  8. * Copyright 2007 Przemyslaw Czerpak <druzus / at / priv.onet.pl>
  9. * www - http://www.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_PCRE_REGEX */
  52. #define _HB_REGEX_INTERNAL_
  53. #include "hbregex.h"
  54. #include "hbapiitm.h"
  55. #include "hbapierr.h"
  56. static void hb_regfree( PHB_REGEX pRegEx )
  57. {
  58. HB_SYMBOL_UNUSED( pRegEx );
  59. }
  60. static int hb_regcomp( PHB_REGEX pRegEx, const char * szRegEx )
  61. {
  62. HB_SYMBOL_UNUSED( pRegEx );
  63. HB_SYMBOL_UNUSED( szRegEx );
  64. return -1;
  65. }
  66. static int hb_regexec( PHB_REGEX pRegEx, const char * szString, ULONG ulLen,
  67. int iMatches, HB_REGMATCH * aMatches )
  68. {
  69. HB_SYMBOL_UNUSED( pRegEx );
  70. HB_SYMBOL_UNUSED( szString );
  71. HB_SYMBOL_UNUSED( ulLen );
  72. HB_SYMBOL_UNUSED( iMatches );
  73. HB_SYMBOL_UNUSED( aMatches );
  74. return -1;
  75. }
  76. static HB_REG_FREE s_reg_free = hb_regfree;
  77. static HB_REG_COMP s_reg_comp = hb_regcomp;
  78. static HB_REG_EXEC s_reg_exec = hb_regexec;
  79. void hb_regexInit( HB_REG_FREE pFree, HB_REG_COMP pComp, HB_REG_EXEC pExec )
  80. {
  81. s_reg_free = pFree;
  82. s_reg_comp = pComp;
  83. s_reg_exec = pExec;
  84. }
  85. /* This releases regex when called from the garbage collector */
  86. HB_GARBAGE_FUNC( hb_regexRelease )
  87. {
  88. ( s_reg_free )( ( PHB_REGEX ) Cargo );
  89. }
  90. PHB_REGEX hb_regexCompile( const char *szRegEx, ULONG ulLen, int iFlags )
  91. {
  92. PHB_REGEX pRegEx;
  93. HB_SYMBOL_UNUSED( ulLen );
  94. pRegEx = ( PHB_REGEX ) hb_gcAlloc( sizeof( HB_REGEX ), hb_regexRelease );
  95. hb_gcLock( pRegEx );
  96. memset( pRegEx, 0, sizeof( HB_REGEX ) );
  97. pRegEx->fFree = TRUE;
  98. pRegEx->iFlags = iFlags;
  99. if( ( s_reg_comp )( pRegEx, szRegEx ) != 0 )
  100. {
  101. hb_gcFree( pRegEx );
  102. pRegEx = NULL;
  103. }
  104. return pRegEx;
  105. }
  106. PHB_REGEX hb_regexGet( PHB_ITEM pRegExItm, int iFlags )
  107. {
  108. PHB_REGEX pRegEx = NULL;
  109. if( pRegExItm )
  110. {
  111. if( HB_IS_POINTER( pRegExItm ) )
  112. {
  113. pRegEx = ( PHB_REGEX ) hb_itemGetPtrGC( pRegExItm, hb_regexRelease );
  114. }
  115. else if( HB_IS_STRING( pRegExItm ) )
  116. {
  117. ULONG ulLen = hb_itemGetCLen( pRegExItm );
  118. char * szRegEx = hb_itemGetCPtr( pRegExItm );
  119. if( ulLen > 0 )
  120. pRegEx = hb_regexCompile( szRegEx, ulLen, iFlags );
  121. }
  122. }
  123. if( !pRegEx )
  124. hb_errRT_BASE_SubstR( EG_ARG, 3012, "Invalid Regular expression", &hb_errFuncName, 1, pRegExItm );
  125. return pRegEx;
  126. }
  127. void hb_regexFree( PHB_REGEX pRegEx )
  128. {
  129. if( pRegEx && pRegEx->fFree )
  130. {
  131. ( s_reg_free )( pRegEx );
  132. hb_gcFree( pRegEx );
  133. }
  134. }
  135. BOOL hb_regexMatch( PHB_REGEX pRegEx, const char *szString, ULONG ulLen, BOOL fFull )
  136. {
  137. HB_REGMATCH aMatches[ HB_REGMATCH_SIZE( 1 ) ];
  138. BOOL fMatch;
  139. fMatch = ( s_reg_exec )( pRegEx, szString, ulLen, 1, aMatches ) > 0;
  140. return fMatch && ( !fFull ||
  141. ( HB_REGMATCH_SO( aMatches, 0 ) == 0 &&
  142. HB_REGMATCH_EO( aMatches, 0 ) == ( int ) ulLen ) );
  143. }