PageRenderTime 49ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

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

#
C | 172 lines | 99 code | 22 blank | 51 comment | 14 complexity | 8480dcbba289d8c74a4a9238cd196923 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 12753 2009-10-23 07:25:36Z 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_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, ULONG ulLen,
  66. int iMatches, HB_REGMATCH * aMatches )
  67. {
  68. HB_SYMBOL_UNUSED( pRegEx );
  69. HB_SYMBOL_UNUSED( szString );
  70. HB_SYMBOL_UNUSED( ulLen );
  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. BOOL hb_regexIs( PHB_ITEM pItem )
  95. {
  96. return hb_itemGetPtrGC( pItem, &s_gcRegexFuncs ) != NULL;
  97. }
  98. PHB_REGEX hb_regexCompile( const char *szRegEx, ULONG ulLen, int iFlags )
  99. {
  100. PHB_REGEX pRegEx;
  101. HB_SYMBOL_UNUSED( ulLen );
  102. pRegEx = ( PHB_REGEX ) hb_gcAllocate( sizeof( HB_REGEX ), &s_gcRegexFuncs );
  103. memset( pRegEx, 0, sizeof( HB_REGEX ) );
  104. pRegEx->fFree = 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. if( pRegExItm )
  117. {
  118. if( HB_IS_POINTER( pRegExItm ) )
  119. {
  120. pRegEx = ( PHB_REGEX ) hb_itemGetPtrGC( pRegExItm, &s_gcRegexFuncs );
  121. }
  122. else if( HB_IS_STRING( pRegExItm ) )
  123. {
  124. ULONG ulLen = hb_itemGetCLen( pRegExItm );
  125. const char * szRegEx = hb_itemGetCPtr( pRegExItm );
  126. if( ulLen > 0 )
  127. pRegEx = hb_regexCompile( szRegEx, ulLen, iFlags );
  128. }
  129. }
  130. if( !pRegEx )
  131. hb_errRT_BASE_SubstR( EG_ARG, 3012, "Invalid Regular expression", HB_ERR_FUNCNAME, 1, pRegExItm );
  132. return pRegEx;
  133. }
  134. void hb_regexFree( PHB_REGEX pRegEx )
  135. {
  136. if( pRegEx && pRegEx->fFree )
  137. {
  138. ( s_reg_free )( pRegEx );
  139. hb_gcFree( pRegEx );
  140. }
  141. }
  142. BOOL hb_regexMatch( PHB_REGEX pRegEx, const char *szString, ULONG ulLen, BOOL fFull )
  143. {
  144. HB_REGMATCH aMatches[ HB_REGMATCH_SIZE( 1 ) ];
  145. BOOL fMatch;
  146. fMatch = ( s_reg_exec )( pRegEx, szString, ulLen, 1, aMatches ) > 0;
  147. return fMatch && ( !fFull ||
  148. ( HB_REGMATCH_SO( aMatches, 0 ) == 0 &&
  149. HB_REGMATCH_EO( aMatches, 0 ) == ( int ) ulLen ) );
  150. }