-
Notifications
You must be signed in to change notification settings - Fork 11
/
dev_lpt_test.bas
150 lines (120 loc) · 2.87 KB
/
dev_lpt_test.bas
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/' LPTx device '/
#include "fb.bi"
#include "crt\ctype.bi"
/' Tests for the right file name for LPT access.
*
* Allowed file names are:
*
* - PRN:
* - LPT:
* - LPTx: with x>=1
* - LPT:printer_name,EMU=?,TITLE=?,OPT=?, ...
'/
extern "C"
function fb_DevLptParseProtocol( lpt_proto_out as DEV_LPT_PROTOCOL ptr ptr, proto_raw as const ubyte ptr, proto_raw_len as size_t, subst_prn as long ) as long
dim as ubyte ptr p, ptail, pc, pe
dim as DEV_LPT_PROTOCOL ptr lpt_proto
if ( proto_raw = NULL ) then
return FALSE
end if
if ( lpt_proto_out = NULL ) then
return FALSE
end if
*lpt_proto_out = calloc( sizeof( DEV_LPT_PROTOCOL ) + proto_raw_len + 2, 1 )
lpt_proto = *lpt_proto_out
if ( lpt_proto = NULL ) then
return FALSE
end if
strncpy( lpt_proto->raw, proto_raw, proto_raw_len )
lpt_proto->raw[proto_raw_len] = 0
p = lpt_proto->raw
ptail = p + strlen( lpt_proto->raw )
lpt_proto->iPort = 0
lpt_proto->proto = ptail
lpt_proto->name = ptail
lpt_proto->title = ptail
lpt_proto->emu = ptail
/' "PRN:" '/
if ( strcasecmp( p, "PRN:" ) = 0) then
if ( subst_prn <> 0 ) then
strcpy( p, "LPT1:" )
end if
lpt_proto->proto = p
lpt_proto->iPort = 1
return TRUE
end if
/' "LPTx:" '/
if ( strncasecmp( p, "LPT", 3) <> 0) then
return FALSE
end if
pc = strchr( p, asc(":") )
if ( pc = 0 ) then
return FALSE
end if
lpt_proto->proto = p
p = pc + 1
*pc = 0
pc -= 1
/' Get port number if any '/
while ( *pc >= asc("0") and *pc <= asc("9") )
pc -= 1
wend
pc += 1
lpt_proto->iPort = atoi( pc )
/' Name, TITLE=?, EMU=? '/
while( *p )
if ( isspace( *p ) <> 0 orelse *p = asc(",") ) then
p += 1
else
dim as ubyte ptr pt
pe = strchr(p, asc("="))
pc = strchr(p, asc(","))
if ( pc <> 0 andalso pe > pc ) then
pe = NULL
end if
if ( pe = 0 ) then
lpt_proto->name = p
else
/' remove spaces before '=' '/
pt = pe - 1
while( isspace( *pt ) <> 0 )
*pt = 0
pt -= 1
wend
/' remove spaces after '=' or end '/
pe[1] = 0
while( isspace( *pe ) <> 0 )
*pe = 0
pe += 1
wend
if( strcasecmp( p, "EMU" ) = 0) then
lpt_proto->emu = pe
elseif ( strcasecmp( p, "TITLE" ) = 0) then
lpt_proto->title = pe
end if
/' just ignore options we don't understand to allow forward compatibility '/
end if
/' remove spaces before ',' or end '/
pt = iif(pc <> 0, pc, ptail)
pt -= 1
while( isspace( *pt ) <> 0 )
*pt = 0
pt -= 1
wend
if ( pc <> 0 ) then
p = pc + 1
*pc = 0
else
p = ptail
end if
end if
wend
return TRUE
end function
function fb_DevLptTestProtocol( handle as FB_FILE ptr, filename as const ubyte ptr, filename_len as size_t ) as long
dim as DEV_LPT_PROTOCOL ptr lpt_proto
dim as long ret = fb_DevLptParseProtocol( @lpt_proto, filename, filename_len, FALSE )
free( lpt_proto )
return ret
end function
end extern