2
0

CODING_GUIDELINES 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /*
  2. * Copyright (c) 2007-2017, FreeSWITCH Solutions LLC
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. *
  12. * * Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. *
  16. * * Neither the name of the original author; nor the names of any contributors
  17. * may be used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  25. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  26. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  27. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  28. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  29. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  30. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  31. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. /*
  34. Functions that create and destroy objects always take pointer to pointer of that type.
  35. Spaces after loop and conditional operators while, for, if etc.
  36. Newlines after one or more declarations.
  37. Newlines before and after loops.
  38. Newlines before returns.
  39. One-Line if/else sparingly only when very obvious exit from loop or return.
  40. Use brackets even when not necessary other than the one-liners above.
  41. Align the * in pointers to the type or function name (e.g char *foo, char *some_func(char *data) )
  42. Use typedefs for structs especially public-facing.
  43. Only use // style-comments on tempory comments that will probably be removed eventually.
  44. Add the emacs/vi comment to the bottom of every file.
  45. Use Doxygen for function args.
  46. Tabs not spaces.
  47. Use flags as bitwise when possible, use arrays if going beyond 32
  48. Typedef all enums using UPPER_CASE notation for the values
  49. */
  50. typedef enum {
  51. SOME_FLAG_X = (1 << 0),
  52. SOME_FLAG_Y = (1 << 1)
  53. } some_flag_type_t;
  54. typedef enum {
  55. SOME_TYPE_X = 1,
  56. SOME_TYPE_Y,
  57. SOME_TYPE_Z
  58. } some_type_t;
  59. KS_DECLARE(ks_status_t) function_example(somedata_t **data, ks_pool_t *pool)
  60. {
  61. int var = 3, x = 0;
  62. if (!pool) return KS_STATUS_FAIL;
  63. for (x = 0; x < 100; x++) {
  64. var += x;
  65. }
  66. if (var > 20) {
  67. var *= 2;
  68. } else {
  69. var *= 3;
  70. }
  71. while (var) {
  72. var--;
  73. }
  74. if (var) {
  75. *data = ks_pool_alloc(pool, sizeof(*data));
  76. (*data)->pool = pool;
  77. return KS_STATUS_SUCCESS;
  78. } else {
  79. *data = NULL;
  80. return KS_STATUS_FAIL;
  81. }
  82. }
  83. /* For Emacs:
  84. * Local Variables:
  85. * mode:c
  86. * indent-tabs-mode:t
  87. * tab-width:4
  88. * c-basic-offset:4
  89. * End:
  90. * For VIM:
  91. * vim:set softtabstop=4 shiftwidth=4 tabstop=4 noet:
  92. */