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