|
1 | 1 | #include "beer_song.h" |
| 2 | +#include <stdint.h> |
2 | 3 | #include <stdio.h> |
3 | | -#include <string.h> |
4 | 4 |
|
5 | | -static unsigned int get_verse(unsigned int bottles, char *buffer) |
| 5 | +static uint16_t get_verse(uint8_t bottles, char **verse) |
6 | 6 | { |
7 | | - unsigned int bytes_written = 0; |
| 7 | + uint16_t lines_written = 0; |
8 | 8 |
|
9 | 9 | if (bottles == 0) { |
10 | | - bytes_written = |
11 | | - sprintf(buffer, |
12 | | - "No more bottles of beer on the wall, no more bottles of beer.\n" |
13 | | - "Go to the store and buy some more, 99 bottles of beer on the wall.\n"); |
| 10 | + sprintf(*verse, |
| 11 | + "No more bottles of beer on the wall, no more bottles of beer."); |
| 12 | + sprintf(*++verse, |
| 13 | + "Go to the store and buy some more, 99 bottles of beer on the wall."); |
| 14 | + lines_written = 2; |
14 | 15 | } else if (bottles == 1) { |
15 | | - bytes_written = |
16 | | - sprintf(buffer, |
17 | | - "1 bottle of beer on the wall, 1 bottle of beer.\n" |
18 | | - "Take it down and pass it around, no more bottles of beer on the wall.\n"); |
19 | | - } else if (bottles == 2) { |
20 | | - bytes_written = |
21 | | - sprintf(buffer, |
22 | | - "2 bottles of beer on the wall, 2 bottles of beer.\n" |
23 | | - "Take one down and pass it around, 1 bottle of beer on the wall.\n"); |
24 | | - } else if (bottles <= 99) { |
25 | | - bytes_written = |
26 | | - sprintf(buffer, |
27 | | - "%u bottles of beer on the wall, %u bottles of beer.\n" |
28 | | - "Take one down and pass it around, %u bottles of beer on the wall.\n", |
29 | | - bottles, bottles, bottles - 1); |
| 16 | + sprintf(*verse, "%u bottle of beer on the wall, %u bottle of beer.", |
| 17 | + bottles, bottles); |
| 18 | + sprintf(*++verse, |
| 19 | + "Take it down and pass it around, no more bottles of beer on the wall."); |
| 20 | + lines_written = 3; |
| 21 | + } else { |
| 22 | + sprintf(*verse, "%u bottles of beer on the wall, %u bottles of beer.", |
| 23 | + bottles, bottles); |
| 24 | + sprintf(*++verse, |
| 25 | + "Take one down and pass it around, %u bottle%sof beer on the wall.", |
| 26 | + bottles - 1, bottles - 1 == 1 ? " " : "s "); |
| 27 | + lines_written = 3; |
30 | 28 | } |
31 | 29 |
|
32 | | - return bytes_written; |
| 30 | + return lines_written; |
33 | 31 | } |
34 | 32 |
|
35 | | -void recite(unsigned int start_bottles, unsigned int take_down, char *buffer) |
| 33 | +void recite(uint8_t start_bottles, uint8_t take_down, char **song) |
36 | 34 | { |
37 | | - char *current_position = buffer; |
38 | | - |
39 | 35 | while (take_down-- > 0) { |
40 | | - current_position += get_verse(start_bottles--, current_position); |
41 | | - strcpy(current_position, "\n"); |
42 | | - current_position++; |
| 36 | + song += get_verse(start_bottles--, song); |
43 | 37 | } |
44 | | - |
45 | | - strcpy(current_position - 1, "\0"); |
46 | 38 | } |
0 commit comments