/* convert a phpnum to a string. precision is irrelevant for longs. */ obj_t phpnum_to_string(obj_t a, int precision, int efg, int style) { int actual_length; #define ARB_STRING_SIZE 1024 char result[ARB_STRING_SIZE]; if (REALP(a)) { double dval = REAL_TO_DOUBLE(a); while (1) { switch (efg) { case 0: actual_length = pcc_snprintf(result, ARB_STRING_SIZE, E_FORMAT, precision, dval); break; case 1: actual_length = pcc_snprintf(result, ARB_STRING_SIZE, F_FORMAT, precision, dval); break; case 2: if (style == 0) { // echo actual_length = snprintf(result, ARB_STRING_SIZE, G_FORMAT, precision, dval); } else { // var_dump actual_length = pcc_snprintf(result, ARB_STRING_SIZE, G_FORMAT, precision, dval); } break; default: phpnum_fail("bad value for efg"); } /* this bit is from man snprintf. */ if (actual_length > -1 && actual_length < ARB_STRING_SIZE) return string_to_bstring_len(result, actual_length); if (actual_length > -1) /* glibc 2.1 */ phpnum_fail("Arbitrary constant not large enough"); else /* glibc 2.0 */ phpnum_fail("Arbitrary constant not large enough"); } } else { //long long lval = BELONG_TO_LONG(a); /* same game as above */ while (1) { actual_length = snprintf(result, ARB_STRING_SIZE, "%ld", lval); if (actual_length > -1 && actual_length < ARB_STRING_SIZE) return string_to_bstring_len(result, actual_length); if (actual_length > -1) phpnum_fail("Arbitrary constant not large enough"); else phpnum_fail("Arbitrary constant not large enough"); } } phpnum_fail("Reached end of phpnum_to_string unexpectedly."); }
/*---------------------------------------------------------------------*/ BGL_RUNTIME_DEF obj_t bgl_seconds_to_utc_string( long sec ) { struct tm *t; char *s; t = gmtime( (time_t *)&sec ); s = asctime( t ); return string_to_bstring_len( s, strlen( s ) - 1 ); }
/*---------------------------------------------------------------------*/ BGL_RUNTIME_DEF obj_t bgl_seconds_to_string( long sec ) { char *s; obj_t res; bgl_mutex_lock( date_mutex ); s = ctime( (time_t *)&sec ); res = string_to_bstring_len( s, strlen( s ) - 1 ); bgl_mutex_unlock( date_mutex ); return res; }
obj_t bigloo_recv(int fd, int maxlen) { char *str; int bytes_read; if (!(str = malloc(maxlen))) { perror("couldn't allocate memory in bigloo_recv"); exit(1); } bytes_read = recv(fd, str, maxlen, 0); if (bytes_read < 0) { free(str); return BFALSE; } { obj_t retval = string_to_bstring_len(str, bytes_read); free(str); return retval; } }
/*---------------------------------------------------------------------*/ obj_t bgl_seconds_format( long sec, obj_t fmt ) { char *buffer; struct tm *p; int len = (int)STRING_LENGTH( fmt ) + 256; buffer = (char *)GC_MALLOC_ATOMIC( len + 1 ); bgl_mutex_lock( date_mutex ); p = localtime( (time_t *)&sec ); bgl_mutex_unlock( date_mutex ); len = (int)strftime( buffer, len, BSTRING_TO_STRING( fmt ), p ); if( len > 0 ) return string_to_bstring_len( buffer, len ); else { C_FAILURE( "seconds-format", "buffer too short!", BINT( 256 ) ); return BUNSPEC; } }
obj_t php_fgets(FILE *stream, int limit) { /* return a bigloo string no bigger than limit, as read by fgets */ /* return scheme '() on EOF */ obj_t string; static char *buffer = NULL; int actually_read = 0; /* initialize internal buffer if we haven't yet XXX: this is a bona fide one time BUFFERSIZE memory leak. who cares? */ if (buffer == NULL) { if ((buffer = malloc( BUFFERSIZE )) == NULL) { /* out of memory */ return BNIL; } } if (limit <= BUFFERSIZE) { /* in this case we can use the statically allocated buffer that way we can avoid allocating a limit size string for potentially small line */ if (fngets( buffer, limit, stream ) == -1) { return BNIL; } actually_read = strlen( buffer ); string = string_to_bstring_len(buffer, actually_read); } else { /* don't use the static buffer, limit is too big. We use realloc because, at least on my linux/glibc2 system, it behaves really nicely. GC_REALLOC_ATOMIC behaves quadratically. */ char *bigbuf = NULL; int len; do { if ((bigbuf = realloc( bigbuf, actually_read + BUFFERSIZE )) == NULL) { /* out of memory */ return BNIL; } if (fngets( bigbuf + actually_read, MIN(BUFFERSIZE, limit), stream ) == -1) { /* fgets says there's nothing left to read */ if (actually_read > 0) { /* we have read something in previous iterations */ break; } else { /* we've read nothing in this iteration, the stream is at EOF */ free(bigbuf); return BNIL; } } /* this is how much we read this time around */ len = strlen( bigbuf + actually_read ); /* this is ho much we've read total */ actually_read += len; /* this is how much we've got left to read before hitting the limit */ limit -= len; } while (!((len < (BUFFERSIZE - 1)) || /* didn't fill the buffer, so must be finished */ /* filled the buffer exactly, so must be finished */ *(bigbuf + actually_read - 1) == '\n') /* bumped up against the user's limit */ && limit >= 0); string = string_to_bstring_len(bigbuf, actually_read); if (bigbuf) free(bigbuf); } return string; }