Blame gtkmm-osx/jpeg-6b/coderules.doc

darco 56a656
IJG JPEG LIBRARY:  CODING RULES
darco 56a656
darco 56a656
Copyright (C) 1991-1996, Thomas G. Lane.
darco 56a656
This file is part of the Independent JPEG Group's software.
darco 56a656
For conditions of distribution and use, see the accompanying README file.
darco 56a656
darco 56a656
darco 56a656
Since numerous people will be contributing code and bug fixes, it's important
darco 56a656
to establish a common coding style.  The goal of using similar coding styles
darco 56a656
is much more important than the details of just what that style is.
darco 56a656
darco 56a656
In general we follow the recommendations of "Recommended C Style and Coding
darco 56a656
Standards" revision 6.1 (Cannon et al. as modified by Spencer, Keppel and
darco 56a656
Brader).  This document is available in the IJG FTP archive (see
darco 56a656
jpeg/doc/cstyle.ms.tbl.Z, or cstyle.txt.Z for those without nroff/tbl).
darco 56a656
darco 56a656
Block comments should be laid out thusly:
darco 56a656
darco 56a656
/*
darco 56a656
 *  Block comments in this style.
darco 56a656
 */
darco 56a656
darco 56a656
We indent statements in K&R style, e.g.,
darco 56a656
	if (test) {
darco 56a656
	  then-part;
darco 56a656
	} else {
darco 56a656
	  else-part;
darco 56a656
	}
darco 56a656
with two spaces per indentation level.  (This indentation convention is
darco 56a656
handled automatically by GNU Emacs and many other text editors.)
darco 56a656
darco 56a656
Multi-word names should be written in lower case with underscores, e.g.,
darco 56a656
multi_word_name (not multiWordName).  Preprocessor symbols and enum constants
darco 56a656
are similar but upper case (MULTI_WORD_NAME).  Names should be unique within
darco 56a656
the first fifteen characters.  (On some older systems, global names must be
darco 56a656
unique within six characters.  We accommodate this without cluttering the
darco 56a656
source code by using macros to substitute shorter names.)
darco 56a656
darco 56a656
We use function prototypes everywhere; we rely on automatic source code
darco 56a656
transformation to feed prototype-less C compilers.  Transformation is done
darco 56a656
by the simple and portable tool 'ansi2knr.c' (courtesy of Ghostscript).
darco 56a656
ansi2knr is not very bright, so it imposes a format requirement on function
darco 56a656
declarations: the function name MUST BEGIN IN COLUMN 1.  Thus all functions
darco 56a656
should be written in the following style:
darco 56a656
darco 56a656
LOCAL(int *)
darco 56a656
function_name (int a, char *b)
darco 56a656
{
darco 56a656
    code...
darco 56a656
}
darco 56a656
darco 56a656
Note that each function definition must begin with GLOBAL(type), LOCAL(type),
darco 56a656
or METHODDEF(type).  These macros expand to "static type" or just "type" as
darco 56a656
appropriate.  They provide a readable indication of the routine's usage and
darco 56a656
can readily be changed for special needs.  (For instance, special linkage
darco 56a656
keywords can be inserted for use in Windows DLLs.)
darco 56a656
darco 56a656
ansi2knr does not transform method declarations (function pointers in
darco 56a656
structs).  We handle these with a macro JMETHOD, defined as
darco 56a656
	#ifdef HAVE_PROTOTYPES
darco 56a656
	#define JMETHOD(type,methodname,arglist)  type (*methodname) arglist
darco 56a656
	#else
darco 56a656
	#define JMETHOD(type,methodname,arglist)  type (*methodname) ()
darco 56a656
	#endif
darco 56a656
which is used like this:
darco 56a656
	struct function_pointers {
darco 56a656
	  JMETHOD(void, init_entropy_encoder, (int somearg, jparms *jp));
darco 56a656
	  JMETHOD(void, term_entropy_encoder, (void));
darco 56a656
	};
darco 56a656
Note the set of parentheses surrounding the parameter list.
darco 56a656
darco 56a656
A similar solution is used for forward and external function declarations
darco 56a656
(see the EXTERN and JPP macros).
darco 56a656
darco 56a656
If the code is to work on non-ANSI compilers, we cannot rely on a prototype
darco 56a656
declaration to coerce actual parameters into the right types.  Therefore, use
darco 56a656
explicit casts on actual parameters whenever the actual parameter type is not
darco 56a656
identical to the formal parameter.  Beware of implicit conversions to "int".
darco 56a656
darco 56a656
It seems there are some non-ANSI compilers in which the sizeof() operator
darco 56a656
is defined to return int, yet size_t is defined as long.  Needless to say,
darco 56a656
this is brain-damaged.  Always use the SIZEOF() macro in place of sizeof(),
darco 56a656
so that the result is guaranteed to be of type size_t.
darco 56a656
darco 56a656
darco 56a656
The JPEG library is intended to be used within larger programs.  Furthermore,
darco 56a656
we want it to be reentrant so that it can be used by applications that process
darco 56a656
multiple images concurrently.  The following rules support these requirements:
darco 56a656
darco 56a656
1. Avoid direct use of file I/O, "malloc", error report printouts, etc;
darco 56a656
pass these through the common routines provided.
darco 56a656
darco 56a656
2. Minimize global namespace pollution.  Functions should be declared static
darco 56a656
wherever possible.  (Note that our method-based calling conventions help this
darco 56a656
a lot: in many modules only the initialization function will ever need to be
darco 56a656
called directly, so only that function need be externally visible.)  All
darco 56a656
global function names should begin with "jpeg_", and should have an
darco 56a656
abbreviated name (unique in the first six characters) substituted by macro
darco 56a656
when NEED_SHORT_EXTERNAL_NAMES is set.
darco 56a656
darco 56a656
3. Don't use global variables; anything that must be used in another module
darco 56a656
should be in the common data structures.
darco 56a656
darco 56a656
4. Don't use static variables except for read-only constant tables.  Variables
darco 56a656
that should be private to a module can be placed into private structures (see
darco 56a656
the system architecture document, structure.doc).
darco 56a656
darco 56a656
5. Source file names should begin with "j" for files that are part of the
darco 56a656
library proper; source files that are not part of the library, such as cjpeg.c
darco 56a656
and djpeg.c, do not begin with "j".  Keep source file names to eight
darco 56a656
characters (plus ".c" or ".h", etc) to make life easy for MS-DOSers.  Keep
darco 56a656
compression and decompression code in separate source files --- some
darco 56a656
applications may want only one half of the library.
darco 56a656
darco 56a656
Note: these rules (particularly #4) are not followed religiously in the
darco 56a656
modules that are used in cjpeg/djpeg but are not part of the JPEG library
darco 56a656
proper.  Those modules are not really intended to be used in other
darco 56a656
applications.