xref: /plan9/sys/src/cmd/gs/jpeg/coderules.doc (revision 7dd7cddf99dd7472612f1413b4da293630e6b1bc)
1*7dd7cddfSDavid du ColombierIJG JPEG LIBRARY:  CODING RULES
2*7dd7cddfSDavid du Colombier
3*7dd7cddfSDavid du ColombierCopyright (C) 1991-1996, Thomas G. Lane.
4*7dd7cddfSDavid du ColombierThis file is part of the Independent JPEG Group's software.
5*7dd7cddfSDavid du ColombierFor conditions of distribution and use, see the accompanying README file.
6*7dd7cddfSDavid du Colombier
7*7dd7cddfSDavid du Colombier
8*7dd7cddfSDavid du ColombierSince numerous people will be contributing code and bug fixes, it's important
9*7dd7cddfSDavid du Colombierto establish a common coding style.  The goal of using similar coding styles
10*7dd7cddfSDavid du Colombieris much more important than the details of just what that style is.
11*7dd7cddfSDavid du Colombier
12*7dd7cddfSDavid du ColombierIn general we follow the recommendations of "Recommended C Style and Coding
13*7dd7cddfSDavid du ColombierStandards" revision 6.1 (Cannon et al. as modified by Spencer, Keppel and
14*7dd7cddfSDavid du ColombierBrader).  This document is available in the IJG FTP archive (see
15*7dd7cddfSDavid du Colombierjpeg/doc/cstyle.ms.tbl.Z, or cstyle.txt.Z for those without nroff/tbl).
16*7dd7cddfSDavid du Colombier
17*7dd7cddfSDavid du ColombierBlock comments should be laid out thusly:
18*7dd7cddfSDavid du Colombier
19*7dd7cddfSDavid du Colombier/*
20*7dd7cddfSDavid du Colombier *  Block comments in this style.
21*7dd7cddfSDavid du Colombier */
22*7dd7cddfSDavid du Colombier
23*7dd7cddfSDavid du ColombierWe indent statements in K&R style, e.g.,
24*7dd7cddfSDavid du Colombier	if (test) {
25*7dd7cddfSDavid du Colombier	  then-part;
26*7dd7cddfSDavid du Colombier	} else {
27*7dd7cddfSDavid du Colombier	  else-part;
28*7dd7cddfSDavid du Colombier	}
29*7dd7cddfSDavid du Colombierwith two spaces per indentation level.  (This indentation convention is
30*7dd7cddfSDavid du Colombierhandled automatically by GNU Emacs and many other text editors.)
31*7dd7cddfSDavid du Colombier
32*7dd7cddfSDavid du ColombierMulti-word names should be written in lower case with underscores, e.g.,
33*7dd7cddfSDavid du Colombiermulti_word_name (not multiWordName).  Preprocessor symbols and enum constants
34*7dd7cddfSDavid du Colombierare similar but upper case (MULTI_WORD_NAME).  Names should be unique within
35*7dd7cddfSDavid du Colombierthe first fifteen characters.  (On some older systems, global names must be
36*7dd7cddfSDavid du Colombierunique within six characters.  We accommodate this without cluttering the
37*7dd7cddfSDavid du Colombiersource code by using macros to substitute shorter names.)
38*7dd7cddfSDavid du Colombier
39*7dd7cddfSDavid du ColombierWe use function prototypes everywhere; we rely on automatic source code
40*7dd7cddfSDavid du Colombiertransformation to feed prototype-less C compilers.  Transformation is done
41*7dd7cddfSDavid du Colombierby the simple and portable tool 'ansi2knr.c' (courtesy of Ghostscript).
42*7dd7cddfSDavid du Colombieransi2knr is not very bright, so it imposes a format requirement on function
43*7dd7cddfSDavid du Colombierdeclarations: the function name MUST BEGIN IN COLUMN 1.  Thus all functions
44*7dd7cddfSDavid du Colombiershould be written in the following style:
45*7dd7cddfSDavid du Colombier
46*7dd7cddfSDavid du ColombierLOCAL(int *)
47*7dd7cddfSDavid du Colombierfunction_name (int a, char *b)
48*7dd7cddfSDavid du Colombier{
49*7dd7cddfSDavid du Colombier    code...
50*7dd7cddfSDavid du Colombier}
51*7dd7cddfSDavid du Colombier
52*7dd7cddfSDavid du ColombierNote that each function definition must begin with GLOBAL(type), LOCAL(type),
53*7dd7cddfSDavid du Colombieror METHODDEF(type).  These macros expand to "static type" or just "type" as
54*7dd7cddfSDavid du Colombierappropriate.  They provide a readable indication of the routine's usage and
55*7dd7cddfSDavid du Colombiercan readily be changed for special needs.  (For instance, special linkage
56*7dd7cddfSDavid du Colombierkeywords can be inserted for use in Windows DLLs.)
57*7dd7cddfSDavid du Colombier
58*7dd7cddfSDavid du Colombieransi2knr does not transform method declarations (function pointers in
59*7dd7cddfSDavid du Colombierstructs).  We handle these with a macro JMETHOD, defined as
60*7dd7cddfSDavid du Colombier	#ifdef HAVE_PROTOTYPES
61*7dd7cddfSDavid du Colombier	#define JMETHOD(type,methodname,arglist)  type (*methodname) arglist
62*7dd7cddfSDavid du Colombier	#else
63*7dd7cddfSDavid du Colombier	#define JMETHOD(type,methodname,arglist)  type (*methodname) ()
64*7dd7cddfSDavid du Colombier	#endif
65*7dd7cddfSDavid du Colombierwhich is used like this:
66*7dd7cddfSDavid du Colombier	struct function_pointers {
67*7dd7cddfSDavid du Colombier	  JMETHOD(void, init_entropy_encoder, (int somearg, jparms *jp));
68*7dd7cddfSDavid du Colombier	  JMETHOD(void, term_entropy_encoder, (void));
69*7dd7cddfSDavid du Colombier	};
70*7dd7cddfSDavid du ColombierNote the set of parentheses surrounding the parameter list.
71*7dd7cddfSDavid du Colombier
72*7dd7cddfSDavid du ColombierA similar solution is used for forward and external function declarations
73*7dd7cddfSDavid du Colombier(see the EXTERN and JPP macros).
74*7dd7cddfSDavid du Colombier
75*7dd7cddfSDavid du ColombierIf the code is to work on non-ANSI compilers, we cannot rely on a prototype
76*7dd7cddfSDavid du Colombierdeclaration to coerce actual parameters into the right types.  Therefore, use
77*7dd7cddfSDavid du Colombierexplicit casts on actual parameters whenever the actual parameter type is not
78*7dd7cddfSDavid du Colombieridentical to the formal parameter.  Beware of implicit conversions to "int".
79*7dd7cddfSDavid du Colombier
80*7dd7cddfSDavid du ColombierIt seems there are some non-ANSI compilers in which the sizeof() operator
81*7dd7cddfSDavid du Colombieris defined to return int, yet size_t is defined as long.  Needless to say,
82*7dd7cddfSDavid du Colombierthis is brain-damaged.  Always use the SIZEOF() macro in place of sizeof(),
83*7dd7cddfSDavid du Colombierso that the result is guaranteed to be of type size_t.
84*7dd7cddfSDavid du Colombier
85*7dd7cddfSDavid du Colombier
86*7dd7cddfSDavid du ColombierThe JPEG library is intended to be used within larger programs.  Furthermore,
87*7dd7cddfSDavid du Colombierwe want it to be reentrant so that it can be used by applications that process
88*7dd7cddfSDavid du Colombiermultiple images concurrently.  The following rules support these requirements:
89*7dd7cddfSDavid du Colombier
90*7dd7cddfSDavid du Colombier1. Avoid direct use of file I/O, "malloc", error report printouts, etc;
91*7dd7cddfSDavid du Colombierpass these through the common routines provided.
92*7dd7cddfSDavid du Colombier
93*7dd7cddfSDavid du Colombier2. Minimize global namespace pollution.  Functions should be declared static
94*7dd7cddfSDavid du Colombierwherever possible.  (Note that our method-based calling conventions help this
95*7dd7cddfSDavid du Colombiera lot: in many modules only the initialization function will ever need to be
96*7dd7cddfSDavid du Colombiercalled directly, so only that function need be externally visible.)  All
97*7dd7cddfSDavid du Colombierglobal function names should begin with "jpeg_", and should have an
98*7dd7cddfSDavid du Colombierabbreviated name (unique in the first six characters) substituted by macro
99*7dd7cddfSDavid du Colombierwhen NEED_SHORT_EXTERNAL_NAMES is set.
100*7dd7cddfSDavid du Colombier
101*7dd7cddfSDavid du Colombier3. Don't use global variables; anything that must be used in another module
102*7dd7cddfSDavid du Colombiershould be in the common data structures.
103*7dd7cddfSDavid du Colombier
104*7dd7cddfSDavid du Colombier4. Don't use static variables except for read-only constant tables.  Variables
105*7dd7cddfSDavid du Colombierthat should be private to a module can be placed into private structures (see
106*7dd7cddfSDavid du Colombierthe system architecture document, structure.doc).
107*7dd7cddfSDavid du Colombier
108*7dd7cddfSDavid du Colombier5. Source file names should begin with "j" for files that are part of the
109*7dd7cddfSDavid du Colombierlibrary proper; source files that are not part of the library, such as cjpeg.c
110*7dd7cddfSDavid du Colombierand djpeg.c, do not begin with "j".  Keep source file names to eight
111*7dd7cddfSDavid du Colombiercharacters (plus ".c" or ".h", etc) to make life easy for MS-DOSers.  Keep
112*7dd7cddfSDavid du Colombiercompression and decompression code in separate source files --- some
113*7dd7cddfSDavid du Colombierapplications may want only one half of the library.
114*7dd7cddfSDavid du Colombier
115*7dd7cddfSDavid du ColombierNote: these rules (particularly #4) are not followed religiously in the
116*7dd7cddfSDavid du Colombiermodules that are used in cjpeg/djpeg but are not part of the JPEG library
117*7dd7cddfSDavid du Colombierproper.  Those modules are not really intended to be used in other
118*7dd7cddfSDavid du Colombierapplications.
119