APUE Learning Example Source Code
guowenxue
2019-06-26 157be0b0d4c7d4809cfcafc76235cc18388378c8
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <cstdio>
#include <cstring>
#include <mosquittopp.h>
 
void print_error(const char *topic, char **topics, int topic_count)
{
    int i;
    printf("TOPIC: %s\n", topic);
    printf("TOKENS: ");
    for(i=0; i<topic_count; i++){
        printf("%s", topics[i]);
        if(i+1<topic_count){
            printf("/");
        }
    }
    printf("\n");
}
 
int main(int argc, char *argv[])
{
    char **topics;
    int topic_count;
 
    if(mosqpp::sub_topic_tokenise("topic", &topics, &topic_count)){
        printf("Out of memory.\n");
        return 1;
    }
    if(topic_count != 1 || strcmp(topics[0], "topic")){
        print_error("topic", topics, topic_count);
        return 1;
    }
 
    if(mosqpp::sub_topic_tokenise("a/deep/topic/hierarchy", &topics, &topic_count)){
        printf("Out of memory.\n");
        return 1;
    }
    if(topic_count != 4
            || strcmp(topics[0], "a")
            || strcmp(topics[1], "deep")
            || strcmp(topics[2], "topic")
            || strcmp(topics[3], "hierarchy")){
        print_error("a/deep/topic/hierarchy", topics, topic_count);
        return 1;
    }
 
    if(mosqpp::sub_topic_tokenise("/a/deep/topic/hierarchy", &topics, &topic_count)){
        printf("Out of memory.\n");
        return 1;
    }
    if(topic_count != 5
            || topics[0]
            || strcmp(topics[1], "a")
            || strcmp(topics[2], "deep")
            || strcmp(topics[3], "topic")
            || strcmp(topics[4], "hierarchy")){
        print_error("/a/deep/topic/hierarchy", topics, topic_count);
        return 1;
    }
 
    if(mosqpp::sub_topic_tokenise("a/b/c", &topics, &topic_count)){
        printf("Out of memory.\n");
        return 1;
    }
    if(topic_count != 3
            || strcmp(topics[0], "a")
            || strcmp(topics[1], "b")
            || strcmp(topics[2], "c")){
        print_error("a/b/c", topics, topic_count);
        return 1;
    }
 
    if(mosqpp::sub_topic_tokenise("/a/b/c", &topics, &topic_count)){
        printf("Out of memory.\n");
        return 1;
    }
    if(topic_count != 4
            || topics[0]
            || strcmp(topics[1], "a")
            || strcmp(topics[2], "b")
            || strcmp(topics[3], "c")){
        print_error("/a/b/c", topics, topic_count);
        return 1;
    }
 
    if(mosqpp::sub_topic_tokenise("a///hierarchy", &topics, &topic_count)){
        printf("Out of memory.\n");
        return 1;
    }
    if(topic_count != 4
            || strcmp(topics[0], "a")
            || topics[1]
            || topics[2]
            || strcmp(topics[3], "hierarchy")){
        print_error("a///hierarchy", topics, topic_count);
        return 1;
    }
 
    if(mosqpp::sub_topic_tokenise("/a///hierarchy", &topics, &topic_count)){
        printf("Out of memory.\n");
        return 1;
    }
    if(topic_count != 5
            || topics[0]
            || strcmp(topics[1], "a")
            || topics[2]
            || topics[3]
            || strcmp(topics[4], "hierarchy")){
        print_error("/a///hierarchy", topics, topic_count);
        return 1;
    }
 
    if(mosqpp::sub_topic_tokenise("/a///hierarchy/", &topics, &topic_count)){
        printf("Out of memory.\n");
        return 1;
    }
    if(topic_count != 6
            || topics[0]
            || strcmp(topics[1], "a")
            || topics[2]
            || topics[3]
            || strcmp(topics[4], "hierarchy")
            || topics[3]){
        print_error("/a///hierarchy/", topics, topic_count);
        return 1;
    }
 
    return 0;
}