Qore Util Module Reference 2.1.0
Loading...
Searching...
No Matches
Util.qm.dox.h
1// -*- mode: c++; indent-tabs-mode: nil -*-
3
4/* Util.qm Copyright 2014 - 2025 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
34
35
36
203namespace Init {
204 init();
205
206};
207
209namespace Util {
211public hashdecl UiMessageInfo {
213 string title;
214
216 string intent;
217
219 string content;
220};
221
223public hashdecl AllowedValueInfo {
226
228 *string short_desc;
229
231 auto value;
232
234 string desc;
235
237 *string intent;
238
240 *bool disabled;
241
243 *hash<auto> metadata;
244
246 *list<hash<UiMessageInfo>> messages;
247
249 *string icon;
250
252 *string image;
253};
254
256
258public hashdecl UriQueryInfo {
260
263 string method;
264
266
274 *hash<auto> params;
275
277
279 *hash<auto> path_params;
280};
281
283
304 bool is_ip_private(string address);
305
306
308
331 bool is_ip_private_ex(string address);
332
333
335
348 string ip_address_type(string address);
349
350
352
366 string ip_address_type_ex(string address);
367
368
370
378 string get_bit_string(int i, *int min_bits);
379
380
382
399 int parse_memory_size(string str, *bool use_binary);
400
401
403
416 int compare_version(string lv, string rv);
417
418
420
438 string normalize_dir(string path, string cwd = getcwd());
439
440
442
457 string normalize_dir_unix(string path, string cwd = getcwd());
458
459
461
476 string normalize_dir_windows(string path, string cwd = getcwd());
477
478
480
493 string get_relative_path(string basedir, string path, string cwd = getcwd());
494
495
497
510 bool absolute_path(string path);
511
512
514
527 bool absolute_path_unix(string path);
528
529
531
544 bool absolute_path_windows(string path);
545
546
547// private, non-exported helper function for parse_memory_size()
548int _get_size(string num, number mult);
549
550
551// private, non-exported constant hashes for scan_exp() below
552const ParenMap = ...;
553
554const FwdParenMap = map {$1.value: $1.key}, ParenMap.pairIterator();
555
556// private, non-exported constants for parsing
557const ET_RawString = 0;
558const ET_QuotedString = 1;
559const ET_BracketString = 2;
560const ET_CurlyBracketString = 3;
561const ET_Eq = 4;
562const ET_Comma = 5;
563const ET_FileRef = 6;
564const EtMap = ...;
565
566const Suffixes = ("th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th");
567
568// private scanner for parse_to_qore_value()
569list<hash> scan_exp(string arg);
570
571
572// private, non-exported function: tries to evaluate the argument as a Qore expression/value
573hash<auto> eval_text(string arg, bool err = False);
574
575
576// private, non-exported function: tries to evaluate the argument as a Qore expression/value
577/* returns the value parsed
578*/
579auto parse_to_basic_value(*string arg);
580
581
582string get_string_file_data(string loc);
583
584
585binary get_binary_file_data(string loc);
586
587
588auto get_file_ref(string type, string path);
589
590
592
699 auto parse_to_qore_value(string arg);
700
701
703
721 string serialize_to_string(auto val);
722
723
725
733 hash<UriQueryInfo> parse_uri_query(string path);
734
735
737
742 string make_uri_query(hash<UriQueryInfo> h, string arg_separator = ';');
743
744
746
762 string get_random_string(softint len = 15, int level = 3);
763
764
766
770 string get_exception_string(hash<auto> ex);
771
772
774const StorageNames = ("Kilo", "Mega", "Giga", "Tera", "Peta", "Exa", "Zetta", "Yotta", "Bronto");
775
777const StorageAbbr = map {$1[0].lwr(): $#}, StorageNames;
778
780
788 string get_byte_size(softnumber n, int decimals = 2);
789
790
792
800 string get_marketing_byte_size(softnumber n, int decimals = 2);
801
802
804
815 bool same(list<auto> l);
816
817
819
829 list<auto> slice(list<auto> l, softlist<auto> indices);
830
831
833
852 string tmp_location();
853
854
856
873 bool is_int(string text, bool pure = False);
874
875
876const RE_FLOAT = ...;
877
878
880
897 bool is_float(string text, bool pure = False);
898
899
900const RE_NUMBER = ...;
901
902
904
922 bool is_number(string text, bool pure = False);
923
924
926
941 string lpad(softstring text, int length, string padding = ' ');
942
943
945
960 string rpad(softstring text, int length, string padding = ' ');
961
962
964
975 string ordinal(int i);
976
977
979
998 string plural(int count, string base, string singular = '', string plural = 's');
999
1000
1002
1014 string regex_escape(string text);
1015
1016
1018
1030 string glob_to_regex(string pat);
1031
1032
1034
1045 list<auto> flatten(softlist<auto> arg);
1046
1047
1049
1060 list<int> parse_ranges(string text);
1061
1062
1064
1081 list<auto> uniq(softlist<auto> arg);
1082
1083
1085
1096 list<auto> zip();
1097
1098
1100
1119 bool check_ip_address(string ip, bool exception = False);
1120
1121
1123
1128 bool string_starts_with(string str, string substr);
1129
1130
1132
1137 bool string_ends_with(string str, string substr);
1138
1139
1141public hashdecl UrlRetrievalOptions {
1143 bool as_binary = False;
1144
1147
1149 *hash<auto> http_headers;
1150
1153
1156};
1157
1159
1167 data get_file_from_sftp(string url, string path, *hash<UrlRetrievalOptions> options);
1168
1169
1171
1179 data get_file_from_ftp(string url, string path, *hash<UrlRetrievalOptions> options);
1180
1181
1183
1189 data get_file_from_http(string url, *string path, *hash<UrlRetrievalOptions> options);
1190
1191
1193
1198 data get_local_file(string path, *hash<UrlRetrievalOptions> options);
1199
1200
1202
1209 data get_file_from_url(string url, *hash<UrlRetrievalOptions> options);
1210
1211
1213
1219 string substitute_env_vars(string str);
1220
1221
1223
1229 hash<ExceptionInfo> check_exception_serializable(hash<ExceptionInfo> ex);
1230
1231};
the Util namespace contains all the objects in the Util module
Definition Util.qm.dox.h:209
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.
bool is_ip_private(string address)
Returns True if the IP address is a private IP address.
string ip_address_type_ex(string address)
Returns the type of the IP address; throws an exception if the address is invalid.
bool is_ip_private_ex(string address)
Returns True if the IP address is a private IP address; throws an exception is the address is invalid...
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()
string ip_address_type(string address)
Returns the type of the IP address.
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:774
string get_bit_string(int i, *int min_bits)
Returns a string of the bits set in an integer.
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:777
bool absolute_path(string path)
returns True if the argument is an absolute path, False if not
Allowed value hash.
Definition Util.qm.dox.h:223
*string short_desc
A short description in plain text for the value.
Definition Util.qm.dox.h:228
auto value
The value itself.
Definition Util.qm.dox.h:231
*bool disabled
If the value is disabled.
Definition Util.qm.dox.h:240
string desc
Description for the value.
Definition Util.qm.dox.h:234
*list< hash< UiMessageInfo > > messages
Messages related to the value.
Definition Util.qm.dox.h:246
*string icon
Any icon info for the allowed value.
Definition Util.qm.dox.h:249
*string display_name
A display name for the allowed value.
Definition Util.qm.dox.h:225
*hash< auto > metadata
Any additional data about the value.
Definition Util.qm.dox.h:243
*string intent
Any UI intent associated with the value.
Definition Util.qm.dox.h:237
*string image
Any image info for the allowed value.
Definition Util.qm.dox.h:252
Message information for display to the user.
Definition Util.qm.dox.h:211
string intent
The intent of the message.
Definition Util.qm.dox.h:216
string content
The message content itself.
Definition Util.qm.dox.h:219
string title
The title of the message.
Definition Util.qm.dox.h:213
the return value of the parse_uri_query() function
Definition Util.qm.dox.h:258
*hash< auto > params
Query parameters.
Definition Util.qm.dox.h:274
string method
The URI method.
Definition Util.qm.dox.h:263
*hash< auto > path_params
Path params; only when matching a request with a REST schema.
Definition Util.qm.dox.h:279
URL retrieval options for get_file_from_url()
Definition Util.qm.dox.h:1141
*string ssh2_private_key
SSH2 private key path.
Definition Util.qm.dox.h:1152
bool as_binary
Return the file data as binary data; if False (the default), a string will be returned.
Definition Util.qm.dox.h:1143
*hash< auto > http_headers
HTTP headers to add to any HTTP GET request.
Definition Util.qm.dox.h:1149
*string default_path
The default path for files with relative paths.
Definition Util.qm.dox.h:1146
*string ssh2_public_key
SSH2 public key path.
Definition Util.qm.dox.h:1155