Artifact 5092b09cf9259c02af3e10ca26325b7587c36cc2:
File
src/string_xform.c
part of check-in
[9c01af2d22]
- refactored some /tagview code into more generic bits. Added string_xform.c.
by
stephan on
2008-02-03 21:56:52.
0000: 2f 2a 0a 2a 2a 20 43 6f 70 79 72 69 67 68 74 20 /*.** Copyright
0010: 28 63 29 20 32 30 30 37 20 44 2e 20 52 69 63 68 (c) 2007 D. Rich
0020: 61 72 64 20 48 69 70 70 0a 2a 2a 20 43 6f 70 79 ard Hipp.** Copy
0030: 72 69 67 68 74 20 28 63 29 20 32 30 30 38 20 53 right (c) 2008 S
0040: 74 65 70 68 61 6e 20 42 65 61 6c 0a 2a 2a 0a 2a tephan Beal.**.*
0050: 2a 20 54 68 69 73 20 70 72 6f 67 72 61 6d 20 69 * This program i
0060: 73 20 66 72 65 65 20 73 6f 66 74 77 61 72 65 3b s free software;
0070: 20 79 6f 75 20 63 61 6e 20 72 65 64 69 73 74 72 you can redistr
0080: 69 62 75 74 65 20 69 74 20 61 6e 64 2f 6f 72 0a ibute it and/or.
0090: 2a 2a 20 6d 6f 64 69 66 79 20 69 74 20 75 6e 64 ** modify it und
00a0: 65 72 20 74 68 65 20 74 65 72 6d 73 20 6f 66 20 er the terms of
00b0: 74 68 65 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 the GNU General
00c0: 50 75 62 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e 73 Public.** Licens
00d0: 65 20 61 73 20 70 75 62 6c 69 73 68 65 64 20 62 e as published b
00e0: 79 20 74 68 65 20 46 72 65 65 20 53 6f 66 74 77 y the Free Softw
00f0: 61 72 65 20 46 6f 75 6e 64 61 74 69 6f 6e 3b 20 are Foundation;
0100: 65 69 74 68 65 72 0a 2a 2a 20 76 65 72 73 69 6f either.** versio
0110: 6e 20 32 20 6f 66 20 74 68 65 20 4c 69 63 65 6e n 2 of the Licen
0120: 73 65 2c 20 6f 72 20 28 61 74 20 79 6f 75 72 20 se, or (at your
0130: 6f 70 74 69 6f 6e 29 20 61 6e 79 20 6c 61 74 65 option) any late
0140: 72 20 76 65 72 73 69 6f 6e 2e 0a 2a 2a 0a 2a 2a r version..**.**
0150: 20 54 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 This program is
0160: 20 64 69 73 74 72 69 62 75 74 65 64 20 69 6e 20 distributed in
0170: 74 68 65 20 68 6f 70 65 20 74 68 61 74 20 69 74 the hope that it
0180: 20 77 69 6c 6c 20 62 65 20 75 73 65 66 75 6c 2c will be useful,
0190: 0a 2a 2a 20 62 75 74 20 57 49 54 48 4f 55 54 20 .** but WITHOUT
01a0: 41 4e 59 20 57 41 52 52 41 4e 54 59 3b 20 77 69 ANY WARRANTY; wi
01b0: 74 68 6f 75 74 20 65 76 65 6e 20 74 68 65 20 69 thout even the i
01c0: 6d 70 6c 69 65 64 20 77 61 72 72 61 6e 74 79 20 mplied warranty
01d0: 6f 66 0a 2a 2a 20 4d 45 52 43 48 41 4e 54 41 42 of.** MERCHANTAB
01e0: 49 4c 49 54 59 20 6f 72 20 46 49 54 4e 45 53 53 ILITY or FITNESS
01f0: 20 46 4f 52 20 41 20 50 41 52 54 49 43 55 4c 41 FOR A PARTICULA
0200: 52 20 50 55 52 50 4f 53 45 2e 20 20 53 65 65 20 R PURPOSE. See
0210: 74 68 65 20 47 4e 55 0a 2a 2a 20 47 65 6e 65 72 the GNU.** Gener
0220: 61 6c 20 50 75 62 6c 69 63 20 4c 69 63 65 6e 73 al Public Licens
0230: 65 20 66 6f 72 20 6d 6f 72 65 20 64 65 74 61 69 e for more detai
0240: 6c 73 2e 0a 2a 2a 0a 2a 2a 20 59 6f 75 20 73 68 ls..**.** You sh
0250: 6f 75 6c 64 20 68 61 76 65 20 72 65 63 65 69 76 ould have receiv
0260: 65 64 20 61 20 63 6f 70 79 20 6f 66 20 74 68 65 ed a copy of the
0270: 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 50 75 62 GNU General Pub
0280: 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 61 lic.** License a
0290: 6c 6f 6e 67 20 77 69 74 68 20 74 68 69 73 20 6c long with this l
02a0: 69 62 72 61 72 79 3b 20 69 66 20 6e 6f 74 2c 20 ibrary; if not,
02b0: 77 72 69 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 write to the.**
02c0: 46 72 65 65 20 53 6f 66 74 77 61 72 65 20 46 6f Free Software Fo
02d0: 75 6e 64 61 74 69 6f 6e 2c 20 49 6e 63 2e 2c 20 undation, Inc.,
02e0: 35 39 20 54 65 6d 70 6c 65 20 50 6c 61 63 65 20 59 Temple Place
02f0: 2d 20 53 75 69 74 65 20 33 33 30 2c 0a 2a 2a 20 - Suite 330,.**
0300: 42 6f 73 74 6f 6e 2c 20 4d 41 20 20 30 32 31 31 Boston, MA 0211
0310: 31 2d 31 33 30 37 2c 20 55 53 41 2e 0a 2a 2a 0a 1-1307, USA..**.
0320: 2a 2a 20 41 75 74 68 6f 72 20 63 6f 6e 74 61 63 ** Author contac
0330: 74 20 69 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a t information:.*
0340: 2a 20 20 20 64 72 68 40 68 77 61 63 69 2e 63 6f * drh@hwaci.co
0350: 6d 0a 2a 2a 20 20 20 68 74 74 70 3a 2f 2f 77 77 m.** http://ww
0360: 77 2e 68 77 61 63 69 2e 63 6f 6d 2f 64 72 68 2f w.hwaci.com/drh/
0370: 0a 2a 2a 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a .**.************
0380: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
0390: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
03a0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
03b0: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a ****************
03c0: 2a 2a 2a 0a 2a 2a 0a 2a 2a 20 49 6d 70 6c 65 6d ***.**.** Implem
03d0: 65 6e 74 61 74 69 6f 6e 20 6f 66 20 74 68 65 20 entation of the
03e0: 73 65 76 65 72 61 6c 20 73 74 72 69 6e 67 20 66 several string f
03f0: 6f 72 6d 61 74 74 69 6e 67 20 72 6f 75 74 69 6e ormatting routin
0400: 65 73 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64 65 20 es..*/.#include
0410: 3c 61 73 73 65 72 74 2e 68 3e 0a 23 69 6e 63 6c <assert.h>.#incl
0420: 75 64 65 20 22 63 6f 6e 66 69 67 2e 68 22 0a 23 ude "config.h".#
0430: 69 6e 63 6c 75 64 65 20 22 73 74 72 69 6e 67 5f include "string_
0440: 78 66 6f 72 6d 2e 68 22 0a 0a 23 69 66 20 49 4e xform.h"..#if IN
0450: 54 45 52 46 41 43 45 0a 2f 2a 2a 0a 73 74 72 69 TERFACE./**.stri
0460: 6e 67 5f 75 6e 61 72 79 5f 78 66 6f 72 6d 5f 66 ng_unary_xform_f
0470: 20 69 73 20 61 20 74 79 70 65 64 65 66 20 66 6f is a typedef fo
0480: 72 20 66 75 6e 63 73 20 77 69 74 68 20 74 68 65 r funcs with the
0490: 20 66 6f 6c 6c 6f 77 69 6e 67 20 70 6f 6c 69 63 following polic
04a0: 79 3a 0a 0a 54 68 65 79 20 61 63 63 65 70 74 20 y:..They accept
04b0: 61 20 63 6f 6e 73 74 20 73 74 72 69 6e 67 20 77 a const string w
04c0: 68 69 63 68 20 74 68 65 79 20 74 68 65 6e 20 74 hich they then t
04d0: 72 61 6e 73 66 6f 72 6d 20 69 6e 74 6f 20 73 6f ransform into so
04e0: 6d 65 20 6f 74 68 65 72 0a 66 6f 72 6d 2e 20 54 me other.form. T
04f0: 68 65 79 20 72 65 74 75 72 6e 20 61 20 74 72 61 hey return a tra
0500: 6e 73 66 6f 72 6d 65 64 20 63 6f 70 79 2c 20 77 nsformed copy, w
0510: 68 69 63 68 20 74 68 65 20 63 61 6c 6c 65 72 20 hich the caller
0520: 69 73 20 72 65 73 70 6f 6e 73 69 62 6c 65 0a 66 is responsible.f
0530: 6f 72 20 66 72 65 65 69 6e 67 2e 0a 0a 54 68 65 or freeing...The
0540: 20 69 6e 74 65 6e 74 69 6f 6e 20 6f 66 20 74 68 intention of th
0550: 69 73 20 69 73 20 74 6f 20 70 72 6f 76 69 64 65 is is to provide
0560: 20 61 20 77 61 79 20 66 6f 72 20 61 20 67 65 6e a way for a gen
0570: 65 72 69 63 20 71 75 65 72 79 20 72 6f 75 74 69 eric query routi
0580: 6e 65 0a 74 6f 20 66 6f 72 6d 61 74 20 73 70 65 ne.to format spe
0590: 63 69 66 69 63 20 63 6f 6c 75 6d 6e 20 64 61 74 cific column dat
05a0: 61 20 28 65 2e 67 2e 20 74 72 61 6e 73 66 6f 72 a (e.g. transfor
05b0: 6d 20 61 6e 20 6f 62 6a 65 63 74 20 49 44 20 69 m an object ID i
05c0: 6e 74 6f 20 61 0a 6c 69 6e 6b 20 74 6f 20 74 68 nto a.link to th
05d0: 61 74 20 6f 62 6a 65 63 74 29 2e 0a 2a 2f 0a 74 at object)..*/.t
05e0: 79 70 65 64 65 66 20 63 68 61 72 20 2a 20 28 2a ypedef char * (*
05f0: 73 74 72 69 6e 67 5f 75 6e 61 72 79 5f 78 66 6f string_unary_xfo
0600: 72 6d 5f 66 29 28 20 63 68 61 72 20 63 6f 6e 73 rm_f)( char cons
0610: 74 20 2a 20 29 3b 0a 0a 23 65 6e 64 69 66 20 2f t * );..#endif /
0620: 2f 20 49 4e 54 45 52 46 41 43 45 0a 0a 23 69 66 / INTERFACE..#if
0630: 20 30 0a 2f 2a 2a 20 41 20 6e 6f 2d 6f 70 20 74 0./** A no-op t
0640: 72 61 6e 73 66 6f 72 6d 65 72 20 77 68 69 63 68 ransformer which
0650: 20 63 61 6e 20 62 65 20 75 73 65 64 20 61 73 20 can be used as
0660: 61 20 70 6c 61 63 65 68 6f 6c 64 65 72 2e 0a 0a a placeholder...
0670: 43 6f 6e 66 6f 72 6d 73 20 74 6f 20 74 68 65 20 Conforms to the
0680: 73 74 72 69 6e 67 5f 75 6e 61 72 79 5f 78 66 6f string_unary_xfo
0690: 72 6d 5f 66 20 74 79 70 65 64 65 66 27 73 20 70 rm_f typedef's p
06a0: 6f 6c 69 63 69 65 73 2e 0a 2a 2f 0a 63 68 61 72 olicies..*/.char
06b0: 20 2a 20 73 74 72 78 66 6f 72 6d 5f 63 6f 70 79 * strxform_copy
06c0: 28 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 20 75 ( char const * u
06d0: 75 69 64 20 29 0a 7b 0a 20 20 69 6e 74 20 6c 65 uid ).{. int le
06e0: 6e 20 3d 20 73 74 72 6c 65 6e 28 75 75 69 64 29 n = strlen(uuid)
06f0: 20 2b 20 31 3b 0a 20 20 63 68 61 72 20 2a 20 72 + 1;. char * r
0700: 65 74 20 3d 20 28 63 68 61 72 20 2a 29 20 6d 61 et = (char *) ma
0710: 6c 6c 6f 63 28 20 6c 65 6e 20 29 3b 0a 20 20 72 lloc( len );. r
0720: 65 74 5b 6c 65 6e 5d 20 3d 20 27 5c 30 27 3b 0a et[len] = '\0';.
0730: 20 20 73 74 72 6e 63 70 79 28 20 72 65 74 2c 20 strncpy( ret,
0740: 75 75 69 64 2c 20 6c 65 6e 2d 31 20 29 3b 0a 20 uuid, len-1 );.
0750: 20 72 65 74 75 72 6e 20 72 65 74 3b 0a 7d 0a 23 return ret;.}.#
0760: 65 6e 64 69 66 0a 0a 2f 2a 2a 0a 52 65 74 75 72 endif../**.Retur
0770: 6e 73 20 61 20 68 79 70 65 72 6c 69 6e 6b 20 74 ns a hyperlink t
0780: 6f 20 75 75 69 64 2e 0a 0a 43 6f 6e 66 6f 72 6d o uuid...Conform
0790: 73 20 74 6f 20 74 68 65 20 73 74 72 69 6e 67 5f s to the string_
07a0: 75 6e 61 72 79 5f 78 66 6f 72 6d 5f 66 20 74 79 unary_xform_f ty
07b0: 70 65 64 65 66 27 73 20 70 6f 6c 69 63 69 65 73 pedef's policies
07c0: 2e 0a 2a 2f 0a 63 68 61 72 20 2a 20 73 74 72 78 ..*/.char * strx
07d0: 66 6f 72 6d 5f 6c 69 6e 6b 5f 74 6f 5f 75 75 69 form_link_to_uui
07e0: 64 28 20 63 68 61 72 20 63 6f 6e 73 74 20 2a 20 d( char const *
07f0: 75 75 69 64 20 29 0a 7b 0a 20 20 63 6f 6e 73 74 uuid ).{. const
0800: 20 69 6e 74 20 6f 66 66 73 65 74 20 3d 20 31 30 int offset = 10
0810: 3b 0a 20 20 63 68 61 72 20 73 68 6f 72 74 6e 61 ;. char shortna
0820: 6d 65 5b 6f 66 66 73 65 74 2b 31 5d 3b 0a 20 20 me[offset+1];.
0830: 73 68 6f 72 74 6e 61 6d 65 5b 6f 66 66 73 65 74 shortname[offset
0840: 5d 20 3d 20 27 5c 30 27 3b 0a 20 20 6d 65 6d 63 ] = '\0';. memc
0850: 70 79 28 20 73 68 6f 72 74 6e 61 6d 65 2c 20 75 py( shortname, u
0860: 75 69 64 2c 20 6f 66 66 73 65 74 20 29 3b 0a 20 uid, offset );.
0870: 20 72 65 74 75 72 6e 20 6d 70 72 69 6e 74 66 28 return mprintf(
0880: 22 3c 74 74 3e 3c 61 20 68 72 65 66 3d 27 25 73 "<tt><a href='%s
0890: 2f 76 69 6e 66 6f 2f 25 73 27 3e 3c 73 70 61 6e /vinfo/%s'><span
08a0: 20 73 74 79 6c 65 3d 27 66 6f 6e 74 2d 73 69 7a style='font-siz
08b0: 65 3a 31 2e 35 65 6d 27 3e 22 0a 20 20 20 20 20 e:1.5em'>".
08c0: 20 20 20 20 20 20 20 20 20 20 20 20 22 25 73 3c "%s<
08d0: 2f 73 70 61 6e 3e 25 73 3c 2f 61 3e 3c 2f 74 74 /span>%s</a></tt
08e0: 3e 22 2c 0a 20 20 20 20 20 20 20 20 20 20 20 20 >",.
08f0: 20 20 20 20 20 67 2e 7a 42 61 73 65 55 52 4c 2c g.zBaseURL,
0900: 20 75 75 69 64 2c 20 73 68 6f 72 74 6e 61 6d 65 uuid, shortname
0910: 2c 20 75 75 69 64 2b 6f 66 66 73 65 74 20 29 3b , uuid+offset );
0920: 0a 7d 0a 0a 2f 2a 2a 20 52 65 74 75 72 6e 73 20 .}../** Returns
0930: 61 20 68 79 70 65 72 6c 69 6e 6b 20 74 6f 20 74 a hyperlink to t
0940: 68 65 20 67 69 76 65 6e 20 74 61 67 2e 0a 0a 43 he given tag...C
0950: 6f 6e 66 6f 72 6d 73 20 74 6f 20 74 68 65 20 73 onforms to the s
0960: 74 72 69 6e 67 5f 75 6e 61 72 79 5f 78 66 6f 72 tring_unary_xfor
0970: 6d 5f 66 20 74 79 70 65 64 65 66 27 73 20 70 6f m_f typedef's po
0980: 6c 69 63 69 65 73 2e 0a 2a 2f 0a 63 68 61 72 20 licies..*/.char
0990: 2a 20 73 74 72 78 66 6f 72 6d 5f 6c 69 6e 6b 5f * strxform_link_
09a0: 74 6f 5f 74 61 67 69 64 28 20 63 68 61 72 20 63 to_tagid( char c
09b0: 6f 6e 73 74 20 2a 20 74 61 67 69 64 20 29 0a 7b onst * tagid ).{
09c0: 0a 20 20 72 65 74 75 72 6e 20 6d 70 72 69 6e 74 . return mprint
09d0: 66 28 20 22 3c 61 20 68 72 65 66 3d 27 25 73 2f f( "<a href='%s/
09e0: 74 61 67 76 69 65 77 3f 74 61 67 69 64 3d 25 73 tagview?tagid=%s
09f0: 27 3e 25 73 3c 2f 61 3e 22 2c 0a 20 20 20 20 20 '>%s</a>",.
0a00: 20 20 20 20 20 20 20 20 20 20 20 20 20 67 2e 7a g.z
0a10: 42 61 73 65 55 52 4c 2c 20 74 61 67 69 64 2c 20 BaseURL, tagid,
0a20: 74 61 67 69 64 20 29 3b 0a 7d 0a 0a 2f 2a 2a 20 tagid );.}../**
0a30: 52 65 74 75 72 6e 73 20 61 20 68 79 70 65 72 6c Returns a hyperl
0a40: 69 6e 6b 20 74 6f 20 74 68 65 20 6e 61 6d 65 64 ink to the named
0a50: 20 74 61 67 2e 0a 0a 43 6f 6e 66 6f 72 6d 73 20 tag...Conforms
0a60: 74 6f 20 74 68 65 20 73 74 72 69 6e 67 5f 75 6e to the string_un
0a70: 61 72 79 5f 78 66 6f 72 6d 5f 66 20 74 79 70 65 ary_xform_f type
0a80: 64 65 66 27 73 20 70 6f 6c 69 63 69 65 73 2e 0a def's policies..
0a90: 2a 2f 0a 63 68 61 72 20 2a 20 73 74 72 78 66 6f */.char * strxfo
0aa0: 72 6d 5f 6c 69 6e 6b 5f 74 6f 5f 74 61 67 6e 61 rm_link_to_tagna
0ab0: 6d 65 28 20 63 68 61 72 20 63 6f 6e 73 74 20 2a me( char const *
0ac0: 20 74 61 67 69 64 20 29 0a 7b 0a 20 20 72 65 74 tagid ).{. ret
0ad0: 75 72 6e 20 6d 70 72 69 6e 74 66 28 20 22 3c 61 urn mprintf( "<a
0ae0: 20 68 72 65 66 3d 27 25 73 2f 74 61 67 76 69 65 href='%s/tagvie
0af0: 77 2f 25 73 27 3e 25 73 3c 2f 61 3e 22 2c 0a 20 w/%s'>%s</a>",.
0b00: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20
0b10: 20 67 2e 7a 42 61 73 65 55 52 4c 2c 20 74 61 67 g.zBaseURL, tag
0b20: 69 64 2c 20 74 61 67 69 64 20 29 3b 0a 7d 0a 0a id, tagid );.}..
0b30: 0a 0a ..