aboutsummaryrefslogtreecommitdiffstats
path: root/Projects/Incomplete/StandaloneProgrammer/Lib/ProgrammerConfig.h
blob: ba6e9add6d888764af6717f2b87c4066382fcfc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
             LUFA Library
     Copyright (C) Dean Camera, 2010.
              
  dean [at] fourwalledcubicle [dot] com
      www.fourwalledcubicle.com
*/

/*
  Copyright 2010  Dean Camera (dean [at] fourwalledcubicle [dot] com)

  Permission to use, copy, modify, distribute, and sell this 
  software and its documentation for any purpose is hereby granted
  without fee, provided that the above copyright notice appear in 
  all copies and that both that the copyright notice and this
  permission notice and warranty disclaimer appear in supporting 
  documentation, and that the name of the author not be used in 
  advertising or publicity pertaining to distribution of the 
  software without specific, written prior permission.

  The author disclaim all warranties with regard to this
  software, including all implied warranties of merchantability
  and fitness.  In no event shall the author be liable for any
  special, indirect or consequential damages or any damages
  whatsoever resulting from loss of use, data or profits, whether
  in an action of contract, negligence or other tortious action,
  arising out of or in connection with the use or performance of
  this software.
*/

#ifndef _PROGRAMMER_CONFIG_H_
#define _PROGRAMMER_CONFIG_H_

	/* Includes: */
		#include <stdio.h>
		#include <string.h>
		#include <stdbool.h>
		
		#include "../StandaloneProgrammer.h"

	/* Function Prototypes: */
		bool ProgrammerConfig_ProcessConfiguration(void);

#endif
hlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
/*
 * Copyright (C) 2001 - 2004 Mike Wray <mike.wray@hp.com>
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifndef _XUTIL_IOSTREAM_H_
#define _XUTIL_IOSTREAM_H_

#include <stdarg.h>

#ifdef __KERNEL__
#include <linux/config.h>
#include <linux/types.h>
#include <linux/errno.h>
#else
#include <errno.h>
#include <stdint.h>
#include <stddef.h>
#endif

#include "allocate.h"

/** End of input return value (for getc). */
#define IOSTREAM_EOF -1

/** An input/output abstraction.
 */
typedef struct IOStream IOStream;

/** Record of the functions to use for operations on an
 * IOStream implementation.
 */
typedef struct IOMethods {
    /** Read function.  Called with the user data, buffer to read into
     * and number of bytes to read.  Must return number of bytes read
     * on success, less than zero on error.
     */
    int (*read)(IOStream *stream, void *buf, size_t n);

    /** Write function. Called with user data, buffer to write and
     * number of bytes to write. Must return number of bytes written on
     * success, less than zero otherwise.
     */
    int (*write)(IOStream *stream, const void *buf, size_t n);

    int (*flush)(IOStream *s);

    int (*error)(IOStream *s);

    int (*close)(IOStream *s);

    void (*free)(IOStream *s);

    void (*lock)(IOStream *s);
    void (*unlock)(IOStream *s);

} IOMethods;

/** Abstract i/o object.
 */
struct IOStream {
    /** Methods to use to implement operations. */
    const IOMethods *methods;
    /** Private state for the implementation. */
    const void *data;
    /** Flag indicating whether the stream is closed. */
    int closed;
    /** Number of bytes written. */
    int written;
    /** Number of bytes read. */
    int read;
    /** Flag indicating whether not to free when closed. */
    int nofree;
};


/** IOStream version of stdin. */
extern IOStream *iostdin;

/** IOStream version of stdout, */
extern IOStream *iostdout;

/** IOStream version of stderr. */
extern IOStream *iostderr;

extern int IOStream_print(IOStream *io, const char *format, ...);
extern int IOStream_vprint(IOStream *io, const char *format, va_list args);

/** Read from a stream.
 *
 * @param stream input
 * @param buf where to put input
 * @param n number of bytes to read
 * @return if ok, number of bytes read, otherwise negative error code
 */
static inline int IOStream_read(IOStream *stream, void *buf, size_t n){
    int result;
    if(stream->closed){
        result = -EIO;
        goto exit;
    }
    if(!stream->methods || !stream->methods->read){
        result = -EINVAL;
        goto exit;
    }
    result = (stream->methods->read)(stream, buf, n);
    if(result > 0){
        stream->read += result;
    }
  exit:
    return result;
}

/** Write to a stream.
 *
 * @param stream input
 * @param buf where to put input
 * @param n number of bytes to write
 * @return if ok, number of bytes written, otherwise negative error code
 */
static inline int IOStream_write(IOStream *stream, const void *buf, size_t n){
    int result;
    if(stream->closed){
        result = -EIO;
        goto exit;
    }
    if(!stream->methods || !stream->methods->write){
        result = -EINVAL;
        goto exit;
    }
    result = (stream->methods->write)(stream, buf, n);
    if(result > 0){
        stream->written += result;
    }
  exit:
    return result;
}

/** Flush the stream.
 *
 * @param stream stream
 * @return 0 on success, negative error code otherwise
 */
static inline int IOStream_flush(IOStream *stream){
    int result = 0;
    if(stream->closed){
        result = -EIO;
    } else if(stream->methods->flush){
        result = (stream->methods->flush)(stream);
    }
    return result;
}

/** Check whether the stream has an error.
 *
 * @param stream to check
 * @return 1 for error, 0 otherwise
 */
static inline int IOStream_error(IOStream *stream){
    int err = 0;
    if(stream->methods && stream->methods->error){
       err = (stream->methods->error)(stream);
    }
    return err;
}

/** Close the stream.
 *
 * @param stream to close
 * @return 0 on success, negative error code otherwise
 */
static inline int IOStream_close(IOStream *stream){
    int err = 0;
    if(!stream || stream->closed){
        err = -EIO;
        goto exit;
    }
    if(stream->methods && stream->methods->close){
        err = (stream->methods->close)(stream);
        stream->closed = 1;
    }
    if(stream->nofree) goto exit;
    if(stream->methods && stream->methods->free){
        (stream->methods->free)(stream);
    }
    *stream = (IOStream){};
    deallocate(stream);
  exit:
    return err;
}

/** Test if the stream has been closed.
 *
 * @param stream to check
 * @return 1 if closed, 0 otherwise
 */
static inline int IOStream_is_closed(IOStream *stream){
    return stream->closed;
}

/** Print a character to a stream, like fputc().
 *
 * @param stream to print to
 * @param c character to print
 * @return result code from the print
 */
static inline int IOStream_putc(IOStream *stream, int c){
    int err;
    unsigned char b = (unsigned char)c;
    err = IOStream_write(stream, &b, 1);
    if(err < 1){
        err = IOSTREAM_EOF;
    } else {
        err = b;
    }
    return err;
}

/** Read from a stream, like fgetc().
 *
 * @param stream to read from
 * @return IOSTREAM_EOF on error, character read otherwise
 */
static inline int IOStream_getc(IOStream *stream){
    int err, rc;
    unsigned char b;

    err = IOStream_read(stream, &b, 1);
    if(err < 1){
        rc = IOSTREAM_EOF;
    } else {
        rc = b;
    }
    return rc;
}

/** Get number of bytes read.
 *
 * @param stream to get from
 * @return number of bytes read
 */
static inline int IOStream_get_read(IOStream *stream){
    return stream->read;
}

/** Get number of bytes written.
 *
 * @param stream to get from
 * @return number of bytes written
 */
static inline int IOStream_get_written(IOStream *stream){
    return stream->written;
}


#endif /* ! _XUTIL_IOSTREAM_H_ */