Qore Util Module Reference 1.10
Loading...
Searching...
No Matches
Util.qm.dox.h
1// -*- mode: c++; indent-tabs-mode: nil -*-
3
4/* Util.qm Copyright 2014 - 2023 Qore Technologies, s.r.o.
5
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
12
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23*/
24
25// minimum Qore version
26
27// do not use $ for vars, assume local var scope
28
29// require type definitions everywhere
30
31// enable all warnings
32
33
176namespace Init {
177 init();
178
179};
180
182namespace Util {
184
186 public hashdecl UriQueryInfo {
188 string method;
189
191 *hash<auto> params;
192
194
196 *hash<auto> path_params;
197 };
198
200
217 int parse_memory_size(string str, *bool use_binary);
218
219
221
234 int compare_version(string lv, string rv);
235
236
238
256 string normalize_dir(string path, string cwd = getcwd());
257
258
260
275 string normalize_dir_unix(string path, string cwd = getcwd());
276
277
279
294 string normalize_dir_windows(string path, string cwd = getcwd());
295
296
298
311 string get_relative_path(string basedir, string path, string cwd = getcwd());
312
313
315
328 bool absolute_path(string path);
329
330
332
345 bool absolute_path_unix(string path);
346
347
349
362 bool absolute_path_windows(string path);
363
364
365 // private, non-exported helper function for parse_memory_size()
366 int _get_size(string num, number mult);
367
368
369 // private, non-exported constant hashes for scan_exp() below
370 const ParenMap = (")": "(", "}": "{", "]": "[",);
371 const FwdParenMap = map {$1.value: $1.key}, ParenMap.pairIterator();
372
373 // private, non-exported constants for parsing
374 const ET_RawString = 0;
375 const ET_QuotedString = 1;
376 const ET_BracketString = 2;
377 const ET_CurlyBracketString = 3;
378 const ET_Eq = 4;
379 const ET_Comma = 5;
380 const EtMap = {
381 ET_RawString: "raw string",
382 ET_QuotedString: "quoted string",
383 ET_BracketString: "bracket expression",
384 ET_CurlyBracketString: "curly bracket expression",
385 ET_Eq: "=",
386 ET_Comma: ",",
387 };
388 const Suffixes = ("th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th");
389
390 // private scanner for parse_to_qore_value()
391 list<hash> scan_exp(string arg);
392
393
394 // private, non-exported function: tries to evaluate the argument as a Qore expression/value
395 hash<auto> eval_text(string arg, bool err = False);
396
397
398 // private, non-exported function: tries to evaluate the argument as a Qore expression/value
399 /* returns the value parsed
400 */
401 auto parse_to_basic_value(*string arg);
402
403
405
476 auto parse_to_qore_value(string arg);
477
478
480
498 string serialize_to_string(auto val);
499
500
502
510 hash<UriQueryInfo> parse_uri_query(string path);
511
512
514
519 string make_uri_query(hash<UriQueryInfo> h, string arg_separator = ';');
520
521
523
539 string get_random_string(softint len = 15, int level = 3);
540
541
543
547 string get_exception_string(hash<auto> ex);
548
549
551 const StorageNames = ("Kilo", "Mega", "Giga", "Tera", "Peta", "Exa", "Zetta", "Yotta", "Bronto");
552
554 const StorageAbbr = map {$1[0].lwr(): $#}, StorageNames;
555
557
565 string get_byte_size(softnumber n, int decimals = 2);
566
567
569
577 string get_marketing_byte_size(softnumber n, int decimals = 2);
578
579
581
592 bool same(list<auto> l);
593
594
596
606 list<auto> slice(list<auto> l, softlist<auto> indices);
607
608
610
629 string tmp_location();
630
631
633
650 bool is_int(string text, bool pure = False);
651
652
653 const RE_FLOAT = ...;
654
655
657
674 bool is_float(string text, bool pure = False);
675
676
677 const RE_NUMBER = ...;
678
679
681
699 bool is_number(string text, bool pure = False);
700
701
703
718 string lpad(softstring text, int length, string padding = ' ');
719
720
722
737 string rpad(softstring text, int length, string padding = ' ');
738
739
741
752 string ordinal(int i);
753
754
756
775 string plural(int count, string base, string singular = '', string plural = 's');
776
777
779
791 string regex_escape(string text);
792
793
795
807 string glob_to_regex(string pat);
808
809
811
822 list<auto> flatten(softlist<auto> arg);
823
824
826
837 list<int> parse_ranges(string text);
838
839
841
858 list<auto> uniq(softlist<auto> arg);
859
860
862
873 list<auto> zip();
874
875
877
891 bool check_ip_address(string ip, bool exception = False);
892
893
895
900 bool string_starts_with(string str, string substr);
901
902
904
909 bool string_ends_with(string str, string substr);
910
911
913 public hashdecl UrlRetrievalOptions {
915 bool as_binary = False;
916
919
921 *hash<auto> http_headers;
922
925
928 };
929
931
939 data get_file_from_sftp(string url, string path, *hash<UrlRetrievalOptions> options);
940
941
943
951 data get_file_from_ftp(string url, string path, *hash<UrlRetrievalOptions> options);
952
953
955
961 data get_file_from_http(string url, *string path, *hash<UrlRetrievalOptions> options);
962
963
965
970 data get_local_file(string path, *hash<UrlRetrievalOptions> options);
971
972
974
981 data get_file_from_url(string url, *hash<UrlRetrievalOptions> options);
982
983
985
991 string substitute_env_vars(string str);
992
993
995
1001 hash<ExceptionInfo> check_exception_serializable(hash<ExceptionInfo> ex);
1002
1003};
the Util namespace contains all the objects in the Util module
Definition: Util.qm.dox.h:182
string normalize_dir_windows(string path, string cwd=getcwd())
returns a normalized Windows path name (eliminates ".", "..", and "//")
string get_byte_size(softnumber n, int decimals=2)
returns a string giving a user-friendly real storage size (ie based on 1KiB = 1024 bytes) in KiB,...
data get_file_from_http(string url, *string path, *hash< UrlRetrievalOptions > options)
Retrieves file data from an HTTP URL.
hash< ExceptionInfo > check_exception_serializable(hash< ExceptionInfo > ex)
Recursively ensure that exception arguments are serializable.
list< auto > zip()
Returns a list of lists, where the i-th list contains the i-th element from each of the argument list...
data get_file_from_ftp(string url, string path, *hash< UrlRetrievalOptions > options)
Retrieves file data from an FTP URL and a path.
string tmp_location()
Returns a path for temporary files.
list< auto > slice(list< auto > l, softlist< auto > indices)
list slice implementation
string make_uri_query(hash< UriQueryInfo > h, string arg_separator=';')
returns a serialized string for a UriQueryInfo hash
string serialize_to_string(auto val)
Serializes the given value to a string that can be deserialized with parse_to_qore_value()
bool same(list< auto > l)
checks whether given list has only unique elements
data get_local_file(string path, *hash< UrlRetrievalOptions > options)
Retrieves file data from a local file path.
string get_random_string(softint len=15, int level=3)
function that returns a random string
string ordinal(int i)
Returns string with partially textual representation of ordinal integer value.
string normalize_dir_unix(string path, string cwd=getcwd())
returns a normalized UNIX path name (starting from /, eliminates ".", "..", and "//")
string normalize_dir(string path, string cwd=getcwd())
returns a platform-specific normalized path name (starting from /, eliminates ".",...
bool is_number(string text, bool pure=False)
Checks whether string represents a (true) number value.
data get_file_from_sftp(string url, string path, *hash< UrlRetrievalOptions > options)
Retrieves file data from an SFTP URL and a path.
string lpad(softstring text, int length, string padding=' ')
Returns a string left-padded to a specified length with the specified characters.
list< auto > flatten(softlist< auto > arg)
Flattens a nested list (the nesting can be to any depth).
bool is_float(string text, bool pure=False)
Checks whether string represents a (true) float value.
auto parse_to_qore_value(string arg)
tries to convert a string (such as an argument given from the command-line) to a Qore value
bool absolute_path_windows(string path)
returns True if the argument is a Windows absolute path, False if not
string get_marketing_byte_size(softnumber n, int decimals=2)
returns a string giving a user-friendly "marketing storage" size (ie based on 1KB = 1000 bytes) in KB...
hash< UriQueryInfo > parse_uri_query(string path)
parses a URI path for a arguments and a method; where the method is the part of the path before the f...
string glob_to_regex(string pat)
Translates a shell pattern to a regular expression.
string rpad(softstring text, int length, string padding=' ')
Returns a string right-padded to a specified length with the specified characters.
data get_file_from_url(string url, *hash< UrlRetrievalOptions > options)
Retrieves file data from the given URL.
string get_exception_string(hash< auto > ex)
returns a multi-line string from the exception hash argument suitable for logging or output on the co...
string substitute_env_vars(string str)
Substitutes environment variables in a string and returns the string.
bool string_starts_with(string str, string substr)
Verifies whether a string begins with specific substring.
const StorageNames
storage names (Geop, which would normally follow Bronto, is excluded as the abbreviation would be GB/...
Definition: Util.qm.dox.h:551
string regex_escape(string text)
Escapes (backslashes) all non-alphanumeric characters in a string.
list< int > parse_ranges(string text)
Parses a string and returns a list of integers.
int compare_version(string lv, string rv)
compares complex versions by breaking down strings into component parts
string plural(int count, string base, string singular='', string plural='s')
Returns string with number and proper singular/plural form of noun.
bool string_ends_with(string str, string substr)
Verifies whether a string ends with specific substring.
bool absolute_path_unix(string path)
returns True if the argument is a UNIX absolute path, False if not
bool check_ip_address(string ip, bool exception=False)
verifies if a string is an IPv4 or IPv6 address
bool is_int(string text, bool pure=False)
Checks whether string represents a (true) integer value.
int parse_memory_size(string str, *bool use_binary)
Returns a size in bytes from a string where "1M" = 1048576 and so forth.
list< auto > uniq(softlist< auto > arg)
Returns a duplicate-free version of the list.
string get_relative_path(string basedir, string path, string cwd=getcwd())
returns a path as a relative path from another directory
const StorageAbbr
hash of single-letter storage name abbreviations
Definition: Util.qm.dox.h:554
bool absolute_path(string path)
returns True if the argument is an absolute path, False if not
the return value of the parse_uri_query() function
Definition: Util.qm.dox.h:186
*hash< auto > params
(only present if a "?" character is present in the input); the part of the path after the first "?...
Definition: Util.qm.dox.h:191
string method
the part of the URI path before the first "?" character or the entire path if no "?...
Definition: Util.qm.dox.h:188
*hash< auto > path_params
path params; only when matching a request with a REST schema
Definition: Util.qm.dox.h:196
URL retrieval options for get_file_from_url()
Definition: Util.qm.dox.h:913
*string ssh2_private_key
SSH2 private key path.
Definition: Util.qm.dox.h:924
bool as_binary
Return the file data as binary data; if False (the default), a string will be returned.
Definition: Util.qm.dox.h:915
*hash< auto > http_headers
HTTP headers to add to any HTTP GET request.
Definition: Util.qm.dox.h:921
*string default_path
The default path for files with relative paths.
Definition: Util.qm.dox.h:918
*string ssh2_public_key
SSH2 public key path.
Definition: Util.qm.dox.h:927