More to come...
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

236 line
15KB

  1. /*
  2. Copyright (c) 2023 : Ognjen 'xolatile' Milan Robovic
  3. Xhartae is free software! You will redistribute it or modify it under the terms of the GNU General Public License by Free Software Foundation.
  4. And when you do redistribute it or modify it, it will use either version 3 of the License, or (at yours truly opinion) any later version.
  5. It is distributed in the hope that it will be useful or harmful, it really depends... But no warranty what so ever, seriously. See GNU/GPLv3.
  6. */
  7. #ifndef CHAPTER_0_HEADER
  8. #define CHAPTER_0_HEADER
  9. #include <stdlib.h> // We'll need this header file for malloc, calloc, realloc, free and exit.
  10. #include <fcntl.h> // This one for open and O_ flags.
  11. #include <unistd.h> // And this one for read, write, close and lseek.
  12. /*
  13. > Code formatting
  14. It's very important that your code is aligned, readable and safe. So, we'll talk about it even before the first chapter, the so-called "zero chapter". I write assembly and Ada
  15. beside C, but still, 90% of my projects are written in C programming language! Why? Because C is like a girl that's a bit older than you, quiet but very smart and respectable, and
  16. you don't want to piss her off. Ada is more like nerdy girl who doesn't know what she wants, while assembly is ghost girl in your closet that you pretend not to see at night. So,
  17. this Ada girl was my first serious love, and she had a book "Ada Coding Standard", which is like a bible to her. It's a bit fanatical, but it has some good points which I'll
  18. discuss below without copy/paste, and apply to C.
  19. Lets see some good examples of C code (in my opinion obviously), I'll provide few of them, and note that I use the first style in them. Needless to say, as this book is aimed to
  20. people who don't know C programming language, or are in phase of learning it, you should (and eventually will) develop your own style of writing it, but you shouldn't force it at
  21. the start, it should happen over time, naturally. Just be sure to always be consistent, and to avoid anything that'll generate compiler warnings.
  22. General code formatting should follow these rules in C programming language:
  23. - Always use tabs for indentation, and spaces for alignment. You'll hear people say the otherwise, don't trust them, it's pure cancer.
  24. - Separate most (not all!) operators with space before and after it. We'll provide exceptional cases for this rule below.
  25. - Align most (not all!) operators horizontally, and logical units vertically. Again, examples are the best showcase.
  26. - Always use curly braces and round braces, even when they're not required by the compiler. Most linters warn about this.
  27. - Avoid writing a lot of comments, or avoid them completely, except for license notice, this book is an exception! Also, don't write GPL notice like I do...
  28. - Think very carefully if you'll limit the line length, it depends on your workflow and attention span... For me, 180 characters per line.
  29. - If you want, use pagination, but I don't recommend it when working with C. I use it in Ada, because comments there start with "--".
  30. - Always use snake case (this_style), for enumerations and macros (THIS_STYLE), and never put underscore before or after the identifier.
  31. For now, don't worry about what's an identifier, compiler, linter, those scary and unknown words will be explained later, when the time comes.
  32. Spacing:
  33. - Before and after:
  34. A: B: C: D: E: F:
  35. + += & &= && ==
  36. - -= | |= || !=
  37. * *= ^ ^= ! <=
  38. / /= << <<= ? >=
  39. % %= >> >>= : <
  40. = ~ >
  41. A) Standard operators, addition, subtraction, multiplication, division, modulus and assignment (except assignment obviously).
  42. B) Extension of standard operators, that'll use left-hand variable as first operand.
  43. C) Binary operators, bit-and, bit-or, bit-xor, bit-shift-left, bit-shift-right and bit-not.
  44. D) Again, same extension but for binary operators, that'll use left-hand variable as first operand (except bit-not as it's unary).
  45. E) Logical operators, and, or, not and operators '?' and ':' are used together, as they form ternary operator.
  46. F) Comparison operators, equals, not-equals, less-or-equal, greater-or-equal, less and greater.
  47. Some more symbols are also operators, but my advice is not to treat them as such, just consider them some kind of special cases.
  48. - Before:
  49. ( { [
  50. - After:
  51. ) } ] , ;
  52. - None (unary + and -):
  53. . -> " ' ++ -- + -
  54. Exceptions:
  55. - When using more complicated expressions, apply rules above, or make an exception in them if you think you'll make it more readable by breaking the rules.
  56. - When merging consecutive opened or closed braces, for example, like when you're nesting function calls.
  57. - When declaring functions, align return types and open braces, and by taste, argument types for logical units.
  58. - When declaring variables, align types and asignment operators, and by taste, pointer or array operators.
  59. Naming:
  60. - Have you ever wondered, what the hell is stdio, unistd, stdint, termios, errno, strcat, strchr, strndupa, memcpy, atoi, fgetc, puts, lseek...? I did wonder.
  61. - Are identifiers such as string_concatenate, memory_copy, error_id, integer, boolean, constant, string_to_integer, file_offset too long to type...? I think not.
  62. All that aside, C is an old programming language, and people who have made it knew a lot more about how computers work at their time than most people know nowdays. Hardware got
  63. faster and cheaper, but also more complex. We have enough memory today to edit our text files with word-wrapping enabled, highlight the syntax, and to store large files due to
  64. very long identifiers, 8-space-wide indentation instead of a single tab and overly complicated OOP code. If you can handle those names above, feel free to use them, but be
  65. consistent. I can't handle them, so I tolerate them with C keywords, because I'd actually prefer "constant / integer / character" over "const / int / char"... You'll notice that
  66. C has mixed them in keywords, and most standard library headers mix them too...
  67. - Keywords: register double short signed break while / int char extern auto enum struct
  68. - Headers: string memory limits time complex threads / stdio ctype stdint unistd fcntl termios
  69. - Functions: read write open close exit clock / strncmp memcpy atoi fputc tcsetattr usleep
  70. About that, I don't even know where to begin about talking how bad it is for readability... But lets ignore that for now and focus on code formatting. Text you see above this
  71. paragraph is enough to get you started, and text below is just expanding a little more on common sense when you're formatting your code. For example, how to align consequtive
  72. external or internal function or variable declarations, how to align if statement condition(s) or switch statement body, and more.
  73. There's not much to say here, we'll talk about function declarations more in later chapters, as they are optional, but I like to use them. However, you should avoid to use too
  74. many function agruments, and always place them in consistent and logical order.
  75. @C
  76. // Internal and external function declaration:
  77. static int string_copy_limit (char * string_0, char * string_1, int limit);
  78. extern int32_t string_copy (char * string_a,
  79. char * string_b,
  80. int32_t count);
  81. extern size_t copy_string_n (
  82. string_t a,
  83. string_t b,
  84. size_t n
  85. );
  86. @
  87. Internal ('static') and external ('extern') variable declarations are different from function declarations, because you need to initialize internal variables to some value, I like
  88. to provide either "zero" value for them, with the corresponding type (0, 0.0, '\0', NULL...), or an invalid value (-1, EOF...). Some C compilers and linters will warn you about
  89. redeclaring them, some will warn you that they're already initialized to zero, so when you some across those warnings, you'll know how to fix them. I hate this part of C...
  90. @C
  91. // Internal and external variable declaration:
  92. static int curses_active = 0;
  93. static char curses_signal = '\0';
  94. static char * curses_screen = NULL;
  95. extern unsigned int positive;
  96. extern int negative;
  97. extern int * * coordinate_system;
  98. static void (* encode_type_0 [TYPE_0_COUNT]) (void) = { NULL };
  99. static char * (* encode_type_1 [TYPE_1_COUNT]) (int l) = { NULL };
  100. static int (* encode_type_2 [TYPE_2_COUNT]) (int l, int r) = { NULL };
  101. extern xcb_window_t blesses_window;
  102. extern xcb_gcontext_t blesses_context;
  103. extern xcb_pixmap_t blesses_pixmap;
  104. extern xcb_connection_t * blesses_connection;
  105. extern xcb_screen_t * blesses_screen;
  106. extern xcb_image_t * blesses_image;
  107. @
  108. Only for 'main' function, you shouldn't add 'static' or 'extern' keywords before it. Every C file needs to have exactly one 'main' function, it's the programs' entry point. Also,
  109. some people like to define their own types, with keyword 'typedef', I'm not a fan of it, since in C types are also keywords unlike in Ada. Again, it'll become more clean in future
  110. chapters why I dislike user-defined types, unions and structures.
  111. @C
  112. // Main function:
  113. int main (int argc, char * * argv) {
  114. int32_t main (int32_t argc,
  115. char * argv []) {
  116. number_t main (
  117. number_t argc,
  118. string_t argv []) {
  119. @
  120. Now, I'll write some basic functions that'll be used later in the program, so you can see that code formatting I've spent whole hour writing. Don't mind what they do, just keep an
  121. eye on how are they aligned and named. I'll reimplement some standard functions, and you can revisit them after reading few more chapters, it'll be more understandable then.
  122. */
  123. enum { // This is completely unnecesary, but I don't care, it's a good showcase how boolean type can work, true is 1 and false is 0.
  124. FALSE,
  125. TRUE
  126. };
  127. enum { // This is also one of my preferences, to use CHARACTER_NULL or CHARACTER_LINE_FEED instead of '\0' or '\n' in special (non-string) cases.
  128. CHARACTER_NULL, CHARACTER_START_HEADER, CHARACTER_START_TEXT, CHARACTER_END_TEXT,
  129. CHARACTER_END_TRANSMISSION, CHARACTER_ENQUIRY, CHARACTER_ACKNOWLEDGE, CHARACTER_BELL,
  130. CHARACTER_BACKSPACE, CHARACTER_TAB_HORIZONTAL, CHARACTER_LINE_FEED, CHARACTER_TAB_VERTICAL,
  131. CHARACTER_FORM_FEED, CHARACTER_CARRIAGE_RETURN, CHARACTER_SHIFT_OUT, CHARACTER_SHIFT_IN,
  132. CHARACTER_DATA_LINK_ESCAPE, CHARACTER_DEVICE_CONTROL_1, CHARACTER_DEVICE_CONTROL_2, CHARACTER_DEVICE_CONTROL_3,
  133. CHARACTER_DEVICE_CONTROL_4, CHARACTER_NOT_ACKNOWLEDGE, CHARACTER_SYNCHRONOUS_IDLE, CHARACTER_END_TRANSMISSION_BLOCK,
  134. CHARACTER_CANCEL, CHARACTER_END_MEDIUM, CHARACTER_SUBSTITUTE, CHARACTER_ESCAPE,
  135. CHARACTER_FILE_SEPARATOR, CHARACTER_GROUP_SEPARATOR, CHARACTER_RECORD_SEPARATOR, CHARACTER_UNIT_SEPARATOR,
  136. CHARACTER_COUNT // Not an actual ASCII table count (128), but for starting invisible characters.
  137. };
  138. enum { // I like to align enumerations with 10, 20 or 40 characters per name, and optionally use NAME_ as prefix and NAME_COUNT as last element.
  139. EFFECT_NORMAL, EFFECT_BOLD, EFFECT_ITALIC, EFFECT_UNDERLINE, EFFECT_BLINK, EFFECT_REVERSE,
  140. EFFECT_COUNT
  141. };
  142. enum { // Because of text auto-completition, it's always easy to find what I want, or to use NAME_COUNT in arrays. In some cases, order matters!
  143. COLOUR_GREY, COLOUR_RED, COLOUR_GREEN, COLOUR_YELLOW, COLOUR_BLUE, COLOUR_PINK, COLOUR_CYAN, COLOUR_WHITE,
  144. COLOUR_COUNT
  145. };
  146. extern void in (void * data, int size); // We'll use these functions later as core standard input/output functions.
  147. extern void out (void * data, int size);
  148. extern void echo (char * data); // Function 'echo' behaves similar to 'out', but it's easier to use because it doesn't require size.
  149. extern void fatal_failure (int condition, char * message); // Some functions fail, and sometimes we want to abort everything, crash and leak memory, we just don't care.
  150. extern void limit (int * value, int minimum, int maximum);
  151. extern void * allocate ( int size); // Core memory management functions with some minimal error checking.
  152. extern void * reallocate (void * data, int size);
  153. extern void * deallocate (void * data );
  154. extern int string_length (char * string); // We deal with strings a lot in this program, so string functions will be more important than character functions from chapter one.
  155. extern int string_compare (char * string_0, char * string_1); // See how nicely they align, right?
  156. extern char * string_copy (char * string_0, char * string_1);
  157. extern char * string_concatenate (char * string_0, char * string_1);
  158. extern char * string_reverse (char * string); // Notice last function, we didn't align ');'...
  159. extern int string_compare_limit (char * string_0, char * string_1, int limit); // These ones too, it's beautiful (in my opinion), tho some consider it useless.
  160. extern char * string_copy_limit (char * string_0, char * string_1, int limit);
  161. extern char * string_concatenate_limit (char * string_0, char * string_1, int limit);
  162. extern char * string_reverse_limit (char * string, int limit); // And we align the last argument in this case, use whatever you prefer.
  163. extern char * string_realign (char * string, int amount, char character); // This is a simple function that realigns a string to right, we'll use it way later ahead.
  164. extern int memory_compare (void * memory, void * source, int length); // We'll cover these functions later, they are more complex.
  165. extern void memory_copy (void * memory, void * source, int length);
  166. // In chapter two, we'll explain ASCII escape sequences, for now, consider this to be some black magic.
  167. extern void terminal_clear (void); // Offset and clear terminal screen output.
  168. extern void terminal_colour (int colour, int effect); // Set terminal character attributes.
  169. extern void terminal_cancel (void); // Reset terminal character attributes.
  170. extern void terminal_show_cursor (int show); // Toggle rendering of terminal cursor.
  171. // These will be useful in chapter three, where we'll learn about 'printf' function. It's too complex to cover it at this point.
  172. extern char * number_to_string (int number);
  173. extern char * format_to_string (int number, int sign, int base, int amount, char character);
  174. #endif