davide carboni / Mbed 2 deprecated pymite_http_get

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers tuple.c Source File

tuple.c

Go to the documentation of this file.
00001 /*
00002 # This file is Copyright 2003, 2006, 2007, 2009 Dean Hall.
00003 #
00004 # This file is part of the PyMite VM.
00005 # The PyMite VM is free software: you can redistribute it and/or modify
00006 # it under the terms of the GNU GENERAL PUBLIC LICENSE Version 2.
00007 #
00008 # The PyMite VM is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00011 # A copy of the GNU GENERAL PUBLIC LICENSE Version 2
00012 # is seen in the file COPYING in this directory.
00013 */
00014 
00015 
00016 #undef __FILE_ID__
00017 #define __FILE_ID__ 0x13
00018 
00019 
00020 /**
00021  * \file
00022  * \brief Tuple Object Type
00023  *
00024  * Tuple object type operations.
00025  */
00026 
00027 
00028 #include "pm.h"
00029 
00030 
00031 /* The follwing value should match that in pmImgCreator.py */
00032 #define MAX_TUPLE_LEN 253
00033 
00034 
00035 PmReturn_t
00036 tuple_loadFromImg(PmMemSpace_t memspace,
00037                   uint8_t const **paddr, pPmObj_t *r_ptuple)
00038 {
00039     PmReturn_t retval = PM_RET_OK;
00040     uint8_t i = (uint8_t)0;
00041     uint8_t n = (uint8_t)0;
00042 
00043     /* Get num objs in tuple */
00044     n = mem_getByte(memspace, paddr);
00045 
00046     /* Create empty tuple */
00047     retval = tuple_new(n, r_ptuple);
00048     PM_RETURN_IF_ERROR(retval);
00049 
00050     /* Load the next n objs into tuple */
00051     for (i = (uint8_t)0; i < n; i++)
00052     {
00053         retval = obj_loadFromImg(memspace,
00054                                  paddr,
00055                                  (pPmObj_t *)&(((pPmTuple_t)*r_ptuple)->
00056                                                val[i]));
00057         PM_RETURN_IF_ERROR(retval);
00058     }
00059     return PM_RET_OK;
00060 }
00061 
00062 
00063 PmReturn_t
00064 tuple_new(uint16_t n, pPmObj_t *r_ptuple)
00065 {
00066     PmReturn_t retval = PM_RET_OK;
00067     uint16_t size = 0;
00068 
00069     /* Raise a SystemError for a Tuple that is too large */
00070     if (n > MAX_TUPLE_LEN)
00071     {
00072         PM_RAISE(retval, PM_RET_EX_SYS);
00073         return retval;
00074     }
00075 
00076     /* Calc size of struct to hold tuple; (n-1) because PmTuple_t has val[1] */
00077     size = sizeof(PmTuple_t) + ((n - 1) * sizeof(pPmObj_t));
00078 
00079     /* Allocate a tuple */
00080     retval = heap_getChunk(size, (uint8_t **)r_ptuple);
00081     PM_RETURN_IF_ERROR(retval);
00082     OBJ_SET_TYPE(*r_ptuple, OBJ_TYPE_TUP);
00083 
00084     /* Set the number of objs in the tuple */
00085     ((pPmTuple_t)*r_ptuple)->length = n;
00086 
00087     /* No need to null the ptrs because they are set by the caller */
00088     return retval;
00089 }
00090 
00091 
00092 PmReturn_t
00093 tuple_replicate(pPmObj_t ptup, int16_t n, pPmObj_t *r_ptuple)
00094 {
00095     PmReturn_t retval = PM_RET_OK;
00096     int16_t length;
00097     int16_t i;
00098     int16_t j;
00099 
00100     /* Raise TypeError if object is not a Tuple */
00101     if (OBJ_GET_TYPE(ptup) != OBJ_TYPE_TUP)
00102     {
00103         PM_RAISE(retval, PM_RET_EX_SYS);
00104         return retval;
00105     }
00106 
00107     C_ASSERT(n >= 0);
00108 
00109     /* Allocate the new tuple */
00110     length = ((pPmTuple_t)ptup)->length;
00111     retval = tuple_new(length * n, r_ptuple);
00112     PM_RETURN_IF_ERROR(retval);
00113 
00114     /* Copy src tuple the designated number of times */
00115     for (i = 0; i < n; i++)
00116     {
00117         for (j = 0; j < length; j++)
00118         {
00119             ((pPmTuple_t)*r_ptuple)->val[length * i + j] =
00120                 ((pPmTuple_t)ptup)->val[j];
00121         }
00122     }
00123     return retval;
00124 }
00125 
00126 
00127 PmReturn_t
00128 tuple_getItem(pPmObj_t ptup, int16_t index, pPmObj_t *r_pobj)
00129 {
00130     PmReturn_t retval = PM_RET_OK;
00131 
00132     /* Adjust for negative index */
00133     if (index < 0)
00134     {
00135         index += ((pPmTuple_t)ptup)->length;
00136     }
00137 
00138     /* Raise IndexError if index is out of bounds */
00139     if ((index < 0) || (index > ((pPmTuple_t)ptup)->length))
00140     {
00141         PM_RAISE(retval, PM_RET_EX_INDX);
00142     }
00143 
00144     /* Get the tuple item */
00145     *r_pobj = ((pPmTuple_t)ptup)->val[index];
00146 
00147     return retval;
00148 }
00149 
00150 
00151 #ifdef HAVE_PRINT
00152 PmReturn_t
00153 tuple_print(pPmObj_t ptup)
00154 {
00155     PmReturn_t retval = PM_RET_OK;
00156     int16_t index;
00157 
00158     C_ASSERT(ptup != C_NULL);
00159 
00160     /* If it's not a tuple, raise TypeError */
00161     if (OBJ_GET_TYPE(ptup) != OBJ_TYPE_TUP)
00162     {
00163         PM_RAISE(retval, PM_RET_EX_TYPE);
00164         return retval;
00165     }
00166 
00167     plat_putByte('(');
00168 
00169     for (index = 0; index < ((pPmTuple_t)ptup)->length; index++)
00170     {
00171         if (index != 0)
00172         {
00173             plat_putByte(',');
00174             plat_putByte(' ');
00175         }
00176         retval = obj_print(((pPmTuple_t)ptup)->val[index], 1);
00177         PM_RETURN_IF_ERROR(retval);
00178     }
00179 
00180     return plat_putByte(')');
00181 }
00182 #endif /* HAVE_PRINT */