Added SILC Thread Queue API
[crypto.git] / lib / doc / programming_conv.html
1 <big><b>Programming Conventions</b></big>
2
3 <br />&nbsp;<br />
4 The SILC Toolkit has been programmed with a specific programming style that
5 is consistent across all libraries and interfaces.  The programming style
6 defines for example naming conventions for functions, structures, macros,
7 enumerations, and other constants.
8
9
10 <br />&nbsp;<br />&nbsp;<br />
11 <b>Naming Conventions</b>
12
13 <br />&nbsp;<br />
14 <b>Macros and Defines</b>
15
16 <br />&nbsp;<br />
17 Macros are always capitalised and include underscores to separate words
18 in the name.  All macros start with the "SILC_" prefix.  Example:
19
20 <br />&nbsp;<br />
21 <tt>
22 #define SILC_PACKET_PADLEN(__packetlen, __blocklen)      \<br />
23 &nbsp;&nbsp;SILC_PACKET_DEFAULT_PADLEN - (__packetlen) % \<br />
24 &nbsp;&nbsp;&nbsp;&nbsp;((__blocklen) ? (__blocklen) : SILC_PACKET_DEFAULT_PADLEN)
25 </tt>
26
27 <br />&nbsp;<br />
28 Also other defines (#define) are always capitalised and include 
29 underscores to separate words in the name.  Also all defines start with 
30 the "SILC_" prefix.
31
32 <br />&nbsp;<br />
33 <b>Structures</b>
34
35 <br />&nbsp;<br />
36 All structure names begin with "Silc" prefix, and the name is mixed-case,
37 for example: SilcClientConnection, SilcCommandPayload.  Many of the 
38 structures used in SILC are actually private structures, and application
39 cannot access them directly.  In these cases the structures are forward
40 declared in the public header, and the implementation of the structure
41 is in the source file.  In these case application does not need to know
42 the contents of the structure, and is usually provided with a helper API
43 to access the structure when needed.
44
45 <br />&nbsp;<br />
46 In the most of the cases the forward declaration for a structure is pointer, 
47 for example:
48
49 <br />&nbsp;<br />
50 <tt>typedef struct SilcClientStruct *SilcClient;</tt>
51
52 <br />&nbsp;<br />
53 Application should always use the type defined pointer instead of the
54 actual structure.
55
56 <br />&nbsp;<br />
57 <b>Functions</b>
58
59 <br />&nbsp;<br />
60 Function naming uses the common naming convention used in Toolkit.  All
61 functions are always lowercase and they use underscores.  The name of
62 the function always starts with prefix "silc_".  The name tells what
63 the function do.  The name of a function is constructed from following parts:
64
65 <br />&nbsp;<br />
66 <tt>silc_(module)_(function)</tt>
67
68 <br />&nbsp;<br />
69 The (module) is the library, or interface this functions is part of.  For
70 example: "cipher", "config", "command", "packet", etc.
71
72 <br />&nbsp;<br />
73 The (function) is the description of the functionality of the function.
74 For example: "read", "new_id", "register", "find_by_name", etc.  Examples:
75
76 <br />&nbsp;<br />
77 <tt>
78 silc_server_packet_send<br />
79 silc_server_packet_send_to_channel<br />
80 silc_idcache_del_by_id<br />
81 silc_schedule_init<br />
82 silc_protocol_excute_final<br />
83 silc_buffer_alloc
84 </tt>
85
86 <br />&nbsp;<br />
87 When function registers something the name of the function generally is
88 "silc_function_register" and unregistering is done with
89 "silc_function_unregister".  When function allocates something it
90 is "silc_function_alloc" and when freeing it is
91 "silc_function_free".  Respectively, with init/uninit functions.
92
93 <br />&nbsp;<br />
94 <b>Enumerations</b>
95
96 <br />&nbsp;<br />
97 Enumerations are always capitalised and include underscores to separate
98 words in the name.  All enumerations start with the "SILC_" prefix.  Also,
99 usually all enumerations are type defined to a specific name which can
100 be used as type for the enumeration.  Example:
101
102 <br />&nbsp;<br />
103 <tt>
104 typedef enum {<br />
105 &nbsp;&nbsp;SILC_EXAMPLE_ENUM_NONE,<br />
106 &nbsp;&nbsp;SILC_EXAMPLE_ENUM_LIST,<br />
107 &nbsp;&nbsp;SILC_EXAMPLE_ENUM_STATUS,<br />
108 } SilcExampleEnum;
109 </tt>
110
111 <br />&nbsp;<br />
112 The naming for the type definition for the enumerations follow the
113 normal naming convention; the name starts with "Silc" prefix and the
114 name is mixed-case.
115
116
117 <br />&nbsp;<br />&nbsp;<br />
118 <b>Layout</b>
119
120 <br />&nbsp;<br />
121 <b>Indentation</b>
122
123 <br />&nbsp;<br />
124 The indendation in the source code is 2 characters, and tabulators are
125 not used.  Example piece of code:
126
127 <br />&nbsp;<br />
128 <tt>
129 void silc_client_free(SilcClient client)<br />
130 {<br />
131 &nbsp;&nbsp;if (client) {<br />
132 &nbsp;&nbsp;&nbsp;&nbsp;if (client->rng)<br />
133 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;silc_rng_free(client->rng);<br />
134 &nbsp;&nbsp;&nbsp;&nbsp;silc_free(client);<br />
135 &nbsp;&nbsp;}<br />
136 }
137 </tt>
138
139 <br />&nbsp;<br />
140 <b>Placing Braces</b>
141
142 <br />&nbsp;<br />
143 Generally the braces placing the SILC code follows the K&R style; the
144 opening of the brace is put to the last on the line, and the closing brace
145 is on first on its own line, except for functions.  Examples:
146
147 <br />&nbsp;<br />
148 <tt>
149 if (condition) {<br />
150 &nbsp;&nbsp;silc_something();<br />
151 &nbsp;&nbsp;silc_something_more();<br />
152 }
153 </tt>
154
155 <br />&nbsp;<br />
156 <tt>
157 int silc_client_function()<br />
158 {<br />
159 &nbsp;&nbsp;return 0;<br />
160 }
161 </tt>
162
163 <br />&nbsp;<br />
164 <tt>
165 if (condition) {<br />
166 &nbsp;&nbsp;something;<br />
167 &nbsp;&nbsp;silc_something_more();<br />
168 } else {<br />
169 &nbsp;&nbsp;something_else;<br />
170 }
171 </tt>
172
173 <br />&nbsp;<br />
174 <tt>
175 if (condition) {<br />
176 &nbsp;&nbsp;something;<br />
177 &nbsp;&nbsp;silc_something_more();<br />
178 } else if (other_condition) {<br />
179 &nbsp;&nbsp;something;<br />
180 &nbsp;&nbsp;silc_something_more();<br />
181 } else {<br />
182 &nbsp;&nbsp;something_else;<br />
183 }
184 </tt>
185
186 <br />&nbsp;<br />
187 <b>Header Files</b>
188 <br />&nbsp;<br />
189
190 Standard anti-nesting method is used in the header files to avoid 
191 multiple inclusion of the header file.  Example:
192
193 <br />&nbsp;<br />
194 <tt>
195 #ifndef SILCHEADER_H<br />
196 #define SILCHEADER_H<br />
197 ...<br />
198 #endif /* SILCHEADER_H */
199 </tt>
200
201 <br />&nbsp;<br />
202 All public header files have the "silc" prefix in the filename, for example:
203 silcclient.h, silcprivate.h, silcutil.h.  There are other header files in
204 the Toolkit as well.  Application should not directly include these headers,
205 however if needed it may access them.
206
207 <br />&nbsp;<br />
208 Every header file also includes a copyright notice.