-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgetpos.c
More file actions
136 lines (111 loc) · 2.73 KB
/
getpos.c
File metadata and controls
136 lines (111 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/* -MODULE----------------------------------------------------------------------
RapidBATCH
Copyright (C) 2009 by Phorward Software Technologies
http://www.phorward-software.com ++ contact<at>phorward<dash>software<dot>com
File: getpos.c
Author: Andreas Harbecke
Usage: Implements the function GETPOS.
----------------------------------------------------------------------------- */
/*
* Includes
*/
#include "rb_global.h"
/*
* Global variables
*/
extern BOOLEAN REGEX_ENABLED;
extern BOOLEAN CASE_INSENSITIVITY;
/*
* Functions
*/
/*RBDOC
%%function
getpos( string, find, position )
%%desc:en
Description of getpos
%%desc:ge
Beschreibung von getpos
%%parm:en
string
find
position
%%parm:ge
string
find
position
%%return:en
Return value of getpos
%%return:ge
Rückgabewert von getpos
%%notice:en
%%notice:ge
RBDOC*/
RB_FCT( getpos )
{
/*RBAUTOIMPORT function getpos vvv*/
char* sourceString;
char* targetString;
char* tempString;
char* ptr;
unsigned long position;
unsigned long offset = 0;
int preg_mod = PREGEX_COMP_NOERRORS;
int matches_cnt;
parray* matches;
prange* pr;
PROC( "getpos" );
RB_FCT_DUMP_PARMS();
sourceString = RB_PARM_VAL_GET_STR( RB_FCT_PARM_ACCESS( 0 ) );
VARS( "sourceString", "%s", sourceString );
targetString = RB_PARM_VAL_GET_STR( RB_FCT_PARM_ACCESS( 1 ) );
VARS( "targetString", "%s", targetString );
position = RB_PARM_VAL_GET_ULONG( RB_FCT_PARM_ACCESS( 2 ) );
VARS( "position", "%ld", position);
VARS( "CASE_INSENSITIVITIY", "%d", CASE_INSENSITIVITY );
if( CASE_INSENSITIVITY )
preg_mod |= PREGEX_COMP_INSENSITIVE;
VARS( "REGEX_ENABLED", "%d", REGEX_ENABLED );
if( !REGEX_ENABLED )
preg_mod |= PREGEX_COMP_STATIC;
/* Perform direct string search if no regex-features are required */
if( !CASE_INSENSITIVITY && !REGEX_ENABLED )
{
for( tempString = sourceString - 1;
position > 0 && tempString; position-- )
tempString = strstr( ++tempString, targetString );
if( tempString )
{
for( ptr = sourceString; ptr < tempString;
ptr += putf8_seqlen( ptr ) )
offset++;
}
}
/* else, use a regular expression */
else
{
VARS( "preg_mod", "%d", preg_mod );
if( ( matches_cnt = pregex_qmatch( sourceString, targetString,
preg_mod, &matches ) ) > 0 )
{
MSG( "Matches found" );
VARS( "matches_cnt", "%d", matches_cnt );
if( position <= matches_cnt )
{
pr = (prange*)parray_get( matches, position - 1 );
offset = pr->start - targetString;
}
parray_free( matches );
}
else if( matches_cnt < 0 )
{
/* TODO */
MSG( "Error in regex-handling" );
}
}
VARS( "offset", "%ld", offset );
if( offset )
RB_PARM_VAL_SET_LONG( RB_FCT_RET, offset + 1 );
else
RB_PARM_VAL_SET_LONG( RB_FCT_RET, 0 );
RETURN( 0 );
}