コード例 #1
0
ファイル: IoSeq_immutable.c プロジェクト: doublec/io
IO_METHOD(IoSeq, between)
{
	/*doc Sequence between(aSequence, anotherSequence)
	Returns a new Sequence containing the bytes between the
	occurance of aSequence and anotherSequence in the receiver.
	*/

	long start = 0;
	long end = 0;
	IoSeq *fromSeq, *toSeq;

	fromSeq = (IoSeq *)IoMessage_locals_valueArgAt_(m, locals, 0);

	if (ISSEQ(fromSeq))
	{
		start = UArray_find_from_(DATA(self), DATA(fromSeq), 0) + IoSeq_rawSize(fromSeq);

		if (start == -1)
		{
			start = 0;
		}
	}
	else if (ISNIL(fromSeq))
	{
		start = 0;
	}
	else
	{
		IoState_error_(IOSTATE, m, "Nil or Sequence argument required for arg 0, not a %s",
					IoObject_name((IoObject *)fromSeq));
	}

	toSeq = (IoSeq *)IoMessage_locals_valueArgAt_(m, locals, 1);

	if (ISSEQ(toSeq))
	{
		end = UArray_find_from_(DATA(self), DATA(toSeq), start);
		if (end == -1) start = UArray_size(DATA(self));
	}
	else if (ISNIL(toSeq))
	{
		end = UArray_size(DATA(self));
	}
	else
	{
		IoState_error_(IOSTATE, m, "Nil or Sequence argument required for arg 1, not a %s",
					IoObject_name((IoObject *)toSeq));
	}

	{
		UArray *ba = UArray_slice(DATA(self), start, end);
		IoSeq *result = IoSeq_newWithUArray_copy_(IOSTATE, ba, 0);
		return result;
	}
}
コード例 #2
0
ファイル: IoMySQL.c プロジェクト: jamesrburgess/io
IoObject* IoMySQL_connect(IoObject* self, IoObject* locals, IoMessage* m) {
    IoObject *host = NULL, *user = NULL, *password = NULL, *database = NULL, *port = NULL, *socket = NULL, *ssl = NULL;

    /*doc MySQL connect(host, user, password, database, port, unixSocket, useSSL)
    Connect to a MySQL database.
    */

    switch(IoMessage_argCount(m)) {
    case 7:
        ssl = IoMessage_locals_quickValueArgAt_(m, locals, 6);
    case 6:
        socket = IoMessage_locals_quickValueArgAt_(m, locals, 5);
    case 5:
        port = IoMessage_locals_quickValueArgAt_(m, locals, 4);
    case 4:
        database = IoMessage_locals_quickValueArgAt_(m, locals, 3);
    case 3:
        password = IoMessage_locals_quickValueArgAt_(m, locals, 2);
    case 2:
        user = IoMessage_locals_quickValueArgAt_(m, locals, 1);
    case 1:
        host = IoMessage_locals_quickValueArgAt_(m, locals, 0);
    }

    if(DATA(self)->connected)
    {
        mysql_close(&DATA(self)->connection);
        DATA(self)->connected = 0;
    }

    if(mysql_real_connect(
                &DATA(self)->connection,
                host && ISSEQ(host) ? IoSeq_asCString(host) : NULL,
                user && ISSEQ(user) ? IoSeq_asCString(user) : NULL,
                password && ISSEQ(password) ? IoSeq_asCString(password) : NULL,
                database && ISSEQ(database) ? IoSeq_asCString(database) : NULL,
                port && ISNUMBER(port) ? (unsigned) IoNumber_asInt(port) : 0,
                socket && ISSEQ(socket) ? IoSeq_asCString(socket) : NULL,
                ssl && ISFALSE(ssl) ? 0 : CLIENT_SSL
            )) {
        DATA(self)->connected = 1;

        IoObject_setSlot_to_(self, IOSYMBOL("host"), host ? host : IONIL(self));
        IoObject_setSlot_to_(self, IOSYMBOL("user"), user ? user : IONIL(self));
        IoObject_setSlot_to_(self, IOSYMBOL("password"), password ? password : IONIL(self));
        IoObject_setSlot_to_(self, IOSYMBOL("database"), database ? database : IONIL(self));
        IoObject_setSlot_to_(self, IOSYMBOL("port"), port ? port : IONIL(self));
        IoObject_setSlot_to_(self, IOSYMBOL("socket"), socket ? socket : IONIL(self));
        IoObject_setSlot_to_(self, IOSYMBOL("usingSSL"), ssl ? IOBOOL(self, ISTRUE(ssl)) : IOFALSE(self));
    }
    else
        IoState_error_(IOSTATE, m, "connection error(%d): %s", mysql_errno(&DATA(self)->connection), mysql_error(&DATA(self)->connection));

    return self;
}
コード例 #3
0
ファイル: IoSandbox.c プロジェクト: Habaut/GameBindings
IO_METHOD(IoSandbox, doSandboxString)
{
	/*doc Sandbox doSandboxString(aString)
	Evaluate aString inside the Sandbox.
	*/

	IoState *boxState = IoSandbox_boxState(self);
	char *s = IoMessage_locals_cStringArgAt_(m, locals, 0);

	IoObject *result = IoState_doSandboxCString_(boxState, s);

	if (ISSYMBOL(result))
	{
		return IOSYMBOL(CSTRING(result));
	}

	if (ISSEQ(result))
	{
		return IOSEQ(IOSEQ_BYTES(result), IOSEQ_LENGTH(result));
	}

	if (ISNUMBER(result))
	{
		return IONUMBER(CNUMBER(result));
	}

	return IONIL(self);
}
コード例 #4
0
ファイル: IoTagDB.c プロジェクト: Akiyah/io
Uint64Array *IoTagDB_tagArrayForTagNames_(IoTagDB *self, IoMessage *m, IoList *tagNames)
{
	TagDB *tdb = DATA(self);
	Uint64Array *tags = Uint64Array_new();
	int i;

	for (i = 0; i < IoList_rawSize(tagNames); i ++)
	{
		IoSeq *tagName = IoList_rawAt_(tagNames, i);
		symbolid_t keyid;
		
		IOASSERT(ISSEQ(tagName), "tag names must be Sequences");

		keyid = TagDB_idForSymbol_size_(tdb, CSTRING(tagName), IoSeq_rawSize(tagName));

		Uint64Array_append_(tags, keyid);
		/*
		{
		Datum *keyDatum = TagDB_symbolForId_(tdb, keyid);	
		printf("%s -> %i && ", CSTRING(tagName), (int)keyid);
		printf("%i -> %s\n", (int)keyid, (char *)(keyDatum->data));
		Datum_free(keyDatum);
		}
		*/
	}

	return tags;
}
コード例 #5
0
ファイル: IoTokyoCabinetPrefixCursor.c プロジェクト: ADTSH/io
IoObject *IoTokyoCabinetPrefixCursor_key(IoObject *self, IoObject *locals, IoMessage *m)
{
	/*doc TokyoCabinetPrefixCursor key
	Returns current cursor key or nil.
	*/
	
	int size;
	char *ks;
	
	IoSeq *prefix = IoObject_getSlot_(self, IOSYMBOL("prefix"));
	IOASSERT(ISSEQ(prefix), "prefix must be a sequence");
	IOASSERT(TokyoCabinetPrefixCursor(self), "invalid TokyoCabinetPrefixCursor");
	ks = tcbdbcurkey(TokyoCabinetPrefixCursor(self), &size);

	if (ks)
	{
		UArray *k = UArray_newWithData_type_size_copy_(ks, CTYPE_uint8_t, size, 1);
	
		if (UArray_beginsWith_(k, IoSeq_rawUArray(prefix)))
		{
			//printf("prefix '%s'\n", UArray_bytes(IoSeq_rawUArray(prefix)));
			//printf("before clip '%s'\n", UArray_bytes(k));
			UArray_clipBeforeEndOf_(k, IoSeq_rawUArray(prefix));
			UArray_removeFirst(k); // remove separator
			//printf("after clip  '%s'\n", UArray_bytes(k));
			return IoSeq_newWithUArray_copy_(IOSTATE, k, 0);
		}

		UArray_free(k);
	}

	return IONIL(self);
}
コード例 #6
0
ファイル: IoSeq_mutable.c プロジェクト: Alessandroo/io
IO_METHOD(IoSeq, replaceMap)
{
	/*doc Sequence replaceMap(aMap)
	In the receiver, the keys of aMap replaced with its values. Returns self.
	*/

	IoMap *map = IoMessage_locals_mapArgAt_(m, locals, 0);
	UArray *ba = DATA(self);

	IO_ASSERT_NOT_SYMBOL(self);

	PHASH_FOREACH(IoMap_rawHash(map), k, v,
		{
		IoSymbol *subSeq = k;
		IoSymbol *otherSeq = v;

		if (ISSEQ(otherSeq))
		{
			UArray_replace_with_(ba, DATA(subSeq), DATA(otherSeq));
		}
		else
		{
			IoState_error_(IOSTATE, m,
							"argument 0 to method '%s' must be a Map with Sequence values, not '%s'",
							CSTRING(IoMessage_name(m)), IoObject_name(otherSeq));
		}
		}
	);
コード例 #7
0
ファイル: IoMemcached.c プロジェクト: ADTSH/io
/*doc Memcached getMulti(keys)
Asks memcached to retrieve data corresponding to the list of keys.
Returns a Map with the results.
If some of the keys appearing in a retrieval request are not sent back
by the server in the item list this means that the server does not
hold items with such keys
*/
IoObject *IoMemcached_getMulti(IoMemcached *self, IoObject *locals, IoMessage *m)
{
	IoList *keys_list = IoMessage_locals_listArgAt_(m, locals, 0);
	size_t keys_list_size = IoList_rawSize(keys_list);

	IoObject *results_map = IoMap_new(IOSTATE);

	if(keys_list_size == 0)
		return results_map;

	int i;
	for(i = 0; i < keys_list_size; i++) {
		IoSeq *key = IoList_rawAt_(keys_list, i);
		IOASSERT(ISSEQ(key), "key must be a Sequence");
		IOASSERT(IOSEQ_LENGTH(key) > 0, "key cannot be an empty Sequence");
		IOASSERT(IOSEQ_LENGTH(key) < MEMCACHED_MAX_KEY, "key is too long");
	}

	const char **ckeys = (const char **) malloc(sizeof(const char *) * keys_list_size);
	size_t *ckey_lengths = (size_t *) malloc(sizeof(size_t) * keys_list_size);

	for(i = 0; i < keys_list_size; i++) {
		ckeys[i] = CSTRING(IoList_rawAt_(keys_list, i));
		ckey_lengths[i] = strlen(ckeys[i]);
	}

	memcached_return_t rc = memcached_mget(DATA(self)->mc, ckeys, ckey_lengths, keys_list_size);

	free(ckeys);
	free(ckey_lengths);

	char returned_key[MEMCACHED_MAX_KEY], *returned_value;
	size_t returned_key_length, returned_value_length;
	uint32_t flags;

	returned_value = memcached_fetch(DATA(self)->mc,
		returned_key, &returned_key_length,
		&returned_value_length, &flags, &rc
	);

	while(returned_value != NULL) {
		IoMap_rawAtPut(results_map,
			IoSeq_newSymbolWithData_length_(IOSTATE, returned_key, returned_key_length),
			IoMemcached_deserialize(self, returned_value, returned_value_length, flags)
		);

		free(returned_value);

		returned_value = memcached_fetch(DATA(self)->mc,
			returned_key, &returned_key_length,
			&returned_value_length, &flags, &rc
		);
	}

	return results_map;
}
コード例 #8
0
ファイル: IoSeq.c プロジェクト: asymmetric/io
int IoSeq_compare(IoSeq *self, IoSeq *v)
{
	if (ISSEQ(v))
	{
		if (self == v) return 0;
		return UArray_compare_(DATA(self), DATA(v));
	}

	return IoObject_defaultCompare(self, v);
}
コード例 #9
0
ファイル: IoMessage.c プロジェクト: mikedouglas/io
IoObject *IoMessage_locals_symbolArgAt_(IoMessage *self, IoObject *locals, int n)
{
	IoObject *v = IoMessage_locals_valueArgAt_(self, locals, n);

	if (!ISSEQ(v))
	{
		IoMessage_locals_numberArgAt_errorForType_(self, locals, n, "Sequence");
	}

	return IoSeq_rawAsSymbol(v);
}
コード例 #10
0
ファイル: IoLoudmouth.c プロジェクト: ADTSH/io
//doc Loudmouth connect Connects to the server. Returns <code>self</code>.
IoObject *IoLoudmouth_connect(IoLoudmouth *self, IoObject *locals, IoMessage *m) {
//  Q: Should we io_free() these?
  IoSeq* username   = IoObject_getSlot_(self, IOSYMBOL("username"));
  IoSeq* password   = IoObject_getSlot_(self, IOSYMBOL("password"));
  IoSeq* resource   = IoObject_getSlot_(self, IOSYMBOL("resource"));
  IoSeq* host       = IoObject_getSlot_(self, IOSYMBOL("host"));
  IoNumber* port    = IoObject_getSlot_(self, IOSYMBOL("port"));
  IoObject* use_ssl = IoObject_getSlot_(self, IOSYMBOL("useSsl"));

  IOASSERT(ISSEQ(username), "Loudmouth: username should be a Sequence");
  IOASSERT(ISSEQ(password), "Loudmouth: password should be a Sequence");
  IOASSERT(ISSEQ(resource), "Loudmouth: resource should be a Sequence");
  IOASSERT(ISSEQ(host),     "Loudmouth: host should be a Sequence");
  IOASSERT(ISNUMBER(port),  "Loudmouth: port should be a Number");

  if(LMCONN(self) == NULL) {
    LmConnection *connection = lm_connection_new_with_context(CSTRING(host), main_context);
    IoObject_setDataPointer_(self, connection);

    lm_connection_set_jid(connection, CSTRING(IoObject_getSlot_(self, IOSYMBOL("jid"))));
    lm_connection_set_port(connection, CNUMBER(port));

    if(ISTRUE(use_ssl) && lm_ssl_is_supported()) {
      LmSSL *ssl = lm_ssl_new(NULL, onSslError, NULL, NULL);
      lm_connection_set_ssl(connection, ssl);
      lm_ssl_unref(ssl);
    }

    LmMessageHandler* handler = lm_message_handler_new(onXmppMessage, self, NULL);
    lm_connection_register_message_handler(
      connection, handler,
      LM_MESSAGE_TYPE_MESSAGE, LM_HANDLER_PRIORITY_NORMAL
    );
    lm_message_handler_unref(handler);

    lm_connection_set_disconnect_function(connection, onXmppDisconnect, NULL, NULL);
  }

  lm_connection_open(LMCONN(self), onXmppConnect, self, NULL, NULL);
  return self;
}
コード例 #11
0
ファイル: IoDate.c プロジェクト: bomma/io
IO_METHOD(IoDate, asString)
{
	/*doc Date asString(optionalFormatString)
	Returns a string representation of the receiver using the
receivers format. If the optionalFormatString argument is present, the
receiver's format is set to it first. Formatting is according to ANSI C
date formatting rules.
<p>
<pre>	
%a abbreviated weekday name (Sun, Mon, etc.)
%A full weekday name (Sunday, Monday, etc.)
%b abbreviated month name (Jan, Feb, etc.)
%B full month name (January, February, etc.)
%c full date and time string
%d day of the month as two-digit decimal integer (01-31)
%H hour as two-digit 24-hour clock decimal integer (00-23)
%I hour as two-digit 12-hour clock decimal integer (01-12)
%m month as a two-digit decimal integer (01-12)
%M minute as a two-digit decimal integer (00-59)
%p either "AM" or "PM"
%S second as a two-digit decimal integer (00-59)
%U number of week in the year as two-digit decimal integer (00-52)
with Sunday considered as first day of the week
%w weekday as one-digit decimal integer (0-6) with Sunday as 0
%W number of week in the year as two-digit decimal integer (00-52)
with Monday considered as first day of the week
%x full date string (no time); in the C locale, this is equivalent
to "%m/%d/%y".
%y year without century as two-digit decimal number (00-99)
%Y year with century as four-digit decimal number
%Z time zone name (e.g. EST);
null string if no time zone can be obtained
%% stands for '%' character in output string.
</pre>	
*/

	char *format = "%Y-%m-%d %H:%M:%S %Z";

	if (IoMessage_argCount(m) == 1)
	{
		format = CSTRING(IoMessage_locals_symbolArgAt_(m, locals, 0));
	}
	else
	{
		IoObject *f = IoObject_getSlot_(self, IOSYMBOL("format"));
		if (ISSEQ(f)) { format = CSTRING(f); }
	}

	{
		UArray *ba = Date_asString(DATA(self), format);
		return IoState_symbolWithUArray_copy_(IOSTATE, ba, 0);
	}
}
コード例 #12
0
ファイル: IoTokyoCabinetPrefixCursor.c プロジェクト: ADTSH/io
IoObject *IoTokyoCabinetPrefixCursor_next(IoObject *self, IoObject *locals, IoMessage *m)
{
	/*doc TokyoCabinetPrefixCursor next
	Move cursor to next record. Returns true if there is another key, 
	or false if there is no next record.
	*/

	IoSeq *prefix = IoObject_getSlot_(self, IOSYMBOL("prefix"));
	IOASSERT(ISSEQ(prefix), "prefix must be a sequence");
	IOASSERT(TokyoCabinetPrefixCursor(self), "invalid TokyoCabinetPrefixCursor");
	tcbdbcurnext(TokyoCabinetPrefixCursor(self));
	return IOBOOL(self, IoTokyoCabinetPrefixCursor_keyBeginsWithPrefix_(self, prefix));
}
コード例 #13
0
ファイル: IoFile.c プロジェクト: achoy/io
IO_METHOD(IoFile, reopen)
{
	/*doc File reopen(otherFile, mode)
	Reopens otherFile and redirects its stream to this file's path using mode.
	If mode is omitted, it is copied from otherFile.
	Returns self or raises a File exception on error.
	*/

	IoFile *otherFile;
	IoSeq *mode;

	DATA(self)->flags = IOFILE_FLAGS_NONE;

	IoMessage_assertArgCount_receiver_(m, 1, self);
	
	otherFile = IoMessage_locals_valueArgAt_(m, locals, 0);
	IOASSERT(ISFILE(otherFile), "arg must be a File");
	
	mode = IoMessage_locals_valueArgAt_(m, locals, 1);
	if(ISSEQ(mode))
	{
		DATA(self)->mode = IOREF(mode);
	}
	else
	{
		DATA(self)->mode = IOREF(IoSeq_newWithUArray_copy_(IOSTATE, (UArray *)DATA(DATA(otherFile)->mode), 1));
	}

	if (!DATA(self)->stream)
	{
		FILE *fp = freopen(UTF8CSTRING(DATA(self)->path), CSTRING(DATA(self)->mode), DATA(otherFile)->stream);

		if (fp)
		{
			DATA(self)->stream = fp;
		}
		else
		{
			printf("%i:%s\n", errno, strerror(errno));
			IoState_error_(IOSTATE, m, "unable to reopen to file '%s' with mode %s.", UTF8CSTRING(DATA(self)->path), CSTRING(DATA(self)->mode));
			fclose(fp);
		}
	}

	return self;
}
コード例 #14
0
ファイル: IoTokyoCabinetPrefixCursor.c プロジェクト: ADTSH/io
IoObject *IoTokyoCabinetPrefixCursor_first(IoObject *self, IoObject *locals, IoMessage *m)
{
	/*doc TokyoCabinetPrefixCursor first
	Move cursor to first record. Returns self
	*/

	IoSeq *prefix = IoObject_getSlot_(self, IOSYMBOL("prefix"));
	IOASSERT(ISSEQ(prefix), "prefix must be a sequence");
	IOASSERT(TokyoCabinetPrefixCursor(self), "invalid TokyoCabinetPrefixCursor");
	
	tcbdbcurjump(TokyoCabinetPrefixCursor(self), (const void *)IoSeq_rawBytes(prefix), (int)IoSeq_rawSizeInBytes(prefix));
	
	if(!IoTokyoCabinetPrefixCursor_keyBeginsWithPrefix_(self, prefix))
	{
		tcbdbcurnext(TokyoCabinetPrefixCursor(self));
	}
	
	return IOBOOL(self, IoTokyoCabinetPrefixCursor_keyBeginsWithPrefix_(self, prefix));
}
コード例 #15
0
ファイル: IoLoudmouth.c プロジェクト: ADTSH/io
IoObject *IoLoudmouth_setPresence(IoLoudmouth *self, IoObject *locals, IoMessage *m) {
  char *pres_c  = IoMessage_locals_cStringArgAt_(m, locals, 0);
  IoSeq *status = IoMessage_locals_valueArgAt_(m, locals, 1);
  int success   = 0;
  LmMessage *xmpp_msg = lm_message_new_with_sub_type(
    NULL,
    LM_MESSAGE_TYPE_PRESENCE,
    str2msg_subtype(pres_c)
  );

  if(ISSEQ(status))
    lm_message_node_add_child(xmpp_msg->node, "status", CSTRING(status));

  success = lm_connection_send(LMCONN(self), xmpp_msg, NULL);
  lm_message_unref(xmpp_msg);
  free(pres_c);

  return IOBOOL(self, success);
}
コード例 #16
0
ファイル: IoTokyoCabinetPrefixCursor.c プロジェクト: ADTSH/io
IoObject *IoTokyoCabinetPrefixCursor_last(IoObject *self, IoObject *locals, IoMessage *m)
{
	/*doc TokyoCabinetPrefixCursor last
	Move cursor to last record. Returns self
	*/

	IoSeq *prefix = IoObject_getSlot_(self, IOSYMBOL("prefix"));
	IOASSERT(ISSEQ(prefix), "prefix must be a sequence");
	IOASSERT(TokyoCabinetPrefixCursor(self), "invalid TokyoCabinetPrefixCursor");
	
	{
		UArray *p = UArray_clone(IoSeq_rawUArray(prefix));
		UArray_appendCString_(p, " "); // space preceeds .
		
		tcbdbcurjump(TokyoCabinetPrefixCursor(self), (const void *)UArray_bytes(p), (int)UArray_size(p));
		
		UArray_free(p);
	}
	
	return IOBOOL(self, IoTokyoCabinetPrefixCursor_keyBeginsWithPrefix_(self, prefix));
}
コード例 #17
0
ファイル: IoSeq_immutable.c プロジェクト: ADTSH/io
IO_METHOD(IoSeq, findSeqs)
{
	/*doc Sequence findSeqs(listOfSequences, optionalStartIndex)
	Returns an object with two slots - an \"index\" slot which contains 
	the first occurrence of any of the sequences in listOfSequences found 
	in the receiver after the startIndex, and a \"match\" slot, which 
	contains a reference to the matching sequence from listOfSequences. 
	If no startIndex is specified, the search starts at index 0. 
	nil is returned if no occurences are found. 
	*/

	IoList *others = IoMessage_locals_listArgAt_(m, locals, 0);
	List *delims = IoList_rawList(others);
	long f = 0;
	long firstIndex = -1;
	size_t match = 0;

	if (IoMessage_argCount(m) > 1)
	{
		f = IoMessage_locals_longArgAt_(m, locals, 1);
	}

	{
		size_t index;

		LIST_FOREACH(delims, i, s,
			if (!ISSEQ((IoSeq *)s))
			{
				IoState_error_(IOSTATE, m, "requires Sequences as arguments, not %ss", IoObject_name((IoSeq *)s));
			}

			index = UArray_find_from_(DATA(self), DATA(((IoSeq *)s)), f);

			if(index != -1 && (firstIndex == -1 || index < firstIndex)) 
			{ 
				firstIndex = (long)index; 
				match = i; 
			}
		);
	}
コード例 #18
0
ファイル: IoMemcached.c プロジェクト: ADTSH/io
// Serialize/Deserialize
char *IoMemcached_serialize(IoMemcached *self, IoObject *locals, IoObject *object, size_t *size, uint32_t *flags) {
	char *cvalue;

	if(ISSEQ(object)) {
		*flags = _FLAG_SEQUENCE;
		*size = IOSEQ_LENGTH(object);
		cvalue = (char *) malloc(*size);
		strncpy(cvalue, CSTRING(object), *size);
	}
	else if(ISNUMBER(object)) {
		*flags = _FLAG_NUMBER;
		double cnumber = IoNumber_asDouble(object);
		cvalue = (char *) malloc(128 * sizeof(char));
		*size = snprintf(cvalue, 127, "%.16f", cnumber);
	}
	else if(ISNIL(object)) {
		*flags = _FLAG_NIL;
		*size = 3;
		cvalue = (char *) malloc(3 * sizeof(char));
		strncpy(cvalue, "nil", 3);
	}
	else if(ISBOOL(object)) {
		*flags = _FLAG_BOOLEAN;
		*size = 1;
		cvalue = (char *) malloc(sizeof(char));
		if(object == IOSTATE->ioTrue)  strncpy(cvalue, "1", 1);
		if(object == IOSTATE->ioFalse) strncpy(cvalue, "0", 1);
	}
	else {
		*flags = _FLAG_OBJECT;
		IoMessage *serialize = IoMessage_newWithName_(IOSTATE, IOSYMBOL("serialized"));
		IoSeq *serialized = IoMessage_locals_performOn_(serialize, locals, object);
		*size = IOSEQ_LENGTH(serialized);
		cvalue = (char *) malloc(*size);
		strncpy(cvalue, CSTRING(serialized), *size);
	}

	return cvalue;
}
コード例 #19
0
ファイル: IoTokyoCabinetPrefixCursor.c プロジェクト: ADTSH/io
IoObject *IoTokyoCabinetPrefixCursor_jump(IoObject *self, IoObject *locals, IoMessage *m)
{
	/*doc TokyoCabinetPrefixCursor jump(key)
	Move cursor to record before key. Returns self
	*/
	
	IoSeq *key = IoMessage_locals_seqArgAt_(m, locals, 0);
	int result;
	UArray *p;
	
	IoSeq *prefix = IoObject_getSlot_(self, IOSYMBOL("prefix"));
	IOASSERT(ISSEQ(prefix), "prefix must be a sequence");

	p = UArray_clone(IoSeq_rawUArray(prefix));
	UArray_appendPath_(p, IoSeq_rawUArray(key));
	
	result = tcbdbcurjump(TokyoCabinetPrefixCursor(self), 
						  (const void *)UArray_bytes(p), 
						  (int)UArray_sizeInBytes(p));
	UArray_free(p);
	
	IOASSERT(TokyoCabinetPrefixCursor(self), "invalid TokyoCabinetPrefixCursor");
	return IOBOOL(self, result);
}
コード例 #20
0
ファイル: IoMySQL.c プロジェクト: jamesrburgess/io
IoObject* IoMySQL_query(IoObject* self, IoObject* locals, IoMessage* m)
{
    /*doc MySQL query(aQueryString)
    Perform a SQL query and return a list of results.
    <pre>
    db query("SELECT * FROM accounts") foreach(println)
    </pre>
    */

    IoObject * queryString = 0x0;
    bool useMap;

    MYSQL* conn = &DATA(self)->connection;
    MYSQL_RES* result;
    MYSQL_ROW row;
    MYSQL_FIELD* column;
    char** columnNames;
    unsigned c, colLength;
    unsigned long* colLengths;
    IoObject *list, *rowObject; //, *tmpObject;

    if(IoMessage_argCount(m) < 1 || !ISSEQ(queryString = IoMessage_locals_quickValueArgAt_(m, locals, 0)))
        IoState_error_(IOSTATE, m, "argument 0 to method 'query' must be a Sequence");

    useMap = IoMessage_argCount(m) > 1 && ISTRUE(IoMessage_locals_quickValueArgAt_(m, locals, 1));

    if(!DATA(self)->connected) //printf("not connected?\n");
        IoState_error_(IOSTATE, m, "not connected yet");

    if(mysql_real_query(conn, CSTRING(queryString), IOSEQ_LENGTH(queryString)))
        IoState_error_(IOSTATE, m, "query error(%d): %s", mysql_errno(&DATA(self)->connection), mysql_error(&DATA(self)->connection));

    if((result = mysql_store_result(conn)) && (colLength = mysql_num_fields(result))) {
        list = IoList_new(IOSTATE);

        if(useMap) {
            columnNames = (char**) malloc(colLength * sizeof(char*));
            for(c = 0; c < colLength && (column = mysql_fetch_field(result)); ++c)
                columnNames[c] = column->name;

            while((row = mysql_fetch_row(result)))
            {
                colLengths = mysql_fetch_lengths(result);
                rowObject = IoMap_new(IOSTATE);

                for(c = 0; c < colLength; ++c)
                    IoMap_rawAtPut(rowObject, IOSYMBOL(columnNames[c]), IOSEQ((unsigned char *)row[c], (size_t)colLengths[c]));

                IoList_rawAppend_(list, rowObject);
            }

            free(columnNames);
        }
        else
        {
            while((row = mysql_fetch_row(result))) {
                colLengths = mysql_fetch_lengths(result);
                rowObject = IoList_new(IOSTATE);

                for(c = 0; c < colLength; ++c)
                    IoList_rawAppend_(rowObject, IOSEQ((unsigned char *)row[c], (size_t)colLengths[c]));

                IoList_rawAppend_(list, rowObject);
            }
        }

        mysql_free_result(result);
        return list;
    }
    else
        return IONUMBER(mysql_affected_rows(conn));
}
コード例 #21
0
ファイル: IoSeq_immutable.c プロジェクト: ADTSH/io
IO_METHOD(IoSeq, between)
{
	/*doc Sequence betweenSeq(aSequence, anotherSequence)
	Returns a new Sequence containing the bytes between the
	occurrence of aSequence and anotherSequence in the receiver. 
	If aSequence is empty, this method is equivalent to beforeSeq(anotherSequence).
	If anotherSequence is nil, this method is equivalent to afterSeq(aSequence).
	nil is returned if no match is found.
	*/

	long start = 0;
	long end = 0;
	IoSeq *fromSeq, *toSeq;

	fromSeq = (IoSeq *)IoMessage_locals_valueArgAt_(m, locals, 0);

	if (ISSEQ(fromSeq))
	{
		if (IoSeq_rawSize(fromSeq) == 0)
		{
			start = 0;
		}
		else
		{
			start = UArray_find_from_(DATA(self), DATA(fromSeq), 0);

			if (start == -1)
			{
				//start = 0;
				return IONIL(self);
			}
			start += IoSeq_rawSize(fromSeq);
		}
	}
	else if (ISNIL(fromSeq))
	{
		start = 0;
	}
	else
	{
		IoState_error_(IOSTATE, m, "Nil or Sequence argument required for arg 0, not a %s",
					IoObject_name((IoObject *)fromSeq));
	}

	toSeq = (IoSeq *)IoMessage_locals_valueArgAt_(m, locals, 1);

	if (ISSEQ(toSeq))
	{
		end = UArray_find_from_(DATA(self), DATA(toSeq), start);
		//if (end == -1) start = UArray_size(DATA(self));
		if (end == -1) return IONIL(self);
	}
	else if (ISNIL(toSeq))
	{
		end = UArray_size(DATA(self));
	}
	else
	{
		IoState_error_(IOSTATE, m, "Nil or Sequence argument required for arg 1, not a %s",
					IoObject_name((IoObject *)toSeq));
	}

	{
		UArray *ba = UArray_slice(DATA(self), start, end);
		IoSeq *result = IoSeq_newWithUArray_copy_(IOSTATE, ba, 0);
		return result;
	}
}
コード例 #22
0
ファイル: IoSeq.c プロジェクト: asymmetric/io
int IoObject_isNotStringOrBuffer(IoSeq *self)
{
	return !(ISSEQ(self));
}
コード例 #23
0
ファイル: IoSeq.c プロジェクト: asymmetric/io
int IoObject_isStringOrBuffer(IoSeq *self)
{
	return ISSEQ(self);
}
コード例 #24
0
ファイル: IoSeq.c プロジェクト: asymmetric/io
int ISMUTABLESEQ(IoObject *self)
{
	return ISSEQ(self) && !(IoObject_isSymbol(self));
}