Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
src/vm/strobj.h@0:14e5e829dffe, 2010-07-21 (annotated)
- Committer:
- dadaista
- Date:
- Wed Jul 21 12:50:41 2010 +0000
- Revision:
- 0:14e5e829dffe
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dadaista | 0:14e5e829dffe | 1 | /* |
dadaista | 0:14e5e829dffe | 2 | # This file is Copyright 2003, 2006, 2007, 2009 Dean Hall. |
dadaista | 0:14e5e829dffe | 3 | # |
dadaista | 0:14e5e829dffe | 4 | # This file is part of the PyMite VM. |
dadaista | 0:14e5e829dffe | 5 | # The PyMite VM is free software: you can redistribute it and/or modify |
dadaista | 0:14e5e829dffe | 6 | # it under the terms of the GNU GENERAL PUBLIC LICENSE Version 2. |
dadaista | 0:14e5e829dffe | 7 | # |
dadaista | 0:14e5e829dffe | 8 | # The PyMite VM is distributed in the hope that it will be useful, |
dadaista | 0:14e5e829dffe | 9 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
dadaista | 0:14e5e829dffe | 10 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
dadaista | 0:14e5e829dffe | 11 | # A copy of the GNU GENERAL PUBLIC LICENSE Version 2 |
dadaista | 0:14e5e829dffe | 12 | # is seen in the file COPYING in this directory. |
dadaista | 0:14e5e829dffe | 13 | */ |
dadaista | 0:14e5e829dffe | 14 | |
dadaista | 0:14e5e829dffe | 15 | |
dadaista | 0:14e5e829dffe | 16 | #ifndef __STRING_H__ |
dadaista | 0:14e5e829dffe | 17 | #define __STRING_H__ |
dadaista | 0:14e5e829dffe | 18 | |
dadaista | 0:14e5e829dffe | 19 | |
dadaista | 0:14e5e829dffe | 20 | /** |
dadaista | 0:14e5e829dffe | 21 | * \file |
dadaista | 0:14e5e829dffe | 22 | * \brief String Object Type |
dadaista | 0:14e5e829dffe | 23 | * |
dadaista | 0:14e5e829dffe | 24 | * String object type header. |
dadaista | 0:14e5e829dffe | 25 | */ |
dadaista | 0:14e5e829dffe | 26 | |
dadaista | 0:14e5e829dffe | 27 | |
dadaista | 0:14e5e829dffe | 28 | /** Set to nonzero to enable string cache. DO NOT REMOVE THE DEFINITION. */ |
dadaista | 0:14e5e829dffe | 29 | #define USE_STRING_CACHE 1 |
dadaista | 0:14e5e829dffe | 30 | |
dadaista | 0:14e5e829dffe | 31 | |
dadaista | 0:14e5e829dffe | 32 | /** |
dadaista | 0:14e5e829dffe | 33 | * Loads a string from image |
dadaista | 0:14e5e829dffe | 34 | * |
dadaista | 0:14e5e829dffe | 35 | * @param ms memoryspace paddr points to |
dadaista | 0:14e5e829dffe | 36 | * @param paddr address in memoryspace of source string |
dadaista | 0:14e5e829dffe | 37 | * @param r_pstring Return by reference; a new string object |
dadaista | 0:14e5e829dffe | 38 | * @return Return status |
dadaista | 0:14e5e829dffe | 39 | */ |
dadaista | 0:14e5e829dffe | 40 | #define string_loadFromImg(ms, paddr, r_pstring) \ |
dadaista | 0:14e5e829dffe | 41 | string_create((ms), (paddr), (int16_t)-1, (int16_t)1, (r_pstring)) |
dadaista | 0:14e5e829dffe | 42 | |
dadaista | 0:14e5e829dffe | 43 | /** |
dadaista | 0:14e5e829dffe | 44 | * Creates String object from character array in RAM |
dadaista | 0:14e5e829dffe | 45 | * |
dadaista | 0:14e5e829dffe | 46 | * @param paddr pointer to address of source string |
dadaista | 0:14e5e829dffe | 47 | * @param r_pstring Return arg; addr of ptr to string |
dadaista | 0:14e5e829dffe | 48 | */ |
dadaista | 0:14e5e829dffe | 49 | #define string_new(paddr, r_pstring) \ |
dadaista | 0:14e5e829dffe | 50 | string_create(MEMSPACE_RAM, (uint8_t const **)(paddr), 0, (int16_t)1, (r_pstring)) |
dadaista | 0:14e5e829dffe | 51 | |
dadaista | 0:14e5e829dffe | 52 | /** |
dadaista | 0:14e5e829dffe | 53 | * Creates String object from character array in RAM which may contain |
dadaista | 0:14e5e829dffe | 54 | * embedded null characters. |
dadaista | 0:14e5e829dffe | 55 | * |
dadaista | 0:14e5e829dffe | 56 | * @param paddr pointer to address of source string |
dadaista | 0:14e5e829dffe | 57 | * @param len length of source string |
dadaista | 0:14e5e829dffe | 58 | * @param r_pstring Return arg; addr of ptr to string |
dadaista | 0:14e5e829dffe | 59 | */ |
dadaista | 0:14e5e829dffe | 60 | #define string_newWithLen(paddr, len, r_pstring) \ |
dadaista | 0:14e5e829dffe | 61 | string_create(MEMSPACE_RAM, (uint8_t const **)(paddr), (len), (int16_t)1, \ |
dadaista | 0:14e5e829dffe | 62 | (r_pstring)) |
dadaista | 0:14e5e829dffe | 63 | |
dadaista | 0:14e5e829dffe | 64 | /** |
dadaista | 0:14e5e829dffe | 65 | * Creates String object by replicating an existing C string, n times |
dadaista | 0:14e5e829dffe | 66 | * |
dadaista | 0:14e5e829dffe | 67 | * @param paddr pointer to address of source string |
dadaista | 0:14e5e829dffe | 68 | * @param n number of times to replicate the source string |
dadaista | 0:14e5e829dffe | 69 | * @param r_pstring Return arg; addr of ptr to string |
dadaista | 0:14e5e829dffe | 70 | */ |
dadaista | 0:14e5e829dffe | 71 | #define string_replicate(paddr, n, r_pstring) \ |
dadaista | 0:14e5e829dffe | 72 | string_create(MEMSPACE_RAM, (paddr), (uint8_t)0, (n), (r_pstring)) |
dadaista | 0:14e5e829dffe | 73 | |
dadaista | 0:14e5e829dffe | 74 | /*************************************************************** |
dadaista | 0:14e5e829dffe | 75 | * Types |
dadaista | 0:14e5e829dffe | 76 | **************************************************************/ |
dadaista | 0:14e5e829dffe | 77 | |
dadaista | 0:14e5e829dffe | 78 | /** |
dadaista | 0:14e5e829dffe | 79 | * String obj |
dadaista | 0:14e5e829dffe | 80 | * |
dadaista | 0:14e5e829dffe | 81 | * Null terminated array of chars. |
dadaista | 0:14e5e829dffe | 82 | */ |
dadaista | 0:14e5e829dffe | 83 | typedef struct PmString_s |
dadaista | 0:14e5e829dffe | 84 | { |
dadaista | 0:14e5e829dffe | 85 | /** Object descriptor */ |
dadaista | 0:14e5e829dffe | 86 | PmObjDesc_t od; |
dadaista | 0:14e5e829dffe | 87 | |
dadaista | 0:14e5e829dffe | 88 | /** Length of string */ |
dadaista | 0:14e5e829dffe | 89 | uint16_t length; |
dadaista | 0:14e5e829dffe | 90 | |
dadaista | 0:14e5e829dffe | 91 | #if USE_STRING_CACHE |
dadaista | 0:14e5e829dffe | 92 | /** Ptr to next string in cache */ |
dadaista | 0:14e5e829dffe | 93 | struct PmString_s *next; |
dadaista | 0:14e5e829dffe | 94 | #endif /* USE_STRING_CACHE */ |
dadaista | 0:14e5e829dffe | 95 | |
dadaista | 0:14e5e829dffe | 96 | /** |
dadaista | 0:14e5e829dffe | 97 | * Null-term char array |
dadaista | 0:14e5e829dffe | 98 | * |
dadaista | 0:14e5e829dffe | 99 | * Use length 1 here so that string-alloc function can use |
dadaista | 0:14e5e829dffe | 100 | * "sizeof(PmString_t) + len" and there will be room for the null-term |
dadaista | 0:14e5e829dffe | 101 | */ |
dadaista | 0:14e5e829dffe | 102 | uint8_t val[1]; |
dadaista | 0:14e5e829dffe | 103 | } PmString_t, |
dadaista | 0:14e5e829dffe | 104 | *pPmString_t; |
dadaista | 0:14e5e829dffe | 105 | |
dadaista | 0:14e5e829dffe | 106 | |
dadaista | 0:14e5e829dffe | 107 | /*************************************************************** |
dadaista | 0:14e5e829dffe | 108 | * Prototypes |
dadaista | 0:14e5e829dffe | 109 | **************************************************************/ |
dadaista | 0:14e5e829dffe | 110 | |
dadaista | 0:14e5e829dffe | 111 | /** |
dadaista | 0:14e5e829dffe | 112 | * Creates a new String obj. |
dadaista | 0:14e5e829dffe | 113 | * If len is less than zero, load from a String image. |
dadaista | 0:14e5e829dffe | 114 | * If len is zero, copy from a C string (which has a null terminator) |
dadaista | 0:14e5e829dffe | 115 | * If len is positive, copy as many chars as given in the len argument |
dadaista | 0:14e5e829dffe | 116 | * A string image has the following structure: |
dadaista | 0:14e5e829dffe | 117 | * -type: int8 - OBJ_TYPE_STRING |
dadaista | 0:14e5e829dffe | 118 | * -length: uint16 - number of bytes in the string |
dadaista | 0:14e5e829dffe | 119 | * -val: uint8[] - array of chars with null term |
dadaista | 0:14e5e829dffe | 120 | * |
dadaista | 0:14e5e829dffe | 121 | * Returns by reference a ptr to String obj. |
dadaista | 0:14e5e829dffe | 122 | * |
dadaista | 0:14e5e829dffe | 123 | * Obtain space for String from the heap. |
dadaista | 0:14e5e829dffe | 124 | * Copy string from memspace. |
dadaista | 0:14e5e829dffe | 125 | * Leave contents of paddr pointing one byte past end of str. |
dadaista | 0:14e5e829dffe | 126 | * |
dadaista | 0:14e5e829dffe | 127 | * THE PROGRAMMER SHOULD NOT CALL THIS FUNCTION DIRECTLY. |
dadaista | 0:14e5e829dffe | 128 | * Instead, use one of the two macros string_loadFromImg() |
dadaista | 0:14e5e829dffe | 129 | * or string_new(). |
dadaista | 0:14e5e829dffe | 130 | * |
dadaista | 0:14e5e829dffe | 131 | * @param memspace memory space where *paddr points |
dadaista | 0:14e5e829dffe | 132 | * @param paddr ptr to ptr to null term character array or image. |
dadaista | 0:14e5e829dffe | 133 | * @param len length of the C character array |
dadaista | 0:14e5e829dffe | 134 | * (use -1 for string images, 0 for C strings) |
dadaista | 0:14e5e829dffe | 135 | * @param n Number of times to replicate the given string argument |
dadaista | 0:14e5e829dffe | 136 | * @param r_pstring Return by reference; ptr to String obj |
dadaista | 0:14e5e829dffe | 137 | * @return Return status |
dadaista | 0:14e5e829dffe | 138 | */ |
dadaista | 0:14e5e829dffe | 139 | PmReturn_t string_create(PmMemSpace_t memspace, uint8_t const **paddr, |
dadaista | 0:14e5e829dffe | 140 | int16_t len, int16_t n, pPmObj_t *r_pstring); |
dadaista | 0:14e5e829dffe | 141 | |
dadaista | 0:14e5e829dffe | 142 | /** |
dadaista | 0:14e5e829dffe | 143 | * Creates a new String object from a single character. |
dadaista | 0:14e5e829dffe | 144 | * |
dadaista | 0:14e5e829dffe | 145 | * @param c The character to become the string |
dadaista | 0:14e5e829dffe | 146 | * @param r_pstring Return by reference; ptr to String obj |
dadaista | 0:14e5e829dffe | 147 | * @return Return status |
dadaista | 0:14e5e829dffe | 148 | */ |
dadaista | 0:14e5e829dffe | 149 | PmReturn_t string_newFromChar(uint8_t const c, pPmObj_t *r_pstring); |
dadaista | 0:14e5e829dffe | 150 | |
dadaista | 0:14e5e829dffe | 151 | /** |
dadaista | 0:14e5e829dffe | 152 | * Compares two String objects for equality. |
dadaista | 0:14e5e829dffe | 153 | * |
dadaista | 0:14e5e829dffe | 154 | * @param pstr1 Ptr to first string |
dadaista | 0:14e5e829dffe | 155 | * @param pstr2 Ptr to second string |
dadaista | 0:14e5e829dffe | 156 | * @return C_SAME if the strings are equivalent, C_DIFFER otherwise |
dadaista | 0:14e5e829dffe | 157 | */ |
dadaista | 0:14e5e829dffe | 158 | int8_t string_compare(pPmString_t pstr1, pPmString_t pstr2); |
dadaista | 0:14e5e829dffe | 159 | |
dadaista | 0:14e5e829dffe | 160 | #ifdef HAVE_PRINT |
dadaista | 0:14e5e829dffe | 161 | /** |
dadaista | 0:14e5e829dffe | 162 | * Sends out a string object bytewise. Escaping and framing is configurable |
dadaista | 0:14e5e829dffe | 163 | * via marshall. |
dadaista | 0:14e5e829dffe | 164 | * |
dadaista | 0:14e5e829dffe | 165 | * @param pobj Ptr to string object |
dadaista | 0:14e5e829dffe | 166 | * @param marshall If 0, print out string as is. Otherwise escape unprintable |
dadaista | 0:14e5e829dffe | 167 | * characters and surround string with single quotes. |
dadaista | 0:14e5e829dffe | 168 | * @return Return status |
dadaista | 0:14e5e829dffe | 169 | */ |
dadaista | 0:14e5e829dffe | 170 | PmReturn_t string_print(pPmObj_t pstr, uint8_t marshall); |
dadaista | 0:14e5e829dffe | 171 | #endif /* HAVE_PRINT */ |
dadaista | 0:14e5e829dffe | 172 | |
dadaista | 0:14e5e829dffe | 173 | /** |
dadaista | 0:14e5e829dffe | 174 | * Clears the string cache if one exists. |
dadaista | 0:14e5e829dffe | 175 | * Called by heap_init() |
dadaista | 0:14e5e829dffe | 176 | * |
dadaista | 0:14e5e829dffe | 177 | * @return Return status |
dadaista | 0:14e5e829dffe | 178 | */ |
dadaista | 0:14e5e829dffe | 179 | PmReturn_t string_cacheInit(void); |
dadaista | 0:14e5e829dffe | 180 | |
dadaista | 0:14e5e829dffe | 181 | |
dadaista | 0:14e5e829dffe | 182 | /** Returns a pointer to the base of the string cache */ |
dadaista | 0:14e5e829dffe | 183 | PmReturn_t string_getCache(pPmString_t **r_ppstrcache); |
dadaista | 0:14e5e829dffe | 184 | |
dadaista | 0:14e5e829dffe | 185 | /** |
dadaista | 0:14e5e829dffe | 186 | * Returns a new string object that is the concatenation |
dadaista | 0:14e5e829dffe | 187 | * of the two given strings. |
dadaista | 0:14e5e829dffe | 188 | * |
dadaista | 0:14e5e829dffe | 189 | * @param pstr1 First source string |
dadaista | 0:14e5e829dffe | 190 | * @param pstr2 Second source string |
dadaista | 0:14e5e829dffe | 191 | * @param r_pstring Return arg; ptr to new string object |
dadaista | 0:14e5e829dffe | 192 | * @return Return status |
dadaista | 0:14e5e829dffe | 193 | */ |
dadaista | 0:14e5e829dffe | 194 | PmReturn_t |
dadaista | 0:14e5e829dffe | 195 | string_concat(pPmString_t pstr1, pPmString_t pstr2, pPmObj_t *r_pstring); |
dadaista | 0:14e5e829dffe | 196 | |
dadaista | 0:14e5e829dffe | 197 | #ifdef HAVE_STRING_FORMAT |
dadaista | 0:14e5e829dffe | 198 | /** |
dadaista | 0:14e5e829dffe | 199 | * Returns a new string object that is created from the given format string |
dadaista | 0:14e5e829dffe | 200 | * and the argument(s). |
dadaista | 0:14e5e829dffe | 201 | * |
dadaista | 0:14e5e829dffe | 202 | * @param pstr Format string object |
dadaista | 0:14e5e829dffe | 203 | * @param parg Single argument or tuple of arguments |
dadaista | 0:14e5e829dffe | 204 | * @param r_pstring Return arg; ptr to new string object |
dadaista | 0:14e5e829dffe | 205 | * @return Return status |
dadaista | 0:14e5e829dffe | 206 | */ |
dadaista | 0:14e5e829dffe | 207 | PmReturn_t string_format(pPmString_t pstr, pPmObj_t parg, pPmObj_t *r_pstring); |
dadaista | 0:14e5e829dffe | 208 | |
dadaista | 0:14e5e829dffe | 209 | /** |
dadaista | 0:14e5e829dffe | 210 | * Prints n bytes, formatting them if marshall is true |
dadaista | 0:14e5e829dffe | 211 | * |
dadaista | 0:14e5e829dffe | 212 | * @param pb Pointer to C bytes |
dadaista | 0:14e5e829dffe | 213 | * @param marshall Boolean true if bytes are to be formatted |
dadaista | 0:14e5e829dffe | 214 | * @param n Number of bytes to print |
dadaista | 0:14e5e829dffe | 215 | * @return Return status |
dadaista | 0:14e5e829dffe | 216 | */ |
dadaista | 0:14e5e829dffe | 217 | PmReturn_t string_printFormattedBytes(uint8_t *pb, |
dadaista | 0:14e5e829dffe | 218 | uint8_t marshall, |
dadaista | 0:14e5e829dffe | 219 | uint16_t n); |
dadaista | 0:14e5e829dffe | 220 | #endif /* HAVE_STRING_FORMAT */ |
dadaista | 0:14e5e829dffe | 221 | |
dadaista | 0:14e5e829dffe | 222 | #endif /* __STRING_H__ */ |