forked from simsum/oscat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BUFFER_TO_STRING.EXP
executable file
·76 lines (58 loc) · 1.67 KB
/
BUFFER_TO_STRING.EXP
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
(* @NESTEDCOMMENTS := 'Yes' *)
(* @PATH := '\/Buffer Management' *)
(* @OBJECTFLAGS := '0, 8' *)
(* @SYMFILEFLAGS := '2048' *)
FUNCTION BUFFER_TO_STRING : STRING(STRING_LENGTH)
VAR_INPUT
PT : POINTER TO ARRAY[0..32767] OF BYTE;
SIZE : UINT;
START : UINT;
STOP : UINT;
END_VAR
VAR
ps : POINTER TO BYTE;
i : UINT;
stp: UINT;
sta: UINT;
END_VAR
(*
version 1.5 12. nov. 2009
programmer hugo
tested by oscat
this function will retrieve a string from an array of byte starting at position start and stop at position stop.
the function needs to be called: buffer_TO_String(adr("array"),sizeof("array"), start, stop);
because this function works with pointers its very time efficient and it needs no extra memory.
*)
(* @END_DECLARATION := '0' *)
ps := ADR(BUFFER_TO_STRING);
IF size = 0 THEN RETURN; END_IF;
sta := MIN(start, size -1);
stp := MIN(stop, size -1);
(* check for maximum string_length *)
IF UINT_TO_INT(stp - sta + 1) >= STRING_LENGTH THEN
stp := sta + INT_TO_UINT(STRING_LENGTH) - 1;
END_IF;
FOR i := sta TO stp DO
ps^ := pt^[i];
ps := ps + 1;
END_FOR;
(* terminate the string *)
ps^ := 0;
(* revision History
hm 5. mar. 2008 rev 1.0
original version
hm 16. mar. 2008 rev 1.1
changed type of input size to uint
hm 13. may. 2008 rev 1.2
changed type of pointer to array[0..32767]
changed size of string to STRING_LENGTH
hm 12. jun. 2008 rev 1.3
check for pointer overrun
change input start and stop to uint
added type conversions to avoid warnings under codesys 3.0
hm 23. mar. 2009 rev 1.4
avoid writing to input stop
hm 12. nov. 2009 rev 1.5
limit start and stop to size -1
*)
END_FUNCTION