fix red.
[kugel-rb/myfork.git] / apps / plugins / goban / util.h
blob83dc880ac7bb87e8f16ea3bcd29fec6331d82891
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Copyright (C) 2007-2009 Joshua Simmons <mud at majidejima dot com>
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation; either version 2
15 * of the License, or (at your option) any later version.
17 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
18 * KIND, either express or implied.
20 ****************************************************************************/
22 #ifndef GOBAN_UTIL_H
23 #define GOBAN_UTIL_H
25 #include "types.h"
26 #include "goban.h"
28 /* Call before using a stack, returns false on setup failure */
29 bool setup_stack (struct stack_t *stack, void *buffer, size_t buffer_size);
31 /* Push, pop or peek from the stack. Returns false on failure (usually
32 stack full or empty, depending on the function) */
33 bool push_stack (struct stack_t *stack, void *buffer, size_t buffer_size);
34 bool pop_stack (struct stack_t *stack, void *buffer, size_t buffer_size);
35 bool peek_stack (struct stack_t *stack, void *buffer, size_t buffer_size);
37 /* Clear all of the data from the stack and move the stack pointer to the
38 beginning */
39 void empty_stack (struct stack_t *stack);
41 /* Convenience functions for pushing/poping/peeking standard value types
42 to a stack */
43 #define pop_pos_stack(stack, pos) pop_stack(stack, pos, sizeof (unsigned short))
44 #define peek_pos_stack(stack, pos) peek_stack(stack, pos, sizeof (unsigned short))
46 #define pop_int_stack(stack, num) pop_stack(stack, num, sizeof (int))
47 #define peek_int_stack(stack, num) peek_stack(stack, num, sizeof (int))
49 #define pop_char_stack(stack, num) pop_stack(stack, num, sizeof (char))
50 #define peek_char_stack(stack, num) peek_stack(stack, num, sizeof (char))
52 bool push_pos_stack (struct stack_t *stack, unsigned short pos);
53 bool push_int_stack (struct stack_t *stack, int num);
54 bool push_char_stack (struct stack_t *stack, char num);
57 #define min(x, y) (x < y ? x : y)
58 #define max(x, y) (x > y ? x : y)
60 /* Returns the fd of the file */
61 int create_or_open_file (const char *filename);
63 /* Returns the number of characters printed */
64 int snprint_fixed (char *buffer, int buffer_size, int fixed);
66 /* These should all be obvious, they are simply wrappers on the normal
67 rockbox file functions which will loop several times if the rockbox
68 file functions don't deal with all of the data at once */
69 ssize_t read_file (int fd, void *buf, size_t count);
70 ssize_t write_file (int fd, const void *buf, size_t count);
71 void close_file (int *fd);
73 int peek_char (int fd);
74 int read_char (int fd);
75 bool write_char (int fd, char to_write);
77 /* Seek to the next non-whitespace character (doesn't go anywhere if the
78 current character is already non-whitespace), and then peek it -1 on
79 EOF or error */
80 int peek_char_no_whitespace (int fd);
81 /* Same deal, with reading -1 on EOF or error */
82 int read_char_no_whitespace (int fd);
84 /* Returns true if a character is whitespace. Should /NOT/ be called with
85 anything but the return value of one of the peeking/reading functions or
86 a standard character. */
87 bool is_whitespace (int value);
89 /* Gets rid of ']' characdters from the string by overwritting them. This
90 is needed in header strings because they would otherwise corrupt the
91 SGF file when outputted */
92 void sanitize_string (char *string);
94 /* Return an aligned version of the bufer, with the size updated Returns
95 NULL if the buffer is too small to align. */
96 void *align_buffer (void *buffer, size_t * buffer_size);
98 /* Get the string and buffer size for a SGF property which is stored in
99 the header. Returns false on failure, in which case any information set
100 in **buffer and *size are not to be trusted or used. */
101 bool get_header_string_and_size (struct header_t *header,
102 enum prop_type_t type,
103 char **buffer, int *size);
105 /* Output a summary of the game metadata (Game Info)
107 void metadata_summary (void);
109 #ifdef GBN_TEST
110 void run_tests (void);
111 #endif
113 #endif