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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
/**
* @file tcl_ref.c Purple Tcl typed references API
*
* purple
*
* Copyright (C) 2006 Ethan Blanton <eblanton@cs.purdue.edu>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include <tcl.h>
#include <glib.h>
#include "tcl_purple.h"
#include "stringref.h"
/* Instead of all that internal representation mumbo jumbo, use these
* macros to access the internal representation of a PurpleTclRef */
#define OBJ_REF_TYPE(obj) (obj->internalRep.twoPtrValue.ptr1)
#define OBJ_REF_VALUE(obj) (obj->internalRep.twoPtrValue.ptr2)
static Tcl_FreeInternalRepProc purple_tcl_ref_free;
static Tcl_DupInternalRepProc purple_tcl_ref_dup;
static Tcl_UpdateStringProc purple_tcl_ref_update;
static Tcl_SetFromAnyProc purple_tcl_ref_set;
static Tcl_ObjType purple_tcl_ref = {
"PurpleTclRef",
purple_tcl_ref_free,
purple_tcl_ref_dup,
purple_tcl_ref_update,
purple_tcl_ref_set
};
void purple_tcl_ref_init()
{
Tcl_RegisterObjType(&purple_tcl_ref);
}
void *purple_tcl_ref_get(Tcl_Interp *interp, Tcl_Obj *obj, PurpleStringref *type)
{
if (obj->typePtr != &purple_tcl_ref) {
if (Tcl_ConvertToType(interp, obj, &purple_tcl_ref) != TCL_OK)
return NULL;
}
if (strcmp(purple_stringref_value(OBJ_REF_TYPE(obj)),
purple_stringref_value(type))) {
if (interp) {
Tcl_Obj *error = Tcl_NewStringObj("Bad Purple reference type: expected ", -1);
Tcl_AppendToObj(error, purple_stringref_value(type), -1);
Tcl_AppendToObj(error, " but got ", -1);
Tcl_AppendToObj(error, purple_stringref_value(OBJ_REF_TYPE(obj)), -1);
Tcl_SetObjResult(interp, error);
}
return NULL;
}
return OBJ_REF_VALUE(obj);
}
Tcl_Obj *purple_tcl_ref_new(PurpleStringref *type, void *value)
{
Tcl_Obj *obj = Tcl_NewObj();
obj->typePtr = &purple_tcl_ref;
OBJ_REF_TYPE(obj) = purple_stringref_ref(type);
OBJ_REF_VALUE(obj) = value;
Tcl_InvalidateStringRep(obj);
return obj;
}
static void purple_tcl_ref_free(Tcl_Obj *obj)
{
purple_stringref_unref(OBJ_REF_TYPE(obj));
}
static void purple_tcl_ref_dup(Tcl_Obj *obj1, Tcl_Obj *obj2)
{
OBJ_REF_TYPE(obj2) = purple_stringref_ref(OBJ_REF_TYPE(obj1));
OBJ_REF_VALUE(obj2) = OBJ_REF_VALUE(obj1);
}
static void purple_tcl_ref_update(Tcl_Obj *obj)
{
/* This is ugly on memory, but we pretty much have to either
* do this or guesstimate lengths or introduce a varargs
* function in here ... ugh. */
char *bytes = g_strdup_printf("purple-%s:%p",
purple_stringref_value(OBJ_REF_TYPE(obj)),
OBJ_REF_VALUE(obj));
obj->length = strlen(bytes);
obj->bytes = ckalloc(obj->length + 1);
strcpy(obj->bytes, bytes);
g_free(bytes);
}
/* This isn't as memory-efficient as setting could be, because we
* essentially have to synthesize the Stringref here, where we would
* really rather dup it. Oh, well. */
static int purple_tcl_ref_set(Tcl_Interp *interp, Tcl_Obj *obj)
{
char *bytes = Tcl_GetStringFromObj(obj, NULL);
char *ptr;
PurpleStringref *type;
void *value;
static const char prefix[] = "purple-";
static const int prefixlen = sizeof(prefix) - 1;
if (strlen(bytes) < prefixlen
|| strncmp(bytes, prefix, prefixlen)
|| (ptr = strchr(bytes, ':')) == NULL
|| (ptr - bytes) == prefixlen)
goto badobject;
/* Bad Ethan */
*ptr = '\0';
type = purple_stringref_new(bytes + prefixlen);
*ptr = ':';
ptr++;
if (sscanf(ptr, "%p", &value) == 0) {
purple_stringref_unref(type);
goto badobject;
}
/* At this point we know we have a good object; free the old and
* install our internal representation. */
if (obj->typePtr != NULL && obj->typePtr->freeIntRepProc != NULL)
obj->typePtr->freeIntRepProc(obj);
obj->typePtr = &purple_tcl_ref;
OBJ_REF_TYPE(obj) = type;
OBJ_REF_VALUE(obj) = value;
return TCL_OK;
badobject:
if (interp) {
Tcl_SetObjResult(interp,
Tcl_NewStringObj("invalid PurpleTclRef representation", -1));
}
return TCL_ERROR;
}
|