annotate utillib.tcl @ 698:6ba9f961e463 default tip

quotedb: Bump version and copyright.
author Matti Hamalainen <ccr@tnsp.org>
date Mon, 18 Sep 2023 11:38:41 +0300
parents d82cadd5b4ff
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
1 ##########################################################################
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
2 #
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
3 # TCL functions library by Matti 'ccr' Hamalainen <ccr@tnsp.org>
578
14dfb925a64a Bump copyright years.
Matti Hamalainen <ccr@tnsp.org>
parents: 575
diff changeset
4 # (C) Copyright 2015-2021 Tecnic Software productions (TNSP)
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
5 #
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
6 # This script is freely distributable under GNU GPL (version 2) license.
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
7 #
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
8 ##########################################################################
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
9
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
10 set utl_html_ent_str "&#45;|-|&#39;|'|—|-|&rlm;||&#8212;|-|&#8211;|--|&#x202a;||&#x202c;|"
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
11 append utl_html_ent_str "|&lrm;||&aring;|å|&Aring;|Å|&eacute;|é|&#58;|:|&nbsp;| "
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
12 append utl_html_ent_str "|&#8221;|\"|&#8220;|\"|&laquo;|<<|&raquo;|>>|&quot;|\""
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
13 append utl_html_ent_str "|&auml;|ä|&ouml;|ö|&Auml;|Ä|&Ouml;|Ö|&amp;|&|&lt;|<|&gt;|>"
440
cd53828bb724 utillib: Add some new entity translations.
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
14 append utl_html_ent_str "|&#228;|ä|&#229;|ö|&mdash;|--|&#039;|'|&ndash;|-|&#034;|\""
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
15 append utl_html_ent_str "|&#124;|-|&#8217;|'|&uuml;|ü|&Uuml;|Ü|&bull;|*|&euro;|€"
317
994c398cb3fc utillib: Add some entity conversions.
Matti Hamalainen <ccr@tnsp.org>
parents: 316
diff changeset
16 append utl_html_ent_str "|&rdquo;|\"|&#8216;|'|&#xe4;|ä|&#xb7;|*|&#174;|®|&acute;|'"
574
2a1f3e0f0977 utillib: Adjust and add some HTML entity/Unicode conversions.
Matti Hamalainen <ccr@tnsp.org>
parents: 542
diff changeset
17 append utl_html_ent_str "|&#246;|ö|&#xf6;|ö|&#35;|#|&apos;|'|&shy;||–|-"
337
e01963cb88fd utillib: Add some more entity etc. conversions.
Matti Hamalainen <ccr@tnsp.org>
parents: 333
diff changeset
18 append utl_html_ent_str "|&#x2026;|...|&hellip;|...|&#34;|\"|&#xad;||&#064;|#"
440
cd53828bb724 utillib: Add some new entity translations.
Matti Hamalainen <ccr@tnsp.org>
parents: 432
diff changeset
19 append utl_html_ent_str "|&#xa0;| |&pound;|£|&#38;|&|&#158;|ž|&#196;|Ä|&#186;|º"
574
2a1f3e0f0977 utillib: Adjust and add some HTML entity/Unicode conversions.
Matti Hamalainen <ccr@tnsp.org>
parents: 542
diff changeset
20 append utl_html_ent_str "|&#8230;|...|&#47;|/|&#10;| |&#034;|\"|&#x27;|\""
609
a5e83abb51a8 utillib: Add one HTML entity conversion.
Matti Hamalainen <ccr@tnsp.org>
parents: 604
diff changeset
21 append utl_html_ent_str "|‘|'|’|'|”|\"|&scaron;|š|&Scaron;|Š|&#37;|%|&#214;|Ö"
574
2a1f3e0f0977 utillib: Adjust and add some HTML entity/Unicode conversions.
Matti Hamalainen <ccr@tnsp.org>
parents: 542
diff changeset
22
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
23 set utl_html_ent_list [split [encoding convertfrom "utf-8" $utl_html_ent_str] "|"]
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
24
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
25
432
124b97f5a19d utillib: Add some comments, bump copyright.
Matti Hamalainen <ccr@tnsp.org>
parents: 428
diff changeset
26 # Split given string "str" into a list of sub-strings of maximum length
124b97f5a19d utillib: Add some comments, bump copyright.
Matti Hamalainen <ccr@tnsp.org>
parents: 428
diff changeset
27 # "maxlen", by attempting to split at "words", if possible.
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
28 proc utl_str_split {str maxlen} {
424
825cac46b1cb Cosmetic / stray trailing whitespace cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 422
diff changeset
29 set pos 0
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
30 set len [string length $str]
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
31 set ulen 0
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
32 set ustr ""
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
33 set result {}
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
34 while {$pos < $len} {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
35 set end [string wordend $str $pos]
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
36 set new [expr $end - $pos + 1]
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
37 if {$ulen + $new < $maxlen} {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
38 append ustr [string range $str $pos $end]
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
39 set ulen [expr $ulen + $new]
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
40 } else {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
41 append ustr [string range $str $pos $end]
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
42 lappend result $ustr
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
43 set ustr ""
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
44 set ulen 0
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
45 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
46 set pos [expr $end + 1]
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
47 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
48 if {$ustr != ""} {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
49 lappend result $ustr
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
50 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
51 return $result
31
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
52 }
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
53
1ddfada536ff Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff changeset
54
314
1cf897164a25 utillib: Add utl_match_delim_list helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
55 proc utl_match_delim_list {ulist ustr} {
1cf897164a25 utillib: Add utl_match_delim_list helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
56 foreach ukey [split $ulist ";"] {
316
682adbf6f09f urllog: 10L Bugfix.
Matti Hamalainen <ccr@tnsp.org>
parents: 314
diff changeset
57 if {[string match $ukey $ustr]} {
314
1cf897164a25 utillib: Add utl_match_delim_list helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
58 return 1
1cf897164a25 utillib: Add utl_match_delim_list helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
59 }
1cf897164a25 utillib: Add utl_match_delim_list helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
60 }
1cf897164a25 utillib: Add utl_match_delim_list helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
61 return 0
1cf897164a25 utillib: Add utl_match_delim_list helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
62 }
1cf897164a25 utillib: Add utl_match_delim_list helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
63
1cf897164a25 utillib: Add utl_match_delim_list helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 310
diff changeset
64
528
20a1b0f39f71 utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 502
diff changeset
65 proc utl_valid_user {uhand} {
20a1b0f39f71 utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 502
diff changeset
66 if {$uhand != "" && $uhand != "{}" && $uhand != "*"} {
610
a5c929a473ac utillib: Check against validuser() in utl_valid_user() proc.
Matti Hamalainen <ccr@tnsp.org>
parents: 609
diff changeset
67 return [validuser $uhand]
528
20a1b0f39f71 utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 502
diff changeset
68 }
20a1b0f39f71 utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 502
diff changeset
69 return 0
20a1b0f39f71 utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 502
diff changeset
70 }
20a1b0f39f71 utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 502
diff changeset
71
20a1b0f39f71 utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 502
diff changeset
72
432
124b97f5a19d utillib: Add some comments, bump copyright.
Matti Hamalainen <ccr@tnsp.org>
parents: 428
diff changeset
73 # Send IRC message with given message type, splitting the
124b97f5a19d utillib: Add some comments, bump copyright.
Matti Hamalainen <ccr@tnsp.org>
parents: 428
diff changeset
74 # string to fit to IRCNet (etc.) max message length.
426
84d3d7abeb8a Add utl_msg_do() function into utillib and refactor out similar things in quotedb and weather scripts.
Matti Hamalainen <ccr@tnsp.org>
parents: 424
diff changeset
75 proc utl_msg_do {upreferredmsg upublic unick uchan umsg} {
539
c9dc79874939 utillib: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 528
diff changeset
76 # Split message per line
c9dc79874939 utillib: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 528
diff changeset
77 foreach qmsg [split $umsg "\n"] {
c9dc79874939 utillib: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 528
diff changeset
78 # Split each line to fit max message limit
c9dc79874939 utillib: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 528
diff changeset
79 foreach uline [utl_str_split $qmsg 445] {
c9dc79874939 utillib: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 528
diff changeset
80 if {$upublic == 1} {
c9dc79874939 utillib: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 528
diff changeset
81 putserv "$upreferredmsg $uchan :$uline"
c9dc79874939 utillib: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 528
diff changeset
82 } else {
c9dc79874939 utillib: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 528
diff changeset
83 putserv "$upreferredmsg $unick :$uline"
c9dc79874939 utillib: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 528
diff changeset
84 }
426
84d3d7abeb8a Add utl_msg_do() function into utillib and refactor out similar things in quotedb and weather scripts.
Matti Hamalainen <ccr@tnsp.org>
parents: 424
diff changeset
85 }
84d3d7abeb8a Add utl_msg_do() function into utillib and refactor out similar things in quotedb and weather scripts.
Matti Hamalainen <ccr@tnsp.org>
parents: 424
diff changeset
86 }
84d3d7abeb8a Add utl_msg_do() function into utillib and refactor out similar things in quotedb and weather scripts.
Matti Hamalainen <ccr@tnsp.org>
parents: 424
diff changeset
87 }
84d3d7abeb8a Add utl_msg_do() function into utillib and refactor out similar things in quotedb and weather scripts.
Matti Hamalainen <ccr@tnsp.org>
parents: 424
diff changeset
88
84d3d7abeb8a Add utl_msg_do() function into utillib and refactor out similar things in quotedb and weather scripts.
Matti Hamalainen <ccr@tnsp.org>
parents: 424
diff changeset
89
539
c9dc79874939 utillib: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 528
diff changeset
90 # Substitute @n@ -> uvalues{n} in the ustr
c9dc79874939 utillib: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 528
diff changeset
91 proc utl_str_map_values {ustr {uvalues {}}} {
c9dc79874939 utillib: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 528
diff changeset
92 set narg 1
c9dc79874939 utillib: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 528
diff changeset
93 foreach marg $uvalues {
c9dc79874939 utillib: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 528
diff changeset
94 set ustr [string map [list "@$narg@" $marg] $ustr]
c9dc79874939 utillib: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 528
diff changeset
95 incr narg
c9dc79874939 utillib: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 528
diff changeset
96 }
c9dc79874939 utillib: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 528
diff changeset
97 return $ustr
c9dc79874939 utillib: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 528
diff changeset
98 }
c9dc79874939 utillib: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 528
diff changeset
99
c9dc79874939 utillib: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 528
diff changeset
100
528
20a1b0f39f71 utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 502
diff changeset
101 proc utl_msg_args {upreferredmsg upublic unick uchan umsg {uargs {}}} {
20a1b0f39f71 utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 502
diff changeset
102
539
c9dc79874939 utillib: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 528
diff changeset
103 # Replace named tokens
528
20a1b0f39f71 utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 502
diff changeset
104 set umsg [string map [list "@nick@" $unick] $umsg]
20a1b0f39f71 utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 502
diff changeset
105 set umsg [string map [list "@chan@" $uchan] $umsg]
20a1b0f39f71 utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 502
diff changeset
106
539
c9dc79874939 utillib: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 528
diff changeset
107 # Replace numeric tokens
c9dc79874939 utillib: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 528
diff changeset
108 set umsg [utl_str_map_values $umsg $uargs]
528
20a1b0f39f71 utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 502
diff changeset
109
20a1b0f39f71 utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 502
diff changeset
110 utl_msg_do $upreferredmsg $upublic $unick $uchan $umsg
20a1b0f39f71 utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 502
diff changeset
111 }
20a1b0f39f71 utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 502
diff changeset
112
20a1b0f39f71 utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents: 502
diff changeset
113
539
c9dc79874939 utillib: Cleanups.
Matti Hamalainen <ccr@tnsp.org>
parents: 528
diff changeset
114 # Return formatted time string for given UNIX timestamp
604
543f1d72361e utillib: Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 599
diff changeset
115 proc utl_ctime {ustamp} {
543f1d72361e utillib: Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 599
diff changeset
116 if {$ustamp == "" || $ustamp == "*"} {
543f1d72361e utillib: Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 599
diff changeset
117 set ustamp 0
422
880a07485275 Add utl_ctime() to utillib and use it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents: 337
diff changeset
118 }
604
543f1d72361e utillib: Cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 599
diff changeset
119 return [clock format $ustamp -format "%d.%m.%Y %H:%M"]
422
880a07485275 Add utl_ctime() to utillib and use it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents: 337
diff changeset
120 }
880a07485275 Add utl_ctime() to utillib and use it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents: 337
diff changeset
121
880a07485275 Add utl_ctime() to utillib and use it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents: 337
diff changeset
122
599
76d19fa28753 utillib: Add command matching helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 582
diff changeset
123 proc utl_cmd_match { ucommands uid ustr } {
76d19fa28753 utillib: Add command matching helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 582
diff changeset
124 upvar $ucommands ucmds
76d19fa28753 utillib: Add command matching helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 582
diff changeset
125
76d19fa28753 utillib: Add command matching helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 582
diff changeset
126 if {[info exists ucmds($uid)] && [regexp -nocase -- $ucmds($uid) $ustr]} {
76d19fa28753 utillib: Add command matching helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 582
diff changeset
127 return 1
76d19fa28753 utillib: Add command matching helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 582
diff changeset
128 } else {
76d19fa28753 utillib: Add command matching helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 582
diff changeset
129 return 0
76d19fa28753 utillib: Add command matching helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 582
diff changeset
130 }
76d19fa28753 utillib: Add command matching helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 582
diff changeset
131 }
76d19fa28753 utillib: Add command matching helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 582
diff changeset
132
76d19fa28753 utillib: Add command matching helper function.
Matti Hamalainen <ccr@tnsp.org>
parents: 582
diff changeset
133
611
6f1d26814cc9 utillib: Add helper function utl_arg_get for getting arguments from a given list.
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
134 proc utl_arg_get {uarglist uindex uarg uautoinc} {
6f1d26814cc9 utillib: Add helper function utl_arg_get for getting arguments from a given list.
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
135 upvar $uindex rindex
6f1d26814cc9 utillib: Add helper function utl_arg_get for getting arguments from a given list.
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
136 upvar $uarg rarg
6f1d26814cc9 utillib: Add helper function utl_arg_get for getting arguments from a given list.
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
137
6f1d26814cc9 utillib: Add helper function utl_arg_get for getting arguments from a given list.
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
138 if {[llength $uarglist] < $rindex + 1} {
6f1d26814cc9 utillib: Add helper function utl_arg_get for getting arguments from a given list.
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
139 return 0
6f1d26814cc9 utillib: Add helper function utl_arg_get for getting arguments from a given list.
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
140 }
6f1d26814cc9 utillib: Add helper function utl_arg_get for getting arguments from a given list.
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
141
6f1d26814cc9 utillib: Add helper function utl_arg_get for getting arguments from a given list.
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
142 set rarg [lindex $uarglist $rindex]
6f1d26814cc9 utillib: Add helper function utl_arg_get for getting arguments from a given list.
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
143 set rindex [expr $rindex + $uautoinc]
6f1d26814cc9 utillib: Add helper function utl_arg_get for getting arguments from a given list.
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
144
6f1d26814cc9 utillib: Add helper function utl_arg_get for getting arguments from a given list.
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
145 return 1
6f1d26814cc9 utillib: Add helper function utl_arg_get for getting arguments from a given list.
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
146 }
6f1d26814cc9 utillib: Add helper function utl_arg_get for getting arguments from a given list.
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
147
6f1d26814cc9 utillib: Add helper function utl_arg_get for getting arguments from a given list.
Matti Hamalainen <ccr@tnsp.org>
parents: 610
diff changeset
148
664
586caf75fccc utillib: Add utl_arg_rest helper.
Matti Hamalainen <ccr@tnsp.org>
parents: 658
diff changeset
149 proc utl_arg_rest {uarglist uindex ustr} {
586caf75fccc utillib: Add utl_arg_rest helper.
Matti Hamalainen <ccr@tnsp.org>
parents: 658
diff changeset
150 upvar $ustr dstr
586caf75fccc utillib: Add utl_arg_rest helper.
Matti Hamalainen <ccr@tnsp.org>
parents: 658
diff changeset
151
586caf75fccc utillib: Add utl_arg_rest helper.
Matti Hamalainen <ccr@tnsp.org>
parents: 658
diff changeset
152 if {$uindex < [llength $uarglist]} {
586caf75fccc utillib: Add utl_arg_rest helper.
Matti Hamalainen <ccr@tnsp.org>
parents: 658
diff changeset
153 set dstr [join [lrange $uarglist $uindex end] " "]
586caf75fccc utillib: Add utl_arg_rest helper.
Matti Hamalainen <ccr@tnsp.org>
parents: 658
diff changeset
154 return 1
586caf75fccc utillib: Add utl_arg_rest helper.
Matti Hamalainen <ccr@tnsp.org>
parents: 658
diff changeset
155 } else {
586caf75fccc utillib: Add utl_arg_rest helper.
Matti Hamalainen <ccr@tnsp.org>
parents: 658
diff changeset
156 set dstr ""
586caf75fccc utillib: Add utl_arg_rest helper.
Matti Hamalainen <ccr@tnsp.org>
parents: 658
diff changeset
157 return 0
586caf75fccc utillib: Add utl_arg_rest helper.
Matti Hamalainen <ccr@tnsp.org>
parents: 658
diff changeset
158 }
586caf75fccc utillib: Add utl_arg_rest helper.
Matti Hamalainen <ccr@tnsp.org>
parents: 658
diff changeset
159 }
586caf75fccc utillib: Add utl_arg_rest helper.
Matti Hamalainen <ccr@tnsp.org>
parents: 658
diff changeset
160
586caf75fccc utillib: Add utl_arg_rest helper.
Matti Hamalainen <ccr@tnsp.org>
parents: 658
diff changeset
161
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
162 ###
658
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
163 ### HTML / HTTP related
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
164 ###
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
165 # Convert given string, containing HTML/XML style entities into a normal
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
166 # UTF-8 Unicode string, using the above entity->character mapping
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
167 proc utl_convert_html_ent {udata} {
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
168 global utl_html_ent_list
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
169 return [string map -nocase $utl_html_ent_list [string map $utl_html_ent_list $udata]]
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
170 }
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
171
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
172
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
173 proc utl_http_clear_request { urlStatus urlSCode urlCode urlData urlMeta } {
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
174
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
175 ### Clear the request data
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
176 upvar $urlStatus ustatus
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
177 upvar $urlSCode uscode
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
178 upvar $urlCode ucode
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
179 upvar $urlData udata
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
180 upvar $urlMeta umeta
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
181
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
182 unset ustatus
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
183 unset uscode
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
184 unset ucode
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
185 unset udata
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
186 array unset umeta
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
187 }
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
188
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
189
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
190 proc utl_http_do_request { urlHeaders urlStr urlStatus urlSCode urlCode urlData urlMeta } {
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
191
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
192 upvar $urlStatus ustatus
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
193 upvar $urlSCode uscode
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
194 upvar $urlCode ucode
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
195 upvar $urlData udata
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
196 upvar $urlMeta umeta
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
197
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
198 #set urlHeaders {}
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
199 #lappend urlHeaders "Accept-Encoding" "identity"
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
200 #lappend urlHeaders "Connection" "keep-alive"
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
201
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
202 ### Perform request
681
204699e84dee utillib: Increase HTTP request timeout to 8 seconds from 6.
Matti Hamalainen <ccr@tnsp.org>
parents: 664
diff changeset
203 if {[catch {set utoken [::http::geturl $urlStr -timeout 8000 -binary 1 -headers $urlHeaders]} uerror]} {
696
d82cadd5b4ff Set ustatus and uscode if returning an error -1 from utl_http_do_request()
Matti Hamalainen <ccr@tnsp.org>
parents: 681
diff changeset
204 set ustatus "ERROR"
d82cadd5b4ff Set ustatus and uscode if returning an error -1 from utl_http_do_request()
Matti Hamalainen <ccr@tnsp.org>
parents: 681
diff changeset
205 set ucode "INTERNAL"
658
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
206 set uscode $uerror
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
207 return -1
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
208 }
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
209
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
210 ### Check status
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
211 set ustatus [::http::status $utoken]
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
212 set uscode [::http::code $utoken]
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
213 set ucode [::http::ncode $utoken]
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
214
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
215 if {$ustatus != "ok"} {
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
216 return -2
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
217 }
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
218
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
219 ### Get data
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
220 set udata [::http::data $utoken]
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
221 array set umeta [::http::meta $utoken]
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
222 ::http::cleanup $utoken
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
223
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
224 ### Sanitize the metadata KEYS
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
225 foreach {ukey uvalue} [array get umeta] {
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
226 set ukey [string tolower $ukey]
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
227 set umeta($ukey) $uvalue
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
228 }
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
229
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
230 return 0
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
231 }
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
232
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
233
33580ee2579e utillib: Add helper functions utl_http_do_request() and utl_http_clear_request().
Matti Hamalainen <ccr@tnsp.org>
parents: 612
diff changeset
234 ###
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
235 ### SQL database handling
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
236 ###
542
a57822226ba0 utillib: Rename utl_tdbc_sql_init to utl_sql_init.
Matti Hamalainen <ccr@tnsp.org>
parents: 539
diff changeset
237 proc utl_sql_init {ndb_handle db_type db_name db_host db_port db_user db_pass} {
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
238 upvar $ndb_handle udb_handle
424
825cac46b1cb Cosmetic / stray trailing whitespace cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents: 422
diff changeset
239
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
240 if {$db_type == "sqlite"} {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
241 if {$db_name == ""} {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
242 putlog " SQLite3 database file not set."
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
243 return 0
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
244 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
245 if {![file exists $db_name]} {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
246 putlog " URLLog SQLite3 database file '$db_name' not found, or not accessible!"
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
247 return 0
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
248 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
249 package require tdbc::sqlite3 1.0
310
d56509a6f435 utillib: Simplify SQL connection creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 295
diff changeset
250 if {[catch {set udb_handle [tdbc::sqlite3::connection new $db_name]} db_errmsg]} {
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
251 putlog " Could not open SQLite3 database '$db_name': $db_errmsg"
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
252 return 0
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
253 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
254 putlog " (Using SQLite3 database $db_name)"
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
255 } elseif {$db_type == "postgres" || $db_type == "mysql"} {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
256 # Check parameters
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
257 set db_args {}
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
258 if {$db_user != ""} { lappend db_args -user $db_user }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
259 if {$db_pass != ""} { lappend db_args -passwd $db_pass }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
260 if {$db_host != ""} {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
261 lappend db_args -host $db_pass
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
262 if {$db_posrt != 0} {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
263 lappend db_args -port $db_port
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
264 set db_host "${db_host}:${db_port}"
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
265 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
266 } else {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
267 set db_host "localhost"
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
268 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
269 if {$db_name == ""} {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
270 putlog " Database name not set."
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
271 return 0
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
272 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
273 lappend db_args -database $db_name
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
274
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
275 if {$db_type == "postgres"} {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
276 package require tdbc::postgres 1.0
310
d56509a6f435 utillib: Simplify SQL connection creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 295
diff changeset
277 if {[catch {set udb_handle [tdbc::postgres::connection new {*}$db_args]} db_errmsg]} {
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
278 putlog " Could not connect to PostgreSQL database '$db_name @ $db_host': $db_errmsg"
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
279 return 0
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
280 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
281 putlog " (Using PostgreSQL database $db_name @ $db_host)"
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
282 } elseif {$db_type == "mysql"} {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
283 package require tdbc::mysql 1.0
310
d56509a6f435 utillib: Simplify SQL connection creation.
Matti Hamalainen <ccr@tnsp.org>
parents: 295
diff changeset
284 if {[catch {set udb_handle [tdbc::mysql::connection new {*}$db_args]} db_errmsg]} {
290
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
285 putlog " Could not connect to MySQL database '$db_name @ $db_host': $db_errmsg"
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
286 return 0
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
287 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
288 putlog " (Using MySQL database $db_name @ $db_host)"
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
289 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
290 } else {
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
291 putlog " Invalid or unsupported database type: '$db_type'."
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
292 return 0
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
293 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
294 return 1
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
295 }
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
296
cad1041b5bc4 Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents: 262
diff changeset
297
295
141bb4a2b76f utillib: utl_escape (which will be deprecated soon).
Matti Hamalainen <ccr@tnsp.org>
parents: 290
diff changeset
298 proc utl_escape {str} {
141bb4a2b76f utillib: utl_escape (which will be deprecated soon).
Matti Hamalainen <ccr@tnsp.org>
parents: 290
diff changeset
299 return [string map {' ''} $str]
141bb4a2b76f utillib: utl_escape (which will be deprecated soon).
Matti Hamalainen <ccr@tnsp.org>
parents: 290
diff changeset
300 }
612
c340683d6c13 utillib: Add helper functions utl_sql_stamp_to_datetime() and
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
301
c340683d6c13 utillib: Add helper functions utl_sql_stamp_to_datetime() and
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
302
c340683d6c13 utillib: Add helper functions utl_sql_stamp_to_datetime() and
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
303 proc utl_sql_stamp_to_datetime { ustamp } {
c340683d6c13 utillib: Add helper functions utl_sql_stamp_to_datetime() and
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
304 return [clock format $ustamp -format "%Y-%m-%d %H:%M:%S"]
c340683d6c13 utillib: Add helper functions utl_sql_stamp_to_datetime() and
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
305 }
c340683d6c13 utillib: Add helper functions utl_sql_stamp_to_datetime() and
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
306
c340683d6c13 utillib: Add helper functions utl_sql_stamp_to_datetime() and
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
307
c340683d6c13 utillib: Add helper functions utl_sql_stamp_to_datetime() and
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
308 proc utl_sql_datetime_to_stamp { ustr } {
c340683d6c13 utillib: Add helper functions utl_sql_stamp_to_datetime() and
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
309 return [clock scan $ustr -format "%Y-%m-%d %H:%M:%S"]
c340683d6c13 utillib: Add helper functions utl_sql_stamp_to_datetime() and
Matti Hamalainen <ccr@tnsp.org>
parents: 611
diff changeset
310 }