FreeBASIC  0.91.0
hlp.bas
Go to the documentation of this file.
1 '' misc helpers
2 ''
3 ''
4 
5 
6 #include once "fb.bi"
7 #include once "fbint.bi"
8 #include once "ir.bi"
9 #include once "lex.bi"
10 #include once "dstr.bi"
11 
12 '':::::
13 function hMatchText _
14  ( _
15  byval txt as zstring ptr _
16  ) as integer
17 
18  if( ucase( *lexGetText( ) ) = *txt ) then
19  lexSkipToken( )
20  function = TRUE
21  else
22  function = FALSE
23  end if
24 
25 end function
26 
27 '':::::
28 function hMatch _
29  ( _
30  byval token as integer _
31  ) as integer
32 
33  if( lexGetToken( ) = token ) then
34  lexSkipToken( )
35  function = TRUE
36  else
37  function = FALSE
38  end if
39 
40 end function
41 
42 '':::::
43 function hHexUInt _
44  ( _
45  byval value as uinteger _
46  ) as zstring ptr static
47 
48  static as zstring * 8 + 1 res
49  dim as zstring ptr p
50  dim as integer lgt, maxlen
51 
52  static as integer hexTB(0 to 15) = _
53  { _
54  asc( "0" ), asc( "1" ), asc( "2" ), asc( "3" ), _
55  asc( "4" ), asc( "5" ), asc( "6" ), asc( "7" ), _
56  asc( "8" ), asc( "9" ), asc( "A" ), asc( "B" ), _
57  asc( "C" ), asc( "D" ), asc( "E" ), asc( "F" ) _
58  }
59 
60  maxlen = 4
61  if( value > 65535 ) then
62  maxlen = 8
63  end if
64 
65  p = @res + 8-1
66  lgt = 0
67 
68  do
69  *p = hexTB( value and &h0000000F )
70 
71  lgt +=1
72  if( lgt = maxlen ) then
73  exit do
74  end if
75 
76  p -= 1
77  value shr= 4
78  loop
79 
80  function = p
81 
82 end function
83 
84 function hFloatToHex _
85  ( _
86  byval value as double, _
87  byval dtype as integer _
88  ) as string
89 
90  '' Emit the raw bytes that make up the float
91  '' x86 little-endian assumption
92  if( typeGet( dtype ) = FB_DATATYPE_DOUBLE ) then
93  function = "0x" + hex( *cptr( ulongint ptr, @value ), 16 )
94  else
95  dim as single singlevalue = value
96  '' Using an intermediate uinteger to allow compiling with FB
97  '' versions before the overload resolution overhaul
98  function = "0x" + hex( cuint( *cptr( ulong ptr, @singlevalue ) ), 8 )
99  end if
100 end function
101 
102 '':::::
103 function hFBrelop2IRrelop _
104  ( _
105  byval tk as integer _
106  ) as integer static
107 
108  dim as integer op = any
109 
110  select case as const tk
111  case FB_TK_EQ
112  op = AST_OP_EQ
113  case FB_TK_GT
114  op = AST_OP_GT
115  case FB_TK_LT
116  op = AST_OP_LT
117  case FB_TK_NE
118  op = AST_OP_NE
119  case FB_TK_LE
120  op = AST_OP_LE
121  case FB_TK_GE
122  op = AST_OP_GE
123  case else
124  errReport( FB_ERRMSG_EXPECTEDRELOP )
125  '' error recovery: fake an op
126  op = AST_OP_EQ
127  end select
128 
129  function = op
130 
131 end function
132 
133 '':::::
134 function hFileExists _
135  ( _
136  byval filename as zstring ptr _
137  ) as integer static
138  dim f as integer
139 
140  f = freefile
141 
142  if( open( *filename, for input, as #f ) = 0 ) then
143  function = TRUE
144  close #f
145  else
146  function = FALSE
147  end if
148 
149 end function
150 
151 '':::::
152 sub hUcase _
153  ( _
154  byval src as const zstring ptr, _
155  byval dst as zstring ptr _
156  ) static
157 
158  dim as integer c
159  dim as const zstring ptr s
160  dim as zstring ptr d
161 
162  s = src
163  d = dst
164 
165  do
166  c = *s
167  if( c >= 97 ) then
168  if( c <= 122 ) then
169  c -= (97 - 65)
170  end if
171  elseif( c = 0 ) then
172  exit do
173  end if
174 
175  *d = c
176 
177  s += 1
178  d += 1
179  loop
180 
181  '' null-term
182  *d = 0
183 
184 end sub
185 
186 '':::::
187 sub hClearName _
188  ( _
189  byval src as zstring ptr _
190  ) static
191 
192  dim as zstring ptr p
193 
194  p = src
195 
196  do
197  select case as const *p
198  case 0
199  exit do
200 
201  case CHAR_AUPP to CHAR_ZUPP, CHAR_ALOW to CHAR_ZLOW, CHAR_0 to CHAR_9, CHAR_UNDER
202 
203  case else
204  *p = CHAR_ZLOW
205  end select
206 
207  p += 1
208  loop
209 
210 end sub
211 
212 '':::::
213 function hStripExt _
214  ( _
215  byval filename as zstring ptr _
216  ) as string static
217 
218  dim as integer p, lp
219 
220  lp = 0
221  do
222  p = instr( lp+1, *filename, "." )
223  if( p = 0 ) then
224  exit do
225  end if
226  lp = p
227  loop
228 
229  if( lp > 0 ) then
230  function = left( *filename, lp-1 )
231  else
232  function = *filename
233  end if
234 
235 end function
236 
237 '':::::
238 function hStripPath _
239  ( _
240  byval filename as zstring ptr _
241  ) as string static
242 
243  dim as integer lp, p_found, p(1 to 2)
244 
245  lp = 0
246  do
247  p(1) = instr( lp+1, *filename, RSLASH )
248  p(2) = instr( lp+1, *filename, "/" )
249  if p(1)=0 or (p(2)>0 and p(2)<p(1)) then
250  p_found = p(2)
251  else
252  p_found = p(1)
253  end if
254  if( p_found = 0 ) then
255  exit do
256  end if
257  lp = p_found
258  loop
259 
260  if( lp > 0 ) then
261  function = mid( *filename, lp+1 )
262  else
263  function = *filename
264  end if
265 
266 end function
267 
268 '':::::
269 function hStripFilename _
270  ( _
271  byval filename as zstring ptr _
272  ) as string static
273 
274  dim as integer lp, p_found, p(1 to 2)
275 
276  lp = 0
277  do
278  p(1) = instr( lp+1, *filename, RSLASH )
279  p(2) = instr( lp+1, *filename, "/" )
280  if p(1)=0 or (p(2)>0 and p(2)<p(1)) then
281  p_found = p(2)
282  else
283  p_found = p(1)
284  end if
285  if( p_found = 0 ) then
286  exit do
287  end if
288  lp = p_found
289  loop
290 
291  if( lp > 0 ) then
292  function = left( *filename, lp )
293  else
294  function = ""
295  end if
296 
297 end function
298 
299 '':::::
300 function hGetFileExt _
301  ( _
302  byval fname as zstring ptr _
303  ) as string static
304 
305  dim as integer p, lp
306  dim as string res
307 
308  lp = 0
309  do
310  p = instr( lp+1, *fname, "." )
311  if( p = 0 ) then
312  exit do
313  end if
314  lp = p
315  loop
316 
317  if( lp = 0 ) then
318  function = ""
319  else
320  res = lcase( mid( *fname, lp+1 ) )
321  if instr( res, RSLASH ) > 0 or instr( res, "/" ) > 0 then
322  '' We had a folder with a "." inside ...
323  function = ""
324  elseif( len(res) > 0 ) then
325  '' . or .. dirs?
326  if( res[0] = asc( RSLASH ) or res[0] = asc( "/" ) ) then
327  function = ""
328  else
329  function = res
330  end if
331  end if
332  end if
333 
334 end function
335 
336 sub hReplaceSlash( byval s as zstring ptr, byval char as integer )
337  for i as integer = 0 to len( *s ) - 1
338  if( (s[i] = CHAR_RSLASH) or (s[i] = CHAR_SLASH) ) then
339  s[i] = char
340  end if
341  next
342 end sub
343 
344 function pathStripDiv( byref path as string ) as string
345  dim as integer length = len( path )
346  if( length > 0 ) then
347  length -= 1
348  select case( path[length] )
349 #if defined( __FB_WIN32__ ) or defined( __FB_DOS__ )
350  case asc("/"), asc("\")
351 #else
352  case asc("/")
353 #endif
354  return left( path, length )
355  end select
356  end if
357  function = path
358 end function
359 
360 function pathIsAbsolute( byval path as zstring ptr ) as integer
361 #if defined( __FB_WIN32__ ) or defined( __FB_DOS__ )
362  if( (*path)[0] <> 0 ) then
363  select case( (*path)[1] )
364  case asc( ":" )
365  '' C:...
366  function = TRUE
367 #ifdef __FB_WIN32__
368  case asc( "\" )
369  '' \\... UNC path
370  function = ((*path)[0] = asc( "\" ))
371 #endif
372  end select
373  end if
374 #else
375  '' /...
376  function = ((*path)[0] = asc( "/" ))
377 #endif
378 end function
379 
380 function hCheckFileFormat( byval f as integer ) as integer
381  dim as long BOM
382  dim as FBFILE_FORMAT fmt
383 
384  '' little-endian assumptions
385  fmt = FBFILE_FORMAT_ASCII
386 
387  if( get( #f, 0, BOM ) = 0 ) then
388  if( BOM = &hFFFE0000 ) then
389  fmt = FBFILE_FORMAT_UTF32BE
390 
391  elseif( BOM = &h0000FEFF ) then
392  fmt = FBFILE_FORMAT_UTF32LE
393 
394  else
395  BOM and= &h00FFFFFF
396  if( BOM = &h00BFBBEF ) then
397  fmt = FBFILE_FORMAT_UTF8
398 
399  else
400  BOM and= &h0000FFFF
401  if( BOM = &h0000FEFF ) then
402  fmt = FBFILE_FORMAT_UTF16LE
403 
404  elseif( BOM = &h0000FFFE ) then
405  fmt = FBFILE_FORMAT_UTF16BE
406  end if
407  end if
408  end if
409 
410  select case fmt
411  case FBFILE_FORMAT_ASCII
412  seek #f, 1
413 
414  case FBFILE_FORMAT_UTF8
415  seek #f, 1+3
416 
417  case FBFILE_FORMAT_UTF16LE, _
418  FBFILE_FORMAT_UTF16BE
419  seek #f, 1+2
420  end select
421  end if
422 
423  function = fmt
424 end function
425 
426 function hCurDir( ) as string
427  '' curdir() usually won't be terminated with a path separator,
428  '' except when it points to the file system root, instead of
429  '' some directory (e.g. C:\ on Win32 or / on Unix).
430  function = pathStripDiv( curdir( ) )
431 end function
432 
433 function hIsValidSymbolName( byval sym as zstring ptr ) as integer
434 
435  if( sym = NULL ) then exit function
436 
437  var symlen = len( *sym )
438 
439  if( symlen = 0 ) then exit function
440 
441  if( (hIsChar(sym[0]) orelse (sym[0] = asc("_"))) = FALSE ) then exit function
442 
443  for i as integer = 1 to symlen-1
444  if( ((hIsChar(sym[i])) orelse (sym[i] = asc("_")) orelse (hIsCharNumeric(sym[i]))) = FALSE ) then exit function
445  next
446 
447  function = TRUE
448 
449 end function
450 
451 '' Checks whether a string starts with and ends in [double-]quotes.
452 function strIsQuoted(byref s as string) as integer
453  dim as integer last = len(s) - 1
454  if (last < 1) then
455  return FALSE
456  end if
457 
458  return (((s[0] = asc("""")) and (s[last] = asc(""""))) or _
459  ((s[0] = asc("'" )) and (s[last] = asc("'" ))))
460 end function
461 
462 function strUnquote(byref s as string) as string
463  if (strIsQuoted(s)) then
464  return mid(s, 2, len(s) - 2)
465  end if
466  return s
467 end function
468