coderules.txt 5.2 KB

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