| 1 |
<?xml version="1.0" encoding="utf-8"?> |
| 2 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> |
| 3 |
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en"> |
| 4 |
<head> |
| 5 |
<title>Redland RDF Application Framework - Hacking Redland librdf</title> |
| 6 |
<style type="text/css"> |
| 7 |
<!-- |
| 8 |
pre |
| 9 |
{ |
| 10 |
margin: 1em 4em 1em 4em; |
| 11 |
background-color: #eee; |
| 12 |
padding: 0.5em; |
| 13 |
border-color: #006; |
| 14 |
border-width: 1px; |
| 15 |
border-style: dotted; |
| 16 |
} |
| 17 |
--> |
| 18 |
</style> |
| 19 |
</head> |
| 20 |
<body> |
| 21 |
<h1>Hacking Redland librdf</h1> |
| 22 |
|
| 23 |
<p>2011-08-23</p> |
| 24 |
|
| 25 |
<p><a href="http://www.dajobe.org/">Dave Beckett</a></p> |
| 26 |
|
| 27 |
<h2>Commits</h2> |
| 28 |
|
| 29 |
<p>Should be:</p> |
| 30 |
|
| 31 |
<ul> |
| 32 |
<li>Licensed with the same license as Redland librdf.</li> |
| 33 |
<li>Patches in GNU <code>diff -u</code> unifed context format preferred.</li> |
| 34 |
<li>Include tests if they add new features.</li> |
| 35 |
</ul> |
| 36 |
|
| 37 |
<h2>Code Style</h2> |
| 38 |
|
| 39 |
<p>Do not make large commits that only change code style unless you have |
| 40 |
previously had this agreed or it is in code under major refactoring |
| 41 |
where a diff is not a large concern. There is always <code>diff -b</code> when |
| 42 |
large code style (whitespace) changes are made.</p> |
| 43 |
|
| 44 |
<h3>Indenting</h3> |
| 45 |
|
| 46 |
<p>2 spaces. No tabs.</p> |
| 47 |
|
| 48 |
<p>All code must be wrapped to 80 chars as far as is possible. Function |
| 49 |
definitions or calls should indent the parameters to the left <code>(</code>.</p> |
| 50 |
|
| 51 |
<p>Redland libraries use very long function names following the naming |
| 52 |
convention which can make linebreaking very hard. In this case, |
| 53 |
indent function parameters on new lines 4 spaces after the function |
| 54 |
name like this:</p> |
| 55 |
|
| 56 |
<pre><code> |
| 57 |
var = function_name_with_very_long_name_that_is_hard_to_wrap_args( |
| 58 |
argument1_with_very_long_name_or_expression, |
| 59 |
argument2, |
| 60 |
..) |
| 61 |
</code></pre> |
| 62 |
|
| 63 |
<p>Use no space between a keyword followed by braces argument. |
| 64 |
For example, use <code>if(cond)</code> rather than <code>if (cond)</code> (ditto for |
| 65 |
while, do etc.) and |
| 66 |
<code>functionname(...)</code> rather than <code>functionname (...)</code> in |
| 67 |
both definition and calls of functions.</p> |
| 68 |
|
| 69 |
<p>There is nothing wrong with introducing a variable to break up a very |
| 70 |
long function call argument.</p> |
| 71 |
|
| 72 |
<h3>Expressions</h3> |
| 73 |
|
| 74 |
<p>Put spaces around operators in expressions, assignments, tests, conditions</p> |
| 75 |
|
| 76 |
<p>GOOD:</p> |
| 77 |
|
| 78 |
<ul> |
| 79 |
<li><code>a += 2 * x</code></li> |
| 80 |
<li><code>if(a < 2)</code></li> |
| 81 |
</ul> |
| 82 |
|
| 83 |
<p>BAD:</p> |
| 84 |
|
| 85 |
<ul> |
| 86 |
<li><code>a+=2*x</code></li> |
| 87 |
<li><code>if(a<2)</code></li> |
| 88 |
</ul> |
| 89 |
|
| 90 |
<p>When comparing to <code>0</code> or a <code>NULL</code> pointer, use the idiomatic form |
| 91 |
that has no comparison.</p> |
| 92 |
|
| 93 |
<p>GOOD:</p> |
| 94 |
|
| 95 |
<ul> |
| 96 |
<li><code>if(!ptr)</code></li> |
| 97 |
<li><code>if(!index)</code></li> |
| 98 |
</ul> |
| 99 |
|
| 100 |
<p>BAD:</p> |
| 101 |
|
| 102 |
<ul> |
| 103 |
<li><code>if(ptr == NULL)</code></li> |
| 104 |
<li><code>if(index == 0)</code></li> |
| 105 |
<li><code>if(0 == index)</code></li> |
| 106 |
</ul> |
| 107 |
|
| 108 |
<p>When comparing a variable to a constant, the code has currently used |
| 109 |
<code>if(var == constant)</code> rather than the slightly safer, and easier to |
| 110 |
compile check, <code>if(constant == var)</code>.</p> |
| 111 |
|
| 112 |
<h3>Blocks</h3> |
| 113 |
|
| 114 |
<p>In general add {}s around blocks in if else chains when one of the blocks |
| 115 |
has more than 1 line of code. Try not to mix, but the final case if it |
| 116 |
is one line, can be braceless.</p> |
| 117 |
|
| 118 |
<pre><code> |
| 119 |
if(var == 1) { |
| 120 |
... multiple lines of code ... |
| 121 |
} else { |
| 122 |
... multiple lines of code ... |
| 123 |
} |
| 124 |
</code></pre> |
| 125 |
|
| 126 |
<p>or</p> |
| 127 |
|
| 128 |
<pre><code> |
| 129 |
if(var == 1) |
| 130 |
... one line of code |
| 131 |
else |
| 132 |
... one line of code |
| 133 |
</code></pre> |
| 134 |
|
| 135 |
<p>or</p> |
| 136 |
|
| 137 |
<pre><code> |
| 138 |
if(var == 1) { |
| 139 |
... multiple lines of code ... |
| 140 |
} else if(var == 2) { |
| 141 |
... multiple lines of code and / or more if conditions ... |
| 142 |
} else |
| 143 |
... one line of code ... |
| 144 |
</code></pre> |
| 145 |
|
| 146 |
<h3>Switches</h3> |
| 147 |
|
| 148 |
<p>If using if else chains on an enumeration, don't do that, use a |
| 149 |
<code>switch()</code> which GCC can use to find missing cases when they get added.</p> |
| 150 |
|
| 151 |
<pre><code> |
| 152 |
switch(enum_var) { |
| 153 |
case ENUM_1: |
| 154 |
... code ... |
| 155 |
break; |
| 156 |
|
| 157 |
case ENUM_2: |
| 158 |
... code ... |
| 159 |
break; |
| 160 |
|
| 161 |
case ENUM_DONT_CARE: |
| 162 |
default: |
| 163 |
... code ... |
| 164 |
break; |
| 165 |
} |
| 166 |
</code></pre> |
| 167 |
|
| 168 |
<p>There should ALWAYS be a <code>default:</code> case.</p> |
| 169 |
|
| 170 |
<h3>Functions</h3> |
| 171 |
|
| 172 |
<p>Declare functions in this format:</p> |
| 173 |
|
| 174 |
<pre><code> |
| 175 |
returntype |
| 176 |
functionname(type1 param1, type2 param2, ...) |
| 177 |
{ |
| 178 |
type3 var1; |
| 179 |
type4 var2; |
| 180 |
|
| 181 |
... first line of code ... |
| 182 |
|
| 183 |
tidy: |
| 184 |
... cleanup code... |
| 185 |
|
| 186 |
return value; |
| 187 |
} |
| 188 |
</code></pre> |
| 189 |
|
| 190 |
<p>Notes:</p> |
| 191 |
|
| 192 |
<ul> |
| 193 |
<li>Declare one variable per line</li> |
| 194 |
<li>Declare all variables at the top of the function (K&R C style)</li> |
| 195 |
<li>You may declare variables in inner <code>{}</code> blocks. The |
| 196 |
form <code>if(1) { ... var decls ...; more code }</code> may be used but |
| 197 |
a code rewrite is preferable.</li> |
| 198 |
<li>If a label is used it <em>MUST</em> be used only for cleanup, and going |
| 199 |
forward in the code to the end of the function.</li> |
| 200 |
<li>Multiple <code>return</code> are allowed but for obvious error or result |
| 201 |
returns. Do not twist the code to enable a single return.</li> |
| 202 |
<li><code>goto</code> may be used for resource cleanup and result return |
| 203 |
where control flow only goes forward.</li> |
| 204 |
</ul> |
| 205 |
|
| 206 |
<h3>C Pre-Processor (CPP) Macros</h3> |
| 207 |
|
| 208 |
<p>Always define macros for internal constants and name the macros with |
| 209 |
the library prefix followed by a descriptive name in ALL CAPS such as:</p> |
| 210 |
|
| 211 |
<pre><code> |
| 212 |
#define LIBRDF_FOOBAR_BUFFER_SIZE 1234 |
| 213 |
</code></pre> |
| 214 |
|
| 215 |
<p>When evaluating macro symbols that may be undefined, always check the |
| 216 |
symbol is defined first. Like this:</p> |
| 217 |
|
| 218 |
<pre><code> |
| 219 |
#if defined(LIBRDF_DEBUG) && LIBRDF_DEBUG > 42 |
| 220 |
... do complex debugging stuff ... |
| 221 |
#endif |
| 222 |
</code></pre> |
| 223 |
|
| 224 |
<p>This is not needed for macros that are known to be defined, such as those |
| 225 |
checked by <code>configure</code> e.g.</p> |
| 226 |
|
| 227 |
<pre><code> |
| 228 |
#if RAPTOR_VERSION_DECIMAL > 20100 |
| 229 |
... do stuff that requires a raptor2 version 2.1.0 or newer ... |
| 230 |
#endif |
| 231 |
</code></pre> |
| 232 |
|
| 233 |
<p>since the above would be checked implicitly by <code>configure</code> using |
| 234 |
<code>pkg-config(1)</code> to validate Raptor 2 is present before getting to the |
| 235 |
code that tries to evaluate the value from <code>raptor2.h</code>.</p> |
| 236 |
|
| 237 |
<p>The debug macros that are used for printing out values when debugging |
| 238 |
is enabled do not need protection by <code>#if</code> or <code>#ifdef</code> and should be |
| 239 |
used like this:</p> |
| 240 |
|
| 241 |
<pre><code> |
| 242 |
LIBRDF_DEBUG1("Something wonderful happened\n"); |
| 243 |
|
| 244 |
LIBRDF_DEBUG2("Something %s happened\n", happening); |
| 245 |
</code></pre> |
| 246 |
|
| 247 |
<h3>Memory allocation</h3> |
| 248 |
|
| 249 |
<p>Allocating a zeroed out block of memory or a set of objects (calloc)</p> |
| 250 |
|
| 251 |
<pre><code> |
| 252 |
var = LIBRDF_CALLOC(type, count, size) |
| 253 |
</code></pre> |
| 254 |
|
| 255 |
<p>Prefering when <code>count</code> = 1 this form:</p> |
| 256 |
|
| 257 |
<pre><code> |
| 258 |
var = LIBRDF_CALLOC(type, 1, sizeof(*var)) |
| 259 |
</code></pre> |
| 260 |
|
| 261 |
<p>Allocating a block of memory:</p> |
| 262 |
|
| 263 |
<pre><code> |
| 264 |
var = LIBRDF_MALLOC(type, size) |
| 265 |
</code></pre> |
| 266 |
|
| 267 |
<p>Freeing memory: </p> |
| 268 |
|
| 269 |
<pre><code> |
| 270 |
LIBRDF_FREE(type, var) |
| 271 |
</code></pre> |
| 272 |
|
| 273 |
<p>The reasoning here is to make allocs mostly fit into 1 line without |
| 274 |
too much boilerplate and duplication of types.</p> |
| 275 |
|
| 276 |
<p>The macro names vary by library such as <code>RAPTOR_CALLOC</code> and |
| 277 |
<code>RASQAL_CALLOC</code> for Raptor and Rasqal respectively.</p> |
| 278 |
|
| 279 |
<h3>Documentation</h3> |
| 280 |
|
| 281 |
<p>Public functions, types, enumerations and defines must have autodocs - |
| 282 |
the structured comment block before the definition. This is read by |
| 283 |
<code>gtk-doc(1)</code> to generate reference API documentation.</p> |
| 284 |
|
| 285 |
<p>Format:</p> |
| 286 |
|
| 287 |
<pre><code> |
| 288 |
/** |
| 289 |
* functionname: |
| 290 |
* @param1: Description of first parameter |
| 291 |
* @param2: Description of second parameter (or NULL) |
| 292 |
* ... more params ... |
| 293 |
* |
| 294 |
* Short Description |
| 295 |
* |
| 296 |
* Long Description. |
| 297 |
* |
| 298 |
* Return value: return value |
| 299 |
*/ |
| 300 |
returntype |
| 301 |
functionname(...) |
| 302 |
{ |
| 303 |
... body ... |
| 304 |
} |
| 305 |
</code></pre> |
| 306 |
|
| 307 |
<p>The <em>Short Description</em> have several commmon forms:</p> |
| 308 |
|
| 309 |
<ul> |
| 310 |
<li>Constructor - creates a FOO object</li> |
| 311 |
<li>Destructor - destroys a FOO object</li> |
| 312 |
<li>Short description of regular function or method.</li> |
| 313 |
<li>INTERNAL - short description</li> |
| 314 |
</ul> |
| 315 |
|
| 316 |
<p>The latter is used for autodocs for internal functions either as |
| 317 |
internal documentation or for APIs that may one day be public.</p> |
| 318 |
|
| 319 |
<p>The <em>(or NULL)</em> phrase is used for pointer parameters that may be |
| 320 |
omitted. This is usually tested by the function as an assertion. |
| 321 |
In some functions there are more complex conditions on which optional |
| 322 |
parameters are allowed, these are described in the <em>Long Description</em>.</p> |
| 323 |
|
| 324 |
<p>The long description may also include a deprecation statement such as:</p> |
| 325 |
|
| 326 |
<pre><code> |
| 327 |
* @Deprecated: Use new_function() with foo = BAR |
| 328 |
</code></pre> |
| 329 |
|
| 330 |
<p>This must be indented to the left and will be used by the |
| 331 |
<code>gtk-doc(1)</code> document generator to provide a link to the replacement |
| 332 |
function and usage.</p> |
| 333 |
|
| 334 |
<h2>Commit Messages</h2> |
| 335 |
|
| 336 |
<p>The general standard for Redland libraries using GIT is a merge |
| 337 |
of the GIT standards format and GNU ChangeLog</p> |
| 338 |
|
| 339 |
<pre><code> |
| 340 |
First line summaries what commit does - this goes into the GIT short log |
| 341 |
|
| 342 |
(function1, function2): what changed |
| 343 |
|
| 344 |
(function3): Added, deprecating function4() |
| 345 |
|
| 346 |
(function4): Deleted, replaced by function3() |
| 347 |
|
| 348 |
struct foo gains field ... |
| 349 |
|
| 350 |
struct bar loses field ... |
| 351 |
|
| 352 |
enum blah gains new value BLAH_2 which ... |
| 353 |
</code></pre> |
| 354 |
|
| 355 |
<p>Use <code>name()</code> in the description for references to functions. |
| 356 |
Make sure to do (function1, function2) NOT (function1,function2) as |
| 357 |
it makes things easier to format later.</p> |
| 358 |
|
| 359 |
<p>Sometimes it's short enough (good) that it all can be done in the first |
| 360 |
line, pretty much only if it's a small change to a single function.</p> |
| 361 |
|
| 362 |
<p>If the change is trivial or a typo and (this is <em>IMPORTANT</em>) NOT a |
| 363 |
commit to code files, then the commit can start with '#'. This may |
| 364 |
get filtered out of commit log message notifications and ChangeLog.</p> |
| 365 |
|
| 366 |
<p>e.g. <code>#spelling</code> or <code>#ws</code> |
| 367 |
the latter is whitespace changes for some reason</p> |
| 368 |
|
| 369 |
<p>The changes will semi-automatically be added to the ChangeLog files |
| 370 |
following the GNU style, indented and word wrapped, and adding the list |
| 371 |
of files at the start. So the commit message above ends up looking |
| 372 |
something like:</p> |
| 373 |
|
| 374 |
<pre><code> |
| 375 |
2010-08-23 User Name <[email protected]> |
| 376 |
|
| 377 |
* dir/file1.c, dir2/file2.c: First line summaries what commit |
| 378 |
does - this goes into the GIT short log |
| 379 |
|
| 380 |
(function1, function2): what changed |
| 381 |
|
| 382 |
(function3): Added, deprecating function4() |
| 383 |
|
| 384 |
(function4): Deleted, replaced by function3() |
| 385 |
|
| 386 |
struct foo gains field ... |
| 387 |
|
| 388 |
struct bar loses field ... |
| 389 |
|
| 390 |
enum blah gains new value BLAH_2 which ... |
| 391 |
</code></pre> |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
|
| 398 |
<hr /> |
| 399 |
|
| 400 |
<p>Copyright (C) 2013 <a href="http://www.dajobe.org/">Dave Beckett</a></p> |
| 401 |
|
| 402 |
</body> |
| 403 |
</html> |