Norimasa Okamoto
/
pymite
python-on-a-chip online compiler
- http://pymbed.appspot.com/
- https://code.google.com/p/python-on-a-chip/
- http://www.youtube.com/watch?v=Oyqc2bFRW9I
- https://bitbucket.org/va009039/pymbed/
more info: python-on-a-chip
vm/strobj.h@15:94ca5c8003e5, 2016-04-14 (annotated)
- Committer:
- va009039
- Date:
- Thu Apr 14 22:32:57 2016 +0000
- Revision:
- 15:94ca5c8003e5
- Parent:
- 0:65f1469d6bfb
update Nucleo-F401RE.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
va009039 | 0:65f1469d6bfb | 1 | /* |
va009039 | 0:65f1469d6bfb | 2 | # This file is Copyright 2002 Dean Hall. |
va009039 | 0:65f1469d6bfb | 3 | # This file is part of the PyMite VM. |
va009039 | 0:65f1469d6bfb | 4 | # This file is licensed under the MIT License. |
va009039 | 0:65f1469d6bfb | 5 | # See the LICENSE file for details. |
va009039 | 0:65f1469d6bfb | 6 | */ |
va009039 | 0:65f1469d6bfb | 7 | |
va009039 | 0:65f1469d6bfb | 8 | |
va009039 | 0:65f1469d6bfb | 9 | #ifndef __STRING_H__ |
va009039 | 0:65f1469d6bfb | 10 | #define __STRING_H__ |
va009039 | 0:65f1469d6bfb | 11 | |
va009039 | 0:65f1469d6bfb | 12 | |
va009039 | 0:65f1469d6bfb | 13 | /** |
va009039 | 0:65f1469d6bfb | 14 | * \file |
va009039 | 0:65f1469d6bfb | 15 | * \brief String Object Type |
va009039 | 0:65f1469d6bfb | 16 | * |
va009039 | 0:65f1469d6bfb | 17 | * String object type header. |
va009039 | 0:65f1469d6bfb | 18 | */ |
va009039 | 0:65f1469d6bfb | 19 | |
va009039 | 0:65f1469d6bfb | 20 | |
va009039 | 0:65f1469d6bfb | 21 | /** Set to nonzero to enable string cache. DO NOT REMOVE THE DEFINITION. */ |
va009039 | 0:65f1469d6bfb | 22 | #define USE_STRING_CACHE 1 |
va009039 | 0:65f1469d6bfb | 23 | |
va009039 | 0:65f1469d6bfb | 24 | |
va009039 | 0:65f1469d6bfb | 25 | /** |
va009039 | 0:65f1469d6bfb | 26 | * Loads a string from image |
va009039 | 0:65f1469d6bfb | 27 | * |
va009039 | 0:65f1469d6bfb | 28 | * @param ms memoryspace paddr points to |
va009039 | 0:65f1469d6bfb | 29 | * @param paddr address in memoryspace of source string |
va009039 | 0:65f1469d6bfb | 30 | * @param r_pstring Return by reference; a new string object |
va009039 | 0:65f1469d6bfb | 31 | * @return Return status |
va009039 | 0:65f1469d6bfb | 32 | */ |
va009039 | 0:65f1469d6bfb | 33 | #define string_loadFromImg(ms, paddr, r_pstring) \ |
va009039 | 0:65f1469d6bfb | 34 | string_create((ms), (paddr), (int16_t)-1, (int16_t)1, (r_pstring)) |
va009039 | 0:65f1469d6bfb | 35 | |
va009039 | 0:65f1469d6bfb | 36 | /** |
va009039 | 0:65f1469d6bfb | 37 | * Creates String object from character array in RAM |
va009039 | 0:65f1469d6bfb | 38 | * |
va009039 | 0:65f1469d6bfb | 39 | * @param paddr pointer to address of source string |
va009039 | 0:65f1469d6bfb | 40 | * @param r_pstring Return arg; addr of ptr to string |
va009039 | 0:65f1469d6bfb | 41 | */ |
va009039 | 0:65f1469d6bfb | 42 | #define string_new(paddr, r_pstring) \ |
va009039 | 0:65f1469d6bfb | 43 | string_create(MEMSPACE_RAM, (uint8_t const **)(paddr), 0, (int16_t)1, (r_pstring)) |
va009039 | 0:65f1469d6bfb | 44 | |
va009039 | 0:65f1469d6bfb | 45 | /** |
va009039 | 0:65f1469d6bfb | 46 | * Creates String object from character array in RAM which may contain |
va009039 | 0:65f1469d6bfb | 47 | * embedded null characters. |
va009039 | 0:65f1469d6bfb | 48 | * |
va009039 | 0:65f1469d6bfb | 49 | * @param paddr pointer to address of source string |
va009039 | 0:65f1469d6bfb | 50 | * @param len length of source string |
va009039 | 0:65f1469d6bfb | 51 | * @param r_pstring Return arg; addr of ptr to string |
va009039 | 0:65f1469d6bfb | 52 | */ |
va009039 | 0:65f1469d6bfb | 53 | #define string_newWithLen(paddr, len, r_pstring) \ |
va009039 | 0:65f1469d6bfb | 54 | string_create(MEMSPACE_RAM, (uint8_t const **)(paddr), (len), (int16_t)1, \ |
va009039 | 0:65f1469d6bfb | 55 | (r_pstring)) |
va009039 | 0:65f1469d6bfb | 56 | |
va009039 | 0:65f1469d6bfb | 57 | /** |
va009039 | 0:65f1469d6bfb | 58 | * Creates String object by replicating an existing C string, n times |
va009039 | 0:65f1469d6bfb | 59 | * |
va009039 | 0:65f1469d6bfb | 60 | * @param paddr pointer to address of source string |
va009039 | 0:65f1469d6bfb | 61 | * @param n number of times to replicate the source string |
va009039 | 0:65f1469d6bfb | 62 | * @param r_pstring Return arg; addr of ptr to string |
va009039 | 0:65f1469d6bfb | 63 | */ |
va009039 | 0:65f1469d6bfb | 64 | #define string_replicate(paddr, n, r_pstring) \ |
va009039 | 0:65f1469d6bfb | 65 | string_create(MEMSPACE_RAM, (paddr), (uint8_t)0, (n), (r_pstring)) |
va009039 | 0:65f1469d6bfb | 66 | |
va009039 | 0:65f1469d6bfb | 67 | /*************************************************************** |
va009039 | 0:65f1469d6bfb | 68 | * Types |
va009039 | 0:65f1469d6bfb | 69 | **************************************************************/ |
va009039 | 0:65f1469d6bfb | 70 | |
va009039 | 0:65f1469d6bfb | 71 | /** |
va009039 | 0:65f1469d6bfb | 72 | * String obj |
va009039 | 0:65f1469d6bfb | 73 | * |
va009039 | 0:65f1469d6bfb | 74 | * Null terminated array of chars. |
va009039 | 0:65f1469d6bfb | 75 | */ |
va009039 | 0:65f1469d6bfb | 76 | typedef struct PmString_s |
va009039 | 0:65f1469d6bfb | 77 | { |
va009039 | 0:65f1469d6bfb | 78 | /** Object descriptor */ |
va009039 | 0:65f1469d6bfb | 79 | PmObjDesc_t od; |
va009039 | 0:65f1469d6bfb | 80 | |
va009039 | 0:65f1469d6bfb | 81 | /** Length of string */ |
va009039 | 0:65f1469d6bfb | 82 | uint16_t length; |
va009039 | 0:65f1469d6bfb | 83 | |
va009039 | 0:65f1469d6bfb | 84 | #if USE_STRING_CACHE |
va009039 | 0:65f1469d6bfb | 85 | /** Ptr to next string in cache */ |
va009039 | 0:65f1469d6bfb | 86 | struct PmString_s *next; |
va009039 | 0:65f1469d6bfb | 87 | #endif /* USE_STRING_CACHE */ |
va009039 | 0:65f1469d6bfb | 88 | |
va009039 | 0:65f1469d6bfb | 89 | /** |
va009039 | 0:65f1469d6bfb | 90 | * Null-term char array |
va009039 | 0:65f1469d6bfb | 91 | * |
va009039 | 0:65f1469d6bfb | 92 | * Use length 1 here so that string-alloc function can use |
va009039 | 0:65f1469d6bfb | 93 | * "sizeof(PmString_t) + len" and there will be room for the null-term |
va009039 | 0:65f1469d6bfb | 94 | */ |
va009039 | 0:65f1469d6bfb | 95 | uint8_t val[1]; |
va009039 | 0:65f1469d6bfb | 96 | } PmString_t, |
va009039 | 0:65f1469d6bfb | 97 | *pPmString_t; |
va009039 | 0:65f1469d6bfb | 98 | |
va009039 | 0:65f1469d6bfb | 99 | |
va009039 | 0:65f1469d6bfb | 100 | /*************************************************************** |
va009039 | 0:65f1469d6bfb | 101 | * Prototypes |
va009039 | 0:65f1469d6bfb | 102 | **************************************************************/ |
va009039 | 0:65f1469d6bfb | 103 | |
va009039 | 0:65f1469d6bfb | 104 | /** |
va009039 | 0:65f1469d6bfb | 105 | * Creates a new String obj. |
va009039 | 0:65f1469d6bfb | 106 | * If len is less than zero, load from a String image. |
va009039 | 0:65f1469d6bfb | 107 | * If len is zero, copy from a C string (which has a null terminator) |
va009039 | 0:65f1469d6bfb | 108 | * If len is positive, copy as many chars as given in the len argument |
va009039 | 0:65f1469d6bfb | 109 | * A string image has the following structure: |
va009039 | 0:65f1469d6bfb | 110 | * -type: int8 - OBJ_TYPE_STRING |
va009039 | 0:65f1469d6bfb | 111 | * -length: uint16 - number of bytes in the string |
va009039 | 0:65f1469d6bfb | 112 | * -val: uint8[] - array of chars with null term |
va009039 | 0:65f1469d6bfb | 113 | * |
va009039 | 0:65f1469d6bfb | 114 | * Returns by reference a ptr to String obj. |
va009039 | 0:65f1469d6bfb | 115 | * |
va009039 | 0:65f1469d6bfb | 116 | * Obtain space for String from the heap. |
va009039 | 0:65f1469d6bfb | 117 | * Copy string from memspace. |
va009039 | 0:65f1469d6bfb | 118 | * Leave contents of paddr pointing one byte past end of str. |
va009039 | 0:65f1469d6bfb | 119 | * |
va009039 | 0:65f1469d6bfb | 120 | * THE PROGRAMMER SHOULD NOT CALL THIS FUNCTION DIRECTLY. |
va009039 | 0:65f1469d6bfb | 121 | * Instead, use one of the two macros string_loadFromImg() |
va009039 | 0:65f1469d6bfb | 122 | * or string_new(). |
va009039 | 0:65f1469d6bfb | 123 | * |
va009039 | 0:65f1469d6bfb | 124 | * @param memspace memory space where *paddr points |
va009039 | 0:65f1469d6bfb | 125 | * @param paddr ptr to ptr to null term character array or image. |
va009039 | 0:65f1469d6bfb | 126 | * @param len length of the C character array |
va009039 | 0:65f1469d6bfb | 127 | * (use -1 for string images, 0 for C strings) |
va009039 | 0:65f1469d6bfb | 128 | * @param n Number of times to replicate the given string argument |
va009039 | 0:65f1469d6bfb | 129 | * @param r_pstring Return by reference; ptr to String obj |
va009039 | 0:65f1469d6bfb | 130 | * @return Return status |
va009039 | 0:65f1469d6bfb | 131 | */ |
va009039 | 0:65f1469d6bfb | 132 | PmReturn_t string_create(PmMemSpace_t memspace, uint8_t const **paddr, |
va009039 | 0:65f1469d6bfb | 133 | int16_t len, int16_t n, pPmObj_t *r_pstring); |
va009039 | 0:65f1469d6bfb | 134 | |
va009039 | 0:65f1469d6bfb | 135 | /** |
va009039 | 0:65f1469d6bfb | 136 | * Creates a new String object from a single character. |
va009039 | 0:65f1469d6bfb | 137 | * |
va009039 | 0:65f1469d6bfb | 138 | * @param c The character to become the string |
va009039 | 0:65f1469d6bfb | 139 | * @param r_pstring Return by reference; ptr to String obj |
va009039 | 0:65f1469d6bfb | 140 | * @return Return status |
va009039 | 0:65f1469d6bfb | 141 | */ |
va009039 | 0:65f1469d6bfb | 142 | PmReturn_t string_newFromChar(uint8_t const c, pPmObj_t *r_pstring); |
va009039 | 0:65f1469d6bfb | 143 | |
va009039 | 0:65f1469d6bfb | 144 | /** |
va009039 | 0:65f1469d6bfb | 145 | * Compares two String objects for equality. |
va009039 | 0:65f1469d6bfb | 146 | * |
va009039 | 0:65f1469d6bfb | 147 | * @param pstr1 Ptr to first string |
va009039 | 0:65f1469d6bfb | 148 | * @param pstr2 Ptr to second string |
va009039 | 0:65f1469d6bfb | 149 | * @return C_SAME if the strings are equivalent, C_DIFFER otherwise |
va009039 | 0:65f1469d6bfb | 150 | */ |
va009039 | 0:65f1469d6bfb | 151 | int8_t string_compare(pPmString_t pstr1, pPmString_t pstr2); |
va009039 | 0:65f1469d6bfb | 152 | |
va009039 | 0:65f1469d6bfb | 153 | #ifdef HAVE_PRINT |
va009039 | 0:65f1469d6bfb | 154 | /** |
va009039 | 0:65f1469d6bfb | 155 | * Sends out a string object bytewise. Escaping and framing is configurable |
va009039 | 0:65f1469d6bfb | 156 | * via is_escaped. |
va009039 | 0:65f1469d6bfb | 157 | * |
va009039 | 0:65f1469d6bfb | 158 | * @param pstr Ptr to string object |
va009039 | 0:65f1469d6bfb | 159 | * @param is_escaped If 0, print out string as is. Otherwise escape unprintable |
va009039 | 0:65f1469d6bfb | 160 | * characters and surround string with single quotes. |
va009039 | 0:65f1469d6bfb | 161 | * @return Return status |
va009039 | 0:65f1469d6bfb | 162 | */ |
va009039 | 0:65f1469d6bfb | 163 | PmReturn_t string_print(pPmObj_t pstr, uint8_t is_escaped); |
va009039 | 0:65f1469d6bfb | 164 | #endif /* HAVE_PRINT */ |
va009039 | 0:65f1469d6bfb | 165 | |
va009039 | 0:65f1469d6bfb | 166 | /** |
va009039 | 0:65f1469d6bfb | 167 | * Clears the string cache if one exists. |
va009039 | 0:65f1469d6bfb | 168 | * Called by heap_init() |
va009039 | 0:65f1469d6bfb | 169 | * |
va009039 | 0:65f1469d6bfb | 170 | * @return Return status |
va009039 | 0:65f1469d6bfb | 171 | */ |
va009039 | 0:65f1469d6bfb | 172 | PmReturn_t string_cacheInit(void); |
va009039 | 0:65f1469d6bfb | 173 | |
va009039 | 0:65f1469d6bfb | 174 | |
va009039 | 0:65f1469d6bfb | 175 | /** Returns a pointer to the base of the string cache */ |
va009039 | 0:65f1469d6bfb | 176 | PmReturn_t string_getCache(pPmString_t **r_ppstrcache); |
va009039 | 0:65f1469d6bfb | 177 | |
va009039 | 0:65f1469d6bfb | 178 | /** |
va009039 | 0:65f1469d6bfb | 179 | * Returns a new string object that is the concatenation |
va009039 | 0:65f1469d6bfb | 180 | * of the two given strings. |
va009039 | 0:65f1469d6bfb | 181 | * |
va009039 | 0:65f1469d6bfb | 182 | * @param pstr1 First source string |
va009039 | 0:65f1469d6bfb | 183 | * @param pstr2 Second source string |
va009039 | 0:65f1469d6bfb | 184 | * @param r_pstring Return arg; ptr to new string object |
va009039 | 0:65f1469d6bfb | 185 | * @return Return status |
va009039 | 0:65f1469d6bfb | 186 | */ |
va009039 | 0:65f1469d6bfb | 187 | PmReturn_t |
va009039 | 0:65f1469d6bfb | 188 | string_concat(pPmString_t pstr1, pPmString_t pstr2, pPmObj_t *r_pstring); |
va009039 | 0:65f1469d6bfb | 189 | |
va009039 | 0:65f1469d6bfb | 190 | /** |
va009039 | 0:65f1469d6bfb | 191 | * Returns a new string object that is created from the given format string |
va009039 | 0:65f1469d6bfb | 192 | * and the argument(s). |
va009039 | 0:65f1469d6bfb | 193 | * |
va009039 | 0:65f1469d6bfb | 194 | * @param pstr Format string object |
va009039 | 0:65f1469d6bfb | 195 | * @param parg Single argument or tuple of arguments |
va009039 | 0:65f1469d6bfb | 196 | * @param r_pstring Return arg; ptr to new string object |
va009039 | 0:65f1469d6bfb | 197 | * @return Return status |
va009039 | 0:65f1469d6bfb | 198 | */ |
va009039 | 0:65f1469d6bfb | 199 | PmReturn_t string_format(pPmString_t pstr, pPmObj_t parg, pPmObj_t *r_pstring); |
va009039 | 0:65f1469d6bfb | 200 | |
va009039 | 0:65f1469d6bfb | 201 | #ifdef HAVE_PRINT |
va009039 | 0:65f1469d6bfb | 202 | /** |
va009039 | 0:65f1469d6bfb | 203 | * Prints n bytes, formatting them if is_escaped is true |
va009039 | 0:65f1469d6bfb | 204 | * |
va009039 | 0:65f1469d6bfb | 205 | * @param pb Pointer to C bytes |
va009039 | 0:65f1469d6bfb | 206 | * @param is_escaped Boolean true if string is to be escaped |
va009039 | 0:65f1469d6bfb | 207 | * @param n Number of bytes to print |
va009039 | 0:65f1469d6bfb | 208 | * @return Return status |
va009039 | 0:65f1469d6bfb | 209 | */ |
va009039 | 0:65f1469d6bfb | 210 | PmReturn_t string_printFormattedBytes(uint8_t *pb, |
va009039 | 0:65f1469d6bfb | 211 | uint8_t is_escaped, |
va009039 | 0:65f1469d6bfb | 212 | uint16_t n); |
va009039 | 0:65f1469d6bfb | 213 | #endif /* HAVE_PRINT */ |
va009039 | 0:65f1469d6bfb | 214 | |
va009039 | 0:65f1469d6bfb | 215 | #endif /* __STRING_H__ */ |