File: write-list.2gg

package info (click to toggle)
golf 601.4.41-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,824 kB
  • sloc: ansic: 20,020; sh: 1,171; makefile: 292
file content (167 lines) | stat: -rw-r--r-- 3,692 bytes parent folder | download
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
.TH GOLF 2gg $VERSION $DATE Development Tools
.SH NAME
write-list \-  (linked-list)
.SH PURPOSE
Write key/value pair into a linked list.

.SH SYNTAX

.RS 4
.EX

write-list <list> key <key> \\
    value <value> \\
    [ append [ <append> ] ] 

.EE
.RE

.SH DESCRIPTION
write-list adds a pair of key/value strings to the linked <list>, specified with <key> and <value> (in "key" and "value" clauses, collectively called an "element"). 

The key/value pair is added just prior to the list's current position, thus becoming a current element. 

If "append" clause is used without boolean variable <append>, or if <append> evaluates to true, then the element is added at the end of the list, and the list's current element becomes the newly added one.

.SH EXAMPLES
Add a key/value pair to the end of the list:

.RS 4
.EX

new-list mylist
write-list mylist key "mykey" value "myvalue" append

.EE
.RE

The following is a list that is process-scoped, i.e. it is a linked-list server, which can add, delete, read and position to various elements:

.RS 4
.EX

%% /llsrv public
    // Create linked list just once for the life of the process
    do-once
    new-list t process-scope  
    end-do-once

    // Get input parameters
    get-param op
    get-param key
    get-param data

    if-true op equal "add" // Add data to list
        // Make a copy of key,data so they are Golf-allocated
        write-list t key (key) value data append
        @Added [<<print-out key>>] value [<<print-out data>>]

    else-if op equal "delete" // Delete first data and obtain the value deleted
        position-list t first
        read-list t key (key) value val status st
        if-true st equal GG_ERR_EXIST
            @Not found 
        else-if
            // If found, then delete key and value
            @Deleted key [<<print-out key>>], [<<print-out val>>]
            delete-list t 
        end-if

    else-if op equal "next" // Position to next element
        position-list t next status st
        if-true st equal GG_OKAY
            @Okay
        else-if
            @Not found
        end-if

    else-if op equal "last" // Position to last element
        position-list t last status st
        if-true st equal GG_OKAY
            @Okay
        else-if
            @Not found
        end-if

    else-if op equal "previous" // Position to element
        position-list t previous status st
        if-true st equal GG_OKAY
            @Okay
        else-if
            @Not found
        end-if

    else-if op equal "first" // Get first element
        position-list t first status st
        if-true st equal GG_OKAY
            @Okay
        else-if
            @Not found
        end-if

    else-if op equal "query" // Get current element
        read-list t key (key) value val status st
        if-true st equal GG_ERR_EXIST
            @Not found
        else-if
            @Key [<<print-out key>>], value [<<print-out val>>]
        end-if

    else-if op equal "purge" // remove all, keep the list
        purge-list t
    end-if
%%

.EE
.RE

Create application:

.RS 4
.EX

sudo mgrg -i -u $(whoami) linkserver

.EE
.RE

Start the server:

.RS 4
.EX

mgrg -w 1 linkserver

.EE
.RE

Try it out:

.RS 4
.EX

gg -r --req="/llsrv/op=add/key=1/data=1" --exec --service
gg -r --req="/llsrv/op=add/key=2/data=2" --exec --service
gg -r --req="/llsrv/op=query" --exec --service
gg -r --req="/llsrv/op=previous" --exec --service
gg -r --req="/llsrv/op=query" --exec --service

.EE
.RE


.EE
.RE

.SH SEE ALSO
 Linked list

\fBdelete-list\fP  
\fBget-list\fP  
\fBnew-list\fP  
\fBposition-list\fP  
\fBpurge-list\fP  
\fBread-list\fP  
\fBwrite-list\fP   
See all 
\fBdocumentation\fP