File: tcltest.cc

package info (click to toggle)
wvstreams 4.6.1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 6,820 kB
  • ctags: 7,837
  • sloc: cpp: 64,203; ansic: 4,154; sh: 4,094; makefile: 549; perl: 402
file content (93 lines) | stat: -rw-r--r-- 2,479 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
 * Worldvisions Weaver Software:
 *   Copyright (C) 1997-2002 Net Integration Technologies, Inc.
 */
#include "wvtclstring.h"
#include "wvstringlist.h"
#include "wvstringmask.h"

int main(int argc, char **argv)
{
    WvStringMask nasties(WVTCL_NASTY_SPACES);
    WvStringMask splitchars(WVTCL_SPLITCHARS);

    bool bad = false;
    
    // correct output (all on one line):
    //   'chicken hammer {} {banana split} {shameless{frog}parts}
    //   big\}monkey\ \{potatoes {hammer\}time} {"quotable quote"}'
    static const char *strarray[] = {
	    "chicken",
	    "hammer",
	    "",
	    "banana split",
            "split\nends",
	    "shameless{frog}parts",
	    "big}monkey {potatoes",
	    "hammer\\}time",
	    "\"quotable quote\"",
	    NULL
    };
    WvStringList l;
    l.fill(strarray);
    
    WvString ls(wvtcl_encode(l, nasties, splitchars));
    printf("     List: '%s'\n", ls.cstr());
    printf("Unescaped: '%s'\n", wvtcl_unescape(ls).cstr());
    
    // wvtcl_encode (like the real tcl list encoder) will never put things
    // in quotes by itself.  However, the list decoder needs to be able to
    // handle them, in case the user provides them by hand.  So we'll just
    // tack some quoted strings onto the end.
    // 
    // We also take this opportunity to test handling of whitespace.
    // 
    ls.append("    \n  \t  \"unquotable quote\"\r\n");
    ls.append("  \"{embraced\\nunquotable}\"  \"quote\"whacker");
    
    // add the expected new strings to the end of the main list, for comparison
    // purposes below.
    l.append(new WvString("unquotable quote"), true);
    l.append(new WvString("{embraced\nunquotable}"), true);
    l.append(new WvString("quote"), true);
    l.append(new WvString("whacker"), true);
    
    printf("\nList split results:\n");
    WvStringList l2;
    wvtcl_decode(l2, ls, splitchars);
    
    WvStringList::Iter i(l), i2(l2);
    for (i.rewind(), i2.rewind(); i.next(), i2.next(), true; )
    {
	if (!i.cur() && !i2.cur())
	    break;
	
	if (!i.cur())
	{
	    printf("Extra element in list 2: '%s'\n", i2->cstr());
	    bad = true;
	    break;
	}
	
	if (!i2.cur())
	{
	    printf("Extra element in list 1: '%s'\n", i->cstr());
	    bad = true;
	    break;
	}
	
	printf("   '%s'\n", i2->cstr());
	
	if (*i != *i2)
	{
	    printf("     --> should be '%s'\n", i->cstr());
	    bad = true;
	}
    }
    
    if (bad)
	printf("Lists don't match!\n");
    else
	printf("Lists match.\n");
    return bad;
}