Home | History | Annotate | Download | only in lib
      1 /*
      2  * Copyright (C) 2008-2009 SVOX AG, Baslerstr. 30, 8048 Zuerich, Switzerland
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *     http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 /**
     17  * @file picopal.h
     18  *
     19  * pico plattform abstraction layer
     20  *
     21  * Copyright (C) 2008-2009 SVOX AG, Baslerstr. 30, 8048 Zuerich, Switzerland
     22  * All rights reserved.
     23  *
     24  * History:
     25  * - 2009-04-20 -- initial version
     26  *
     27  */
     28 /**
     29  * @addtogroup picoos
     30 
     31  * <b> Operating system Platform Specific implementation module </b>\n
     32  *
     33 */
     34 #ifndef PICOPAL_H_
     35 #define PICOPAL_H_
     36 
     37 #include <stdio.h>
     38 #include <stdlib.h>
     39 #include <stdarg.h>
     40 #include <math.h>
     41 #include <stddef.h>
     42 #include "picopltf.h"
     43 #include "picodefs.h"
     44 
     45 #ifdef __cplusplus
     46 extern "C" {
     47 #endif
     48 #if 0
     49 }
     50 #endif
     51 
     52 
     53 /* *********************************************************/
     54 /* general defines and typedefs (used to be in picodefs.h) */
     55 /* *********************************************************/
     56 
     57 #define TRUE 1
     58 #define FALSE 0
     59 
     60 #ifndef NULL
     61 #define NULL 0
     62 #endif
     63 
     64 #define NULLC '\000'
     65 
     66 
     67 /* "strange" defines to switch variants... */
     68 #define PICOPAL_DIV_USE_INV 0
     69 
     70 
     71 /*---------------Externals-----------------------*/
     72 /* used by picocep*/
     73 #if defined(PICO_DEBUG)
     74     extern int numlongmult, numshortmult;
     75 #endif
     76 
     77 
     78 typedef signed int pico_status_t;
     79 
     80 
     81 /* unfortunately, ANSI-C uses eof for results and exceptional results .. */
     82 /* in the context of reading from a CharBuffer, eof means "no more
     83    input available FOR NOW" */
     84 
     85 #define PICO_EOF                        (pico_status_t)    -1
     86 
     87 
     88 /* *************************************************/
     89 /* constants                                       */
     90 /* *************************************************/
     91 
     92 
     93   /* operating system identifications */
     94 #define PICOPAL_OS_NIL        0  /* just an unchangeable first value */
     95 #define PICOPAL_OS_WINDOWS    1
     96 /* ... */
     97 #define PICOPAL_OS_GENERIC   99 /* must always be the last value */
     98 
     99 /* *************************************************/
    100 /* types                                           */
    101 /* *************************************************/
    102 
    103 typedef unsigned char   picopal_uint8;
    104 typedef unsigned short  picopal_uint16;
    105 typedef unsigned int    picopal_uint32;
    106 
    107 typedef signed char     picopal_int8;
    108 typedef signed short    picopal_int16;
    109 typedef signed int      picopal_int32;
    110 
    111 typedef float           picopal_single;
    112 typedef double          picopal_double;
    113 
    114 typedef unsigned char     picopal_char;
    115 
    116 typedef unsigned char   picopal_uchar;
    117 
    118 typedef size_t    picopal_objsize_t;
    119 typedef ptrdiff_t picopal_ptrdiff_t;
    120 
    121 /* *************************************************/
    122 /* functions                                       */
    123 /* *************************************************/
    124 
    125 picopal_int32 picopal_atoi(const picopal_char *);
    126 
    127 picopal_int32 picopal_strcmp(const picopal_char *, const picopal_char *);
    128 picopal_int32 picopal_strncmp(const picopal_char *a, const picopal_char *b, picopal_objsize_t siz);
    129 picopal_objsize_t picopal_strlen(const picopal_char *);
    130 picopal_char * picopal_strchr(const picopal_char *, picopal_char);
    131 picopal_char * picopal_strcpy(picopal_char *d, const picopal_char *s);
    132 picopal_char *picopal_strstr(const picopal_char *s, const picopal_char *substr);
    133 picopal_char *picopal_strcat(picopal_char *dest, const picopal_char *src);
    134 picopal_int16 picopal_sprintf(picopal_char * dst, const picopal_char *fmt, ...);
    135 
    136 /* copies 'length' bytes from 'src' to 'dest'. (regions may be overlapping) no error checks! */
    137 void * picopal_mem_copy(const void * src, void * dst,  picopal_objsize_t length);
    138 
    139 /* sets 'length' bytes starting at dest[0] to 'byte_val' */
    140 void * picopal_mem_set(void * dest, picopal_uint8 byte_val, picopal_objsize_t length);
    141 
    142 /* safe versions */
    143 picopal_objsize_t picopal_vslprintf(picopal_char * dst, picopal_objsize_t siz, const picopal_char *fmt, va_list args);
    144 picopal_objsize_t picopal_slprintf(picopal_char * dst, picopal_objsize_t siz, const picopal_char *fmt, /*args*/ ...);
    145 picopal_objsize_t picopal_strlcpy(picopal_char *dst, const picopal_char *src, picopal_objsize_t siz);
    146 
    147 /*Fixed point computation*/
    148 /*
    149 picopal_int32 picopal_fixptdiv(picopal_int32 a, picopal_int32 b, picopal_uint8 bigpow);
    150 picopal_int32 picopal_fixptmult(picopal_int32 x, picopal_int32 y, picopal_uint8 bigpow);
    151 picopal_int32 picopal_fixptdivORinv(picopal_int32 a, picopal_int32 b, picopal_int32 invb, picopal_uint8 bigpow);
    152 picopal_int32 picopal_fixptmultdouble(picopal_int32 x, picopal_int32 y, picopal_uint8 bigpow);
    153 picopal_uint8 picopal_highestBit(picopal_int32 x);
    154 */
    155 
    156 /* *************************************************/
    157 /* math                                            */
    158 /* *************************************************/
    159 
    160 picopal_double picopal_cos (const picopal_double cos_arg);
    161 picopal_double picopal_sin (const picopal_double sin_arg);
    162 picopal_double picopal_fabs (const picopal_double fabs_arg);
    163 
    164 
    165 
    166 
    167 /* *************************************************/
    168 /* file access                                     */
    169 /* *************************************************/
    170 
    171 extern picopal_char picopal_eol(void);
    172 
    173 #define picopal_FILE      FILE
    174 
    175 
    176 /* seek modes to be used with the 'FSeek' procedure */
    177 #define PICOPAL_SEEK_SET     0   /* absolut seek position */
    178 #define PICOPAL_SEEK_CUR     1   /* relative to current */
    179 #define PICOPAL_SEEK_END     2   /* relative to the end of the file */
    180 
    181 
    182 typedef enum {PICOPAL_BINARY_READ, PICOPAL_BINARY_WRITE, PICOPAL_TEXT_READ, PICOPAL_TEXT_WRITE}  picopal_access_mode;
    183 
    184 typedef picopal_FILE * picopal_File;
    185 
    186 extern picopal_File picopal_fopen (picopal_char fileName[], picopal_access_mode mode);
    187 /* 'FOpen' opens the file with name 'filename'. Depending on
    188    'mode' :
    189       'TextRead'    : Opens an existing text file for reading.
    190                       The file is positioned at the beginning of the file.
    191       'TextWrite'   : Opens and truncates an existing file or creates a new
    192                       text file for writing. The file is positioned at the
    193                       beginning.
    194       'BinaryRead'  : Opens an existing binary file for reading.
    195                       The file is positioned at the beginning of the file.
    196       'BinaryWrite' : Opens and truncates an existing file or creates a new
    197                       binary file for writing. The file is positioned at the
    198                       beginning.
    199     If the opening of the file is successful a file pointer is given
    200     back. Otherwise a NIL-File is given back.
    201 */
    202 
    203 
    204 extern picopal_File picopal_get_fnil (void);
    205 
    206 
    207 extern  picopal_int8 picopal_is_fnil (picopal_File f);
    208 
    209 
    210 extern pico_status_t picopal_fclose (picopal_File f);
    211 
    212 
    213 extern picopal_uint32 picopal_flength (picopal_File f);
    214 
    215 
    216 extern  picopal_uint8 picopal_feof (picopal_File f);
    217 
    218 
    219 extern pico_status_t picopal_fseek (picopal_File f, picopal_uint32 offset, picopal_int8 seekmode);
    220 
    221 
    222 extern pico_status_t picopal_fget_char (picopal_File f, picopal_char * ch);
    223 
    224 
    225 extern picopal_objsize_t picopal_fread_bytes (picopal_File f, void * ptr, picopal_objsize_t objsize, picopal_uint32 nobj);
    226 
    227 extern picopal_objsize_t picopal_fwrite_bytes (picopal_File f, void * ptr, picopal_objsize_t objsize, picopal_uint32 nobj);
    228 
    229 
    230 extern pico_status_t picopal_fflush (picopal_File f);
    231 
    232 /*
    233 extern pico_status_t picopal_fput_char (picopal_File f, picopal_char ch);
    234 */
    235 
    236 
    237 /*
    238 extern pico_status_t picopal_remove (picopal_char filename[]);
    239 
    240 
    241 extern pico_status_t picopal_rename (picopal_char oldname[], picopal_char newname[]);
    242 
    243 */
    244 
    245 /* *************************************************/
    246 /* functions for debugging/testing purposes only   */
    247 /* *************************************************/
    248 
    249 /**
    250  * Returns a pointer to a newly allocated chunk of 'size' bytes, aligned
    251  * to the system page size.
    252  * Memory allocated by this routine may be protected by calling function
    253  * picopal_mrp_protect().
    254  */
    255 void *picopal_mpr_alloc(picopal_objsize_t size);
    256 
    257 /**
    258  * Releases the chunk of memory pointed to by '*p'. 'p' must be previously
    259  * allocated by a call to picopal_mpr_alloc().
    260  */
    261 void picopal_mpr_free(void **p);
    262 
    263 #define PICOPAL_PROT_NONE   0   /* the memory cannot be accessed at all */
    264 #define PICOPAL_PROT_READ   1   /* the memory can be read */
    265 #define PICOPAL_PROT_WRITE  2   /* the memory can be written to */
    266 
    267 /**
    268  * Specifies the desired protection 'prot' for the memory page(s) containing
    269  * part or all of the interval [addr, addr+len-1]. If an access is disallowed
    270  * by the protection given it, the program receives a SIGSEGV.
    271  */
    272 pico_status_t picopal_mpr_protect(void *addr, picopal_objsize_t len, picopal_int16 prot);
    273 
    274 /* Fast, Compact Approximation of the Exponential Function */
    275 picopal_double picopal_quick_exp(const picopal_double y);
    276 
    277 /* *************************************************/
    278 /* types functions for time measurement            */
    279 /* *************************************************/
    280 
    281 extern void picopal_get_timer(picopal_uint32 * sec, picopal_uint32 * usec);
    282 
    283 #ifdef __cplusplus
    284 }
    285 #endif
    286 
    287 
    288 #endif /*PICOPAL_H_*/
    289