visualtest/include/SDL_visualtest_rwhelper.h
author Ryan C. Gordon <icculus@icculus.org>
Thu, 20 Mar 2014 11:22:57 -0400
changeset 8652 8514fc57f124
parent 7924 fcb86d323770
child 11382 2c50e79b19e0
permissions -rw-r--r--
Static analysis fix: dereference of a NULL pointer.
icculus@7924
     1
/* See COPYING.txt for the full license governing this code. */
icculus@7924
     2
/**
icculus@7924
     3
 * \file rwhelper.c
icculus@7924
     4
 *
icculus@7924
     5
 * Header file with some helper functions for working with SDL_RWops.
icculus@7924
     6
 */
icculus@7924
     7
icculus@7924
     8
#include <SDL_rwops.h>
icculus@7924
     9
icculus@7924
    10
#ifndef _SDL_visualtest_rwhelper_h
icculus@7924
    11
#define _SDL_visualtest_rwhelper_h
icculus@7924
    12
icculus@7924
    13
/** Length of the buffer in SDLVisualTest_RWHelperBuffer */
icculus@7924
    14
#define RWOPS_BUFFER_LEN 256
icculus@7924
    15
icculus@7924
    16
/* Set up for C function definitions, even when using C++ */
icculus@7924
    17
#ifdef __cplusplus
icculus@7924
    18
extern "C" {
icculus@7924
    19
#endif
icculus@7924
    20
icculus@7924
    21
/**
icculus@7924
    22
 * Struct that is used as a buffer by the RW helper functions. Should be initialized by calling
icculus@7924
    23
 * SDLVisualTest_RWHelperResetBuffer() before being used.
icculus@7924
    24
 */
icculus@7924
    25
typedef struct SDLVisualTest_RWHelperBuffer
icculus@7924
    26
{
icculus@7924
    27
    /*! Character buffer that data is read into */
icculus@7924
    28
    char buffer[RWOPS_BUFFER_LEN];
icculus@7924
    29
    /*! buffer[buffer_pos] is the next character to be read from the buffer */
icculus@7924
    30
    int buffer_pos;
icculus@7924
    31
    /*! Number of character read into the buffer */
icculus@7924
    32
    int buffer_width;
icculus@7924
    33
} SDLVisualTest_RWHelperBuffer;
icculus@7924
    34
icculus@7924
    35
/**
icculus@7924
    36
 * Resets the buffer pointed to by \c buffer used by some of the helper functions.
icculus@7924
    37
 * This function should be called when you're using one of the helper functions 
icculus@7924
    38
 * with a new SDL_RWops object.
icculus@7924
    39
 */
icculus@7924
    40
void SDLVisualTest_RWHelperResetBuffer(SDLVisualTest_RWHelperBuffer* buffer);
icculus@7924
    41
icculus@7924
    42
/**
icculus@7924
    43
 * Reads a single character using the SDL_RWops object pointed to by \c rw.
icculus@7924
    44
 * This function reads data in blocks and stores them in the buffer pointed to by
icculus@7924
    45
 * \c buffer, so other SDL_RWops functions should not be used in conjunction 
icculus@7924
    46
 * with this function.
icculus@7924
    47
 *
icculus@7924
    48
 * \return The character that was read.
icculus@7924
    49
 */
icculus@7924
    50
char SDLVisualTest_RWHelperReadChar(SDL_RWops* rw,
icculus@7924
    51
                                    SDLVisualTest_RWHelperBuffer* buffer);
icculus@7924
    52
icculus@7924
    53
/**
icculus@7924
    54
 * Reads characters using the SDL_RWops object pointed to by \c rw into the
icculus@7924
    55
 * character array pointed to by \c str (of size \c size) until either the 
icculus@7924
    56
 * array is full or a new line is encountered. If \c comment_char is encountered,
icculus@7924
    57
 * all characters from that position till the end of the line are ignored. The new line
icculus@7924
    58
 * is not included as part of the buffer. Lines with only whitespace and comments
icculus@7924
    59
 * are ignored. This function reads data in blocks and stores them in the buffer
icculus@7924
    60
 * pointed to by \c buffer, so other SDL_RWops functions should not be used in
icculus@7924
    61
 * conjunction with this function.
icculus@7924
    62
 * 
icculus@7924
    63
 * \return pointer to the string on success, NULL on failure or EOF.
icculus@7924
    64
 */
icculus@7924
    65
char* SDLVisualTest_RWHelperReadLine(SDL_RWops* rw, char* str, int size,
icculus@7924
    66
                                     SDLVisualTest_RWHelperBuffer* buffer,
icculus@7924
    67
                                     char comment_char);
icculus@7924
    68
icculus@7924
    69
/**
icculus@7924
    70
 * Counts the number of lines that are not all whitespace and comments using the
icculus@7924
    71
 * SDL_RWops object pointed to by \c rw. \c comment_char indicates the character
icculus@7924
    72
 * used for comments. Uses the buffer pointed to by \c buffer to read data in blocks.
icculus@7924
    73
 *
icculus@7924
    74
 * \return Number of lines on success, -1 on failure.
icculus@7924
    75
 */
icculus@7924
    76
int SDLVisualTest_RWHelperCountNonEmptyLines(SDL_RWops* rw,
icculus@7924
    77
                                             SDLVisualTest_RWHelperBuffer* buffer,
icculus@7924
    78
                                             char comment_char);
icculus@7924
    79
icculus@7924
    80
/* Ends C function definitions when using C++ */
icculus@7924
    81
#ifdef __cplusplus
icculus@7924
    82
}
icculus@7924
    83
#endif
icculus@7924
    84
icculus@7924
    85
#endif /* _SDL_visualtest_rwhelper_h */