Mercurial > hg > egg-tcls
annotate utillib.tcl @ 609:a5e83abb51a8
utillib: Add one HTML entity conversion.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Tue, 09 Feb 2021 00:13:42 +0200 |
parents | 543f1d72361e |
children | a5c929a473ac |
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 | 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 "-|-|'|'|—|-|‏||—|-|–|--|‪||‬|" |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
11 append utl_html_ent_str "|‎||å|å|Å|Å|é|é|:|:| | " |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
12 append utl_html_ent_str "|”|\"|“|\"|«|<<|»|>>|"|\"" |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
13 append utl_html_ent_str "|ä|ä|ö|ö|Ä|Ä|Ö|Ö|&|&|<|<|>|>" |
440
cd53828bb724
utillib: Add some new entity translations.
Matti Hamalainen <ccr@tnsp.org>
parents:
432
diff
changeset
|
14 append utl_html_ent_str "|ä|ä|å|ö|—|--|'|'|–|-|"|\"" |
290
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
15 append utl_html_ent_str "|||-|’|'|ü|ü|Ü|Ü|•|*|€|€" |
317
994c398cb3fc
utillib: Add some entity conversions.
Matti Hamalainen <ccr@tnsp.org>
parents:
316
diff
changeset
|
16 append utl_html_ent_str "|”|\"|‘|'|ä|ä|·|*|®|®|´|'" |
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 "|ö|ö|ö|ö|#|#|'|'|­||–|-" |
337
e01963cb88fd
utillib: Add some more entity etc. conversions.
Matti Hamalainen <ccr@tnsp.org>
parents:
333
diff
changeset
|
18 append utl_html_ent_str "|…|...|…|...|"|\"|­||@|#" |
440
cd53828bb724
utillib: Add some new entity translations.
Matti Hamalainen <ccr@tnsp.org>
parents:
432
diff
changeset
|
19 append utl_html_ent_str "| | |£|£|&|&|ž|ž|Ä|Ä|º|º" |
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 "|…|...|/|/| | |"|\"|'|\"" |
609
a5e83abb51a8
utillib: Add one HTML entity conversion.
Matti Hamalainen <ccr@tnsp.org>
parents:
604
diff
changeset
|
21 append utl_html_ent_str "|‘|'|’|'|”|\"|š|š|Š|Š|%|%|Ö|Ö" |
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 # Convert given string, containing HTML/XML style entities into a normal |
124b97f5a19d
utillib: Add some comments, bump copyright.
Matti Hamalainen <ccr@tnsp.org>
parents:
428
diff
changeset
|
27 # UTF-8 Unicode string, using the above entity->character mapping |
290
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
28 proc utl_convert_html_ent {udata} { |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
29 global utl_html_ent_list |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
30 return [string map -nocase $utl_html_ent_list [string map $utl_html_ent_list $udata]] |
31
1ddfada536ff
Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
31 } |
1ddfada536ff
Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
32 |
290
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
33 |
432
124b97f5a19d
utillib: Add some comments, bump copyright.
Matti Hamalainen <ccr@tnsp.org>
parents:
428
diff
changeset
|
34 # 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
|
35 # "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
|
36 proc utl_str_split {str maxlen} { |
424
825cac46b1cb
Cosmetic / stray trailing whitespace cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
422
diff
changeset
|
37 set pos 0 |
290
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
38 set len [string length $str] |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
39 set ulen 0 |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
40 set ustr "" |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
41 set result {} |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
42 while {$pos < $len} { |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
43 set end [string wordend $str $pos] |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
44 set new [expr $end - $pos + 1] |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
45 if {$ulen + $new < $maxlen} { |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
46 append ustr [string range $str $pos $end] |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
47 set ulen [expr $ulen + $new] |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
48 } else { |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
49 append ustr [string range $str $pos $end] |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
50 lappend result $ustr |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
51 set ustr "" |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
52 set ulen 0 |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
53 } |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
54 set pos [expr $end + 1] |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
55 } |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
56 if {$ustr != ""} { |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
57 lappend result $ustr |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
58 } |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
59 return $result |
31
1ddfada536ff
Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
60 } |
1ddfada536ff
Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
61 |
1ddfada536ff
Add utility "library" for database conversion and creation scripts.
Matti Hamalainen <ccr@tnsp.org>
parents:
diff
changeset
|
62 |
314
1cf897164a25
utillib: Add utl_match_delim_list helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
310
diff
changeset
|
63 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
|
64 foreach ukey [split $ulist ";"] { |
316 | 65 if {[string match $ukey $ustr]} { |
314
1cf897164a25
utillib: Add utl_match_delim_list helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
310
diff
changeset
|
66 return 1 |
1cf897164a25
utillib: Add utl_match_delim_list helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
310
diff
changeset
|
67 } |
1cf897164a25
utillib: Add utl_match_delim_list helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
310
diff
changeset
|
68 } |
1cf897164a25
utillib: Add utl_match_delim_list helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
310
diff
changeset
|
69 return 0 |
1cf897164a25
utillib: Add utl_match_delim_list helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
310
diff
changeset
|
70 } |
1cf897164a25
utillib: Add utl_match_delim_list helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
310
diff
changeset
|
71 |
1cf897164a25
utillib: Add utl_match_delim_list helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
310
diff
changeset
|
72 |
528
20a1b0f39f71
utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
502
diff
changeset
|
73 proc utl_valid_user {uhand} { |
20a1b0f39f71
utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
502
diff
changeset
|
74 if {$uhand != "" && $uhand != "{}" && $uhand != "*"} { |
20a1b0f39f71
utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
502
diff
changeset
|
75 return 1 |
20a1b0f39f71
utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
502
diff
changeset
|
76 } |
20a1b0f39f71
utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
502
diff
changeset
|
77 return 0 |
20a1b0f39f71
utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
502
diff
changeset
|
78 } |
20a1b0f39f71
utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
502
diff
changeset
|
79 |
20a1b0f39f71
utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
502
diff
changeset
|
80 |
432
124b97f5a19d
utillib: Add some comments, bump copyright.
Matti Hamalainen <ccr@tnsp.org>
parents:
428
diff
changeset
|
81 # 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
|
82 # 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
|
83 proc utl_msg_do {upreferredmsg upublic unick uchan umsg} { |
539 | 84 # Split message per line |
85 foreach qmsg [split $umsg "\n"] { | |
86 # Split each line to fit max message limit | |
87 foreach uline [utl_str_split $qmsg 445] { | |
88 if {$upublic == 1} { | |
89 putserv "$upreferredmsg $uchan :$uline" | |
90 } else { | |
91 putserv "$upreferredmsg $unick :$uline" | |
92 } | |
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
|
93 } |
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
|
94 } |
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
|
95 } |
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
|
96 |
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
|
97 |
539 | 98 # Substitute @n@ -> uvalues{n} in the ustr |
99 proc utl_str_map_values {ustr {uvalues {}}} { | |
100 set narg 1 | |
101 foreach marg $uvalues { | |
102 set ustr [string map [list "@$narg@" $marg] $ustr] | |
103 incr narg | |
104 } | |
105 return $ustr | |
106 } | |
107 | |
108 | |
528
20a1b0f39f71
utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
502
diff
changeset
|
109 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
|
110 |
539 | 111 # Replace named tokens |
528
20a1b0f39f71
utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
502
diff
changeset
|
112 set umsg [string map [list "@nick@" $unick] $umsg] |
20a1b0f39f71
utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
502
diff
changeset
|
113 set umsg [string map [list "@chan@" $uchan] $umsg] |
20a1b0f39f71
utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
502
diff
changeset
|
114 |
539 | 115 # Replace numeric tokens |
116 set umsg [utl_str_map_values $umsg $uargs] | |
528
20a1b0f39f71
utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
502
diff
changeset
|
117 |
20a1b0f39f71
utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
502
diff
changeset
|
118 utl_msg_do $upreferredmsg $upublic $unick $uchan $umsg |
20a1b0f39f71
utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
502
diff
changeset
|
119 } |
20a1b0f39f71
utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
502
diff
changeset
|
120 |
20a1b0f39f71
utillib: Add new helper functions.
Matti Hamalainen <ccr@tnsp.org>
parents:
502
diff
changeset
|
121 |
539 | 122 # Return formatted time string for given UNIX timestamp |
604 | 123 proc utl_ctime {ustamp} { |
124 if {$ustamp == "" || $ustamp == "*"} { | |
125 set ustamp 0 | |
422
880a07485275
Add utl_ctime() to utillib and use it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
337
diff
changeset
|
126 } |
604 | 127 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
|
128 } |
880a07485275
Add utl_ctime() to utillib and use it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
337
diff
changeset
|
129 |
880a07485275
Add utl_ctime() to utillib and use it elsewhere.
Matti Hamalainen <ccr@tnsp.org>
parents:
337
diff
changeset
|
130 |
599
76d19fa28753
utillib: Add command matching helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
582
diff
changeset
|
131 proc utl_cmd_match { ucommands uid ustr } { |
76d19fa28753
utillib: Add command matching helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
582
diff
changeset
|
132 upvar $ucommands ucmds |
76d19fa28753
utillib: Add command matching helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
582
diff
changeset
|
133 |
76d19fa28753
utillib: Add command matching helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
582
diff
changeset
|
134 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
|
135 return 1 |
76d19fa28753
utillib: Add command matching helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
582
diff
changeset
|
136 } else { |
76d19fa28753
utillib: Add command matching helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
582
diff
changeset
|
137 return 0 |
76d19fa28753
utillib: Add command matching helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
582
diff
changeset
|
138 } |
76d19fa28753
utillib: Add command matching helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
582
diff
changeset
|
139 } |
76d19fa28753
utillib: Add command matching helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
582
diff
changeset
|
140 |
76d19fa28753
utillib: Add command matching helper function.
Matti Hamalainen <ccr@tnsp.org>
parents:
582
diff
changeset
|
141 |
290
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
142 ### |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
143 ### SQL database handling |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
144 ### |
542
a57822226ba0
utillib: Rename utl_tdbc_sql_init to utl_sql_init.
Matti Hamalainen <ccr@tnsp.org>
parents:
539
diff
changeset
|
145 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
|
146 upvar $ndb_handle udb_handle |
424
825cac46b1cb
Cosmetic / stray trailing whitespace cleanup.
Matti Hamalainen <ccr@tnsp.org>
parents:
422
diff
changeset
|
147 |
290
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
148 if {$db_type == "sqlite"} { |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
149 if {$db_name == ""} { |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
150 putlog " SQLite3 database file not set." |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
151 return 0 |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
152 } |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
153 if {![file exists $db_name]} { |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
154 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
|
155 return 0 |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
156 } |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
157 package require tdbc::sqlite3 1.0 |
310
d56509a6f435
utillib: Simplify SQL connection creation.
Matti Hamalainen <ccr@tnsp.org>
parents:
295
diff
changeset
|
158 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
|
159 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
|
160 return 0 |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
161 } |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
162 putlog " (Using SQLite3 database $db_name)" |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
163 } elseif {$db_type == "postgres" || $db_type == "mysql"} { |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
164 # Check parameters |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
165 set db_args {} |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
166 if {$db_user != ""} { lappend db_args -user $db_user } |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
167 if {$db_pass != ""} { lappend db_args -passwd $db_pass } |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
168 if {$db_host != ""} { |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
169 lappend db_args -host $db_pass |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
170 if {$db_posrt != 0} { |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
171 lappend db_args -port $db_port |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
172 set db_host "${db_host}:${db_port}" |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
173 } |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
174 } else { |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
175 set db_host "localhost" |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
176 } |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
177 if {$db_name == ""} { |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
178 putlog " Database name not set." |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
179 return 0 |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
180 } |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
181 lappend db_args -database $db_name |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
182 |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
183 if {$db_type == "postgres"} { |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
184 package require tdbc::postgres 1.0 |
310
d56509a6f435
utillib: Simplify SQL connection creation.
Matti Hamalainen <ccr@tnsp.org>
parents:
295
diff
changeset
|
185 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
|
186 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
|
187 return 0 |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
188 } |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
189 putlog " (Using PostgreSQL database $db_name @ $db_host)" |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
190 } elseif {$db_type == "mysql"} { |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
191 package require tdbc::mysql 1.0 |
310
d56509a6f435
utillib: Simplify SQL connection creation.
Matti Hamalainen <ccr@tnsp.org>
parents:
295
diff
changeset
|
192 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
|
193 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
|
194 return 0 |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
195 } |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
196 putlog " (Using MySQL database $db_name @ $db_host)" |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
197 } |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
198 } else { |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
199 putlog " Invalid or unsupported database type: '$db_type'." |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
200 return 0 |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
201 } |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
202 return 1 |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
203 } |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
204 |
cad1041b5bc4
Revamp the utility functions library.
Matti Hamalainen <ccr@tnsp.org>
parents:
262
diff
changeset
|
205 |
295
141bb4a2b76f
utillib: utl_escape (which will be deprecated soon).
Matti Hamalainen <ccr@tnsp.org>
parents:
290
diff
changeset
|
206 proc utl_escape {str} { |
141bb4a2b76f
utillib: utl_escape (which will be deprecated soon).
Matti Hamalainen <ccr@tnsp.org>
parents:
290
diff
changeset
|
207 return [string map {' ''} $str] |
141bb4a2b76f
utillib: utl_escape (which will be deprecated soon).
Matti Hamalainen <ccr@tnsp.org>
parents:
290
diff
changeset
|
208 } |