Beispiel #1
0
struct String read_line(struct Value * string){
    //TODO: make robust if line is >= size_t characters.

    // read line takes a string to display as prompt
    // but displays it without quotes
    string->type = TYPE_KEYWORD;
    print(*string);
    string->type = TYPE_STRING;

    struct String input = {
        .length = 0,
        .body = NULL
    };

    char c;
    while ((c = getchar()) != '\n' && c != EOF){
        if (c == 0) {
            ERROR("Null byte in input file");
        } else if(c != '\n'){
            RESIZE(input.body, input.length + 1);
            input.body[input.length] = c;
            input.length++;
        }
    }
    if (input.length == 0) {
        ERROR("Input closed");
    }
    RESIZE(input.body, input.length + 1);
    input.body[input.length] = 0;

    assert(string_is_sane(&input));

    return input;
}
Beispiel #2
0
static cff_Dict *parseDict(const uint8_t *data, const uint32_t len) {
	cff_Dict *dict;
	NEW(dict);
	uint32_t index = 0, advance;
	cff_Value val, stack[48];
	const uint8_t *temp = data;

	while (temp < data + len) {
		advance = cff_decodeCffToken(temp, &val);

		switch (val.t) {
			case cff_OPERATOR:
				RESIZE(dict->ents, dict->count + 1);
				dict->ents[dict->count].op = val.i;
				dict->ents[dict->count].cnt = index;
				NEW(dict->ents[dict->count].vals, index);
				memcpy(dict->ents[dict->count].vals, stack, sizeof(cff_Value) * index);
				dict->count++;
				index = 0;
				break;
			case cff_INTEGER:
			case cff_DOUBLE:
				stack[index++] = val;
				break;
		}

		temp += advance;
	}

	return dict;
}
Text_T File_reader_reader(const char *pathname, const char *filter)
{
	int c, i, max = BUFFSIZE;
	char *buf = ALLOC(max);
	Text_T doc;
	FILE *in;
 
	// debug
	Fmt_register('T', Text_fmt);
	Fmt_fprint(stderr, "%s\n", pathname);
	Fmt_fprint(stderr, "BUFFSIZE: %d\n", BUFFSIZE);

	if ((in = fopen(pathname, "r")) == NULL)
		err(1, "%s", pathname);

	for (c = fgetc(in), i = 0; c != EOF; c = fgetc(in), i++) {
		if (i == max) { 		// max buf
			RESIZE(buf, max <<= 1);
		}

		buf[i] = c;
	}

	doc = Text_put(buf);

	Fmt_fprint(stderr, "%T\n", &doc);

	return doc;
}
Beispiel #4
0
int create_db_conn (void)
{
	int i;

	/* allocate more slots if we need them */
	if (dbConnAlloc == dbConnUsed) {
		i = dbConnAlloc;
		dbConnAlloc += 10;
		if (!dbConnList) {
			dbConnList = CALLOCATE(dbConnAlloc, db_t, TAG_DB, "create_db_conn");
		} else {
			pthread_mutex_lock(db_mut);
			dbConnList = RESIZE(dbConnList, dbConnAlloc, db_t, TAG_DB, "create_db_conn");
			pthread_mutex_unlock(db_mut);
		}
		while (i < dbConnAlloc) {
			dbConnList[i++].flags = DB_FLAG_EMPTY;
		}
	}

	for (i = 0;  i < dbConnAlloc;  i++) {
		if (dbConnList[i].flags & DB_FLAG_EMPTY) {
			dbConnList[i].flags = 0;
			dbConnList[i].type = &no_db;
			dbConnUsed++;
			return i + 1;
		}
	}

	fatal("dbConnAlloc != dbConnUsed, but no empty slots");
}
Beispiel #5
0
/* Replace all occurences of ? in this string buffer with prefix[1..99] */
static int prepare(T S, char prefix) {
        int n, i;
        for (n = i = 0; S->buffer[i]; i++) if (S->buffer[i] == '?') n++;
        if (n > 99)
                THROW(SQLException, "Max 99 parameters are allowed in a prepared statement. Found %d parameters in statement", n);
        else if (n) {
                int j, xl;
                char x[3] = {prefix};
                int required = (n * 2) + S->used;
                if (required >= S->length) {
                        S->length = required;
                        RESIZE(S->buffer, S->length);
                }
                for (i = 0, j = 1; (j <= n); i++) {
                        if (S->buffer[i] == '?') {
                                if(j<10){xl=2;x[1]=j+'0';}else{xl=3;x[1]=(j/10)+'0';x[2]=(j%10)+'0';}
                                memmove(S->buffer + i + xl, S->buffer + i + 1, (S->used - (i + 1)));
                                memmove(S->buffer + i, x, xl);
                                S->used += xl - 1;
                                j++;
                        }
                }
                S->buffer[S->used] = 0;
        }
        return n;
}
Beispiel #6
0
void *
array_del(array_t array, void *element, cmp_fun_t cmp_fun)
{
    void **cell;
    void *result;
    int i;

    if (!array) goto not_found;
    i = array_search(array, element, cmp_fun);
    if (i < 0) goto not_found;

    cell = array->cell;
    result = cell[i];
    array->num_elements --;
    if (i < array->num_elements)
        memmove(&cell[i], &cell[i + 1],
                (array->num_elements - i) * sizeof(void *));
    if (array->num_elements < array->size / 4 && array->size > MIN_SIZE) {
        array_t tmp;
        tmp = realloc(array, RESIZE(array, array->size / 2));
        assert(tmp && tmp == array);
        assert(array->size >= MIN_SIZE);
    }
    return result;
 not_found:
    errno = EEXIST;
    return NULL;
}
Beispiel #7
0
Datei: heap.c Projekt: kcyeu/xcb
static int grow_heap(heap_t heap) {
	heap->avail = (heap->avail << 1) + 1;
	if (RESIZE(heap->h, heap->avail * sizeof *heap->h) == NULL) {
		heap->curr = heap->avail = 0;
		return -1;
	}
	return 0;
}
Beispiel #8
0
void* processEvent(VM* vm, int r, SDL_Event * e) {
  VAL idris_event;

  SDL_Event event = *e;
  idris_requireAlloc(128); // Conservative!


  if (r==0) {
    idris_constructor(idris_event, vm, 0, 0, 0); // Nothing
  }
  else {
    VAL ievent = NULL;
    switch(event.type) {
    case SDL_KEYDOWN:
      ievent = KEY(vm, 0, event.key.keysym.sym);
      break;
    case SDL_KEYUP:
      ievent = KEY(vm, 1, event.key.keysym.sym);
      break;
    case SDL_MOUSEMOTION:
      ievent = MOTION(vm, event.motion.x, event.motion.y,
		      event.motion.xrel, event.motion.yrel);
      break;
    case SDL_MOUSEBUTTONDOWN:
      ievent = BUTTON(vm, 3, event.button.button, event.button.x, event.button.y);
      break;
    case SDL_MOUSEBUTTONUP:
      ievent = BUTTON(vm, 4, event.button.button, event.button.x, event.button.y);
      break;
    case SDL_MOUSEWHEEL:
      idris_constructor(ievent, vm, 5, 1, 0);
      idris_setConArg(ievent, 0, MKINT((intptr_t) event.wheel.y));
      break;
    case SDL_WINDOWEVENT:
      switch(event.window.event) {
      case SDL_WINDOWEVENT_RESIZED:
	ievent = RESIZE(vm, event.window.data1, event.window.data2);
	break;
      default:
	// TODO: other window event
	idris_constructor(ievent, vm, 8, 0, 0);
      }
      break;
    case SDL_QUIT:
      idris_constructor(ievent, vm, 7, 0, 0);
      break;
    default:
      idris_constructor(idris_event, vm, 0, 0, 0); // Nothing
      idris_doneAlloc(vm);
      return idris_event;
    }
    idris_constructor(idris_event, vm, 1, 1, 0);
    idris_setConArg(idris_event, 0, ievent); // Just ievent
  }
  
  idris_doneAlloc(vm);
  return idris_event;
}
Beispiel #9
0
char *fmt_vstring(const char *fmt, va_list ap){
	struct buf cl;
	assert(fmt);
	cl.size = 256;
	cl.buf = cl.bp = ALLOC(cl.size);
	fmt_vfmt(append, &cl, fmt, ap);
	append(0, &cl);
	return RESIZE(cl.buf, cl.bp-cl.buf);
}
Beispiel #10
0
void Fire::SetNumParts(int num)
{
	size=10;
	life=2000;
	radius=5;
	CLEAN(particles);
	RESIZE(particles,Particle,num,0);
	numParticles=num;
	ZeroMemory(particles,sizeof(Particle)*numParticles);
}
Beispiel #11
0
static int append(int c, void *cl){
	struct buf *p=cl;
	if(p->bp >= p->buf + p->size){
		RESIZE(p->buf, 2*p->size);
		p->bp = p->buf + p->size;
		p->size *= 2;
	}
	*p->bp++=c;
	return c;
}
Beispiel #12
0
csRef<iImage> csImageManipulate::Rescale2D (iImage* source, int newwidth, 
  int newheight)
{
  const int Width = source->GetWidth ();
  const int Height = source->GetHeight ();

  if (newwidth == Width && newheight == Height)
    return source;

  // This is a quick and dirty algorithm and it doesn't do funny things
  // such as blending multiple pixels together or bilinear filtering,
  // just a rough scale. It could be improved by someone in the future.

  unsigned int x, y;
  unsigned int dx = csQfixed16 (float (Width) / float (newwidth));
  unsigned int dy = csQfixed16 (float (Height) / float (newheight));

#define RESIZE(pt, Source, Dest)			\
  {							\
    const pt* field = (pt*)Source;			\
    pt* dst = (pt*)Dest;				\
    y = 0;						\
    int ny, nx;						\
    for (ny = newheight; ny; ny--)			\
    {							\
      const pt* src = field + (y >> 16) * Width;	\
      y += dy; x = 0;					\
      for (nx = newwidth; nx; nx--)			\
      {							\
        *dst++ = src [x >> 16];				\
        x += dx;					\
      }							\
    }							\
  }

  csImageMemory* newImg = new csImageMemory (newwidth, newheight,
    source->GetFormat ());
  newImg->SetImageType (source->GetImageType ());

  switch (source->GetFormat () & CS_IMGFMT_MASK)
  {
    case CS_IMGFMT_TRUECOLOR:
      RESIZE (csRGBpixel, source->GetImageData (), newImg->GetImagePtr ())
    break;
    case CS_IMGFMT_PALETTED8:
      RESIZE (uint8, source->GetImageData (), newImg->GetImagePtr ())
    break;
  }
  if (source->GetAlpha ())
    RESIZE (uint8, source->GetAlpha (), newImg->GetAlphaPtr ())

  csRef<iImage> imageRef;
  imageRef.AttachNew (newImg);
  return imageRef;
}
Beispiel #13
0
static bk_GraphNode *_bkgraph_grow(bk_Graph *f) {
	if (f->free) {
		f->length++;
		f->free--;
	} else {
		f->length = f->length + 1;
		f->free = (f->length >> 1) & 0xFFFFFF;
		RESIZE(f->entries, f->length + f->free);
	}
	return &(f->entries[f->length - 1]);
}
Beispiel #14
0
static inline void ensureCapacity(T R, int i) {
        if ((R->columns[i].real_length > R->bind[i].buffer_length)) {
                /* Column was truncated, resize and fetch column directly. */
                RESIZE(R->columns[i].buffer, R->columns[i].real_length + 1);
                R->bind[i].buffer = R->columns[i].buffer;
                R->bind[i].buffer_length = R->columns[i].real_length;
                if ((R->lastError = mysql_stmt_fetch_column(R->stmt, &R->bind[i], i, 0)))
                        THROW(SQLException, "mysql_stmt_fetch_column -- %s", mysql_stmt_error(R->stmt));
                R->needRebind = true;
        }
}
Beispiel #15
0
dstr dstr_remove_avail(dstr ds) {
	struct dshdr *dh;

	if (ds == NULL)
		return NULL;
	dh = (struct dshdr *)(ds - sizeof *dh);
	if (RESIZE(dh, sizeof *dh + dh->len + 1) == NULL)
		return NULL;
	dh->avail = 0;
	return dh->buf;
}
Beispiel #16
0
void array_resize(array_t array, int length){
	assert(array);
	assert(length>=0);
	if(length==0)
		FREE(array->array);
	else if(array->length==0)
		array->array=ALLOC(length*array->size);
	else
		RESIZE(array->array, length*array->size);
	array->length=length;
}
/**
 * Connects child and parent in a process tree
 * @param pt process tree
 * @param parent index
 * @param child index
 * @return TRUE if succeeded otherwise FALSE.
 */
int connectchild(ProcessTree_T *pt, int parent, int child) {
        
        ASSERT(pt);
        
        if (pt[parent].pid == pt[child].pid)
                return FALSE;
        RESIZE(pt[parent].children, sizeof(ProcessTree_T *) * (pt[parent].children_num + 1));
        pt[parent].children[pt[parent].children_num] = child;
        pt[parent].children_num++;
        
        return TRUE;
}
Beispiel #18
0
void Array_resize(T array, size_t length)
{
	assert(array);
	//assert(length >= 0);
	if (length == 0) {
		FREE(array->array);
	} else if (array->length == 0) {
		array->array = ALLOC(length*array->size);
	} else {
		RESIZE(array->array, length*array->size);
	}
	array->length = length;
}
Beispiel #19
0
int
array_add(array_t *array, void *element, cmp_fun_t cmp_fun)
{
    array_t arr = *array;
    void **cell;
    int i;

    if (UNLIKELY(!arr)) {
        arr = malloc(RESIZE(arr, MIN_SIZE));
        if (!arr) return -1;
        arr->size = MIN_SIZE;
        arr->num_elements = 0;
        *array = arr;
    } else if (arr->size <= arr->num_elements) {
        array_t tmp;
        int size = arr ? 2 * arr->size : MIN_SIZE;
        tmp = realloc(arr, RESIZE(arr, size));
        if (!tmp) return -1;
        *array = tmp;
        arr = tmp;
        tmp->size = size;
    }

    i = array_search(arr, element, cmp_fun);
    if (i >= 0) {
        errno = EEXIST;
        return -1;
    }

    i = -i - 1;
    cell = arr->cell;
    assert(0 <= i && i <= arr->num_elements && i < arr->size);
    if (i < arr->num_elements)
        memmove(&cell[i + 1], &cell[i],
                (arr->num_elements - i) * sizeof(void *));
    cell[i] = element;
    arr->num_elements++;
    return 0;
}
static inline void append(T S, const char *s, va_list ap) {
        va_list ap_copy;
        while (true) {
                va_copy(ap_copy, ap);
                int n = vsnprintf((char*)(S->buffer + S->used), S->length - S->used, s, ap_copy);
                va_end(ap_copy);
                if ((S->used + n) < S->length) {
                        S->used += n;
                        break;
                }
                S->length += STRLEN + n;
                RESIZE(S->buffer, S->length);
        }
}
/*
 * Get more LPC sockets structures if we run out
 */
static int more_lpc_sockets()
{
    int i;

    max_lpc_socks += 10;

    if (!lpc_socks)
        lpc_socks = CALLOCATE(10, lpc_socket_t, TAG_SOCKETS, "more_lpc_sockets");
    else
        lpc_socks = RESIZE(lpc_socks, max_lpc_socks, lpc_socket_t, TAG_SOCKETS, "more_lpc_sockets");

    i = max_lpc_socks;
    while (--i >= max_lpc_socks - 10)
        clear_socket(i, 0);

    return max_lpc_socks - 10;
}
Beispiel #22
0
dstr *dstr_split_len(const char *str, size_t length, const char *sep, size_t seplength, int *count) {
	int slots = 5, i, nelems = 0, start = 0;
	dstr *tokens;

	/* FIXME */
	if (str == NULL || sep == NULL)
		return NULL;
	if (length < 0 || seplength < 1)
		return NULL;
	if ((tokens = ALLOC(slots * sizeof *tokens)) == NULL)
		return NULL;
	if (length == 0) {
		*count = 0;
		return tokens;
	}

	for (i = 0; i < length - seplength + 1; ++i) {
		/* make sure there is room for the next and final ones */
		if (slots < nelems + 2) {
			slots *= 2;
			if (RESIZE(tokens, slots * sizeof *tokens) == NULL)
				goto err;
		}
		if ((seplength == 1 && str[i] == sep[0]) || !memcmp(str + i, sep, seplength)) {
			if ((tokens[nelems] = dstr_new_len(str + start, i - start)) == NULL)
				goto err;
			++nelems;
			start = i + seplength;
			i += seplength - 1;
		}
	}
	/* add the final one */
	if ((tokens[nelems] = dstr_new_len(str + start, length - start)) == NULL)
		goto err;
	++nelems;
	*count = nelems;
	return tokens;

err:
	for (i = 0; i < nelems; ++i)
		dstr_free(tokens[i]);
	FREE(tokens);
	*count = 0;
	return NULL;
}
int StringBuffer_replace(T S, const char *a, const char *b) {
        int n = 0;
        assert(S);
        if (a && b && *a) {
                register int i, j;
                for (i = 0; S->buffer[i]; i++) {
                        if (S->buffer[i] == *a) {
                                j = 0;
                                do
                                        if (! a[++j]) {n++; break;}
                                while (S->buffer[i + j] == a[j]);
                        }
                }
                if (n) {
                        int m = n;
                        size_t bl = strlen(b);
                        size_t diff = bl - strlen(a);
                        if (diff > 0) {
                                size_t required = (diff * n) + S->used + 1;
                                if (required >= S->length) {
                                        S->length = (int)required;
                                        RESIZE(S->buffer, S->length);
                                }
                        }
                        for (i = 0; m; i++) {
                                if (S->buffer[i] == *a) {
                                        j = 0;
                                        do
                                                if (! a[++j]) {
                                                        memmove(S->buffer + i + bl, S->buffer + i + j, (S->used - (i + j)));
                                                        memcpy(S->buffer + i, b, bl); 
                                                        S->used += diff;
                                                        i += bl - 1;
                                                        m--;
                                                        break;
                                                }
                                        while (S->buffer[i + j] == a[j]);
                                }
                        }
                        S->buffer[S->used] = 0;
                }
        }
        return n;
}
Beispiel #24
0
char *Str_vcat(const char *s, va_list ap) {
        char *t = NULL;
        if (s) {
                int n = 0;
                va_list ap_copy;
                int size = STRLEN;
                t = ALLOC(size);
                while (true) {
                        va_copy(ap_copy, ap);
                        n = vsnprintf(t, size, s, ap_copy);
                        va_end(ap_copy);
                        if (n < size)
                                break;
                        size = n + 1;
                        RESIZE(t, size);
                }
        }
        return t;
}
Beispiel #25
0
static void bufbeforewrite(caryll_Buffer *buf, size_t towrite) {
	size_t currentSize = buf->size;
	size_t allocated = buf->size + buf->free;
	size_t required = buf->cursor + towrite;
	if (required < currentSize) {
		// Completely overlap.
		return;
	} else if (required <= allocated) {
		// Within range without reallocation
		buf->size = required;
		buf->free = allocated - buf->size;
	} else {
		// Needs realloc
		buf->size = required;
		buf->free = required; // Double growth
		if (buf->free > 0x1000000) { buf->free = 0x1000000; }
		RESIZE(buf->data, buf->size + buf->free);
	}
}
Beispiel #26
0
char *Str_vcat(const char *s, va_list ap) {
        char *buf = NULL;
        if (s) {
                int n = 0;
                va_list ap_copy;
                int size = 88;
                buf = ALLOC(size);
                while (true) {
                        va_copy(ap_copy, ap);
                        n = vsnprintf(buf, size, s, ap_copy);
                        va_end(ap_copy);
                        if (n < size)
                                break;
                        size = n + 1;
                        RESIZE(buf, size);
                }
        }
        return buf;
}
Beispiel #27
0
dstr dstr_make_room(dstr ds, size_t length) {
	struct dshdr *dh;
	size_t newlen;

	if (ds == NULL)
		return NULL;
	dh = (struct dshdr *)(ds - sizeof *dh);
	if (dh->avail >= length)
		return ds;
	newlen = dh->len + length;
	if (newlen < DSTR_MAX_PREALLOC)
		newlen *= 2;
	else
		newlen += DSTR_MAX_PREALLOC;
	if (RESIZE(dh, sizeof *dh + newlen + 1) == NULL)
		return NULL;
	dh->avail = newlen - dh->len;
	return dh->buf;
}
Beispiel #28
0
int
read_file(FILE *help_file, int helpfunc)
{
	char line[BIG_BUFFER_SIZE + 1];
	int item_number = 0;
	int topic = -1;
	struct chelp_index *index = helpfunc ? &script_help : &bitchx_help;

	free_index(index);

	while (fgets(line, sizeof line, help_file)) {
		size_t len = strlen(line);

		if (line[len - 1] == '\n')
			line[len - 1] = '\0';

		if (!*line || *line == '#')
			continue;

		if (*line != ' ') {     /* we got a topic copy to topic */
			if (!my_strnicmp(line, "-RELATED", 7)) {
				if (topic > -1) {
					index->entries[topic].relates = m_strdup(line + 8);
				}
			} else {
				topic++;
				item_number = 0;
				RESIZE(index->entries, index->entries[0], topic + 1);

				index->entries[topic].title = m_strdup(line);
				index->entries[topic].contents = new_malloc(sizeof(char *));
				index->entries[topic].contents[0] = NULL;
				index->entries[topic].relates = NULL;
			}
		} else if (topic > -1) { /* we found the subject material */
			item_number++;
			RESIZE(index->entries[topic].contents, char *, item_number + 1);

			index->entries[topic].contents[item_number - 1] = m_strdup(line);
			index->entries[topic].contents[item_number] = NULL;
		}
	}
/**
 * Read all processes to initialize the process tree
 * @param reference  reference of ProcessTree
 * @return treesize>0 if succeeded otherwise 0.
 */
int initprocesstree_sysdep(ProcessTree_T ** reference) {
        int            i;
        int            treesize;
        ProcessTree_T *pt;
        
        ASSERT(reference);
        
        pstat_getdynamic(&pst_dyn, sizeof(struct pst_dynamic), 1, 0);
        nproc = pst_dyn.psd_activeprocs;
        
        if (nproc)
                RESIZE(psall, nproc * sizeof(struct pst_status));
        else 
                return 0;
        
        if ((treesize = pstat_getproc(psall, sizeof(struct pst_status), nproc , 0)) == -1) {
                LogError("system statistic error 1 -- pstat_getproc failed: %s\n", strerror(errno));
                return 0;
        }
        
        pt = CALLOC(sizeof(ProcessTree_T), treesize);
        
        for (i = 0; i < treesize; i++) {
                pt[i].pid         = psall[i].pst_pid;
                pt[i].ppid        = psall[i].pst_ppid;
                pt[i].starttime   = psall[i].pst_start;
                pt[i].time        = get_float_time();
                pt[i].cputime     =  psall[i].pst_utime + psall[i].pst_stime * 10;
                pt[i].cpu_percent = (int)(1000. * psall[i].pst_pctcpu / (float)systeminfo.cpus);
                pt[i].mem_kbyte   = (unsigned long)(psall[i].pst_rssize * (page_size / 1024.0));
                pt[i].cmdline     = (psall[i].pst_cmd && *psall[i].pst_cmd) ? Str_dup(psall[i].pst_cmd) : Str_dup(psall[i].pst_ucomm);
                
                if ( psall[i].pst_stat == PS_ZOMBIE )
                        pt[i].status_flag |= PROCESS_ZOMBIE;
        }
        
        *reference = pt;
        
        return treesize;
}
Beispiel #30
0
static void ringhelper_update (ringhelper *self, int nph, int mmax, double phi0)
  {
  int m;
  self->norot = (fabs(phi0)<1e-14);
  if (!(self->norot))
    if ((mmax!=self->s_shift-1) || (!FAPPROX(phi0,self->phi0_,1e-12)))
      {
      RESIZE (self->shiftarr,pshtd_cmplx,mmax+1);
      self->s_shift = mmax+1;
      self->phi0_ = phi0;
      for (m=0; m<=mmax; ++m)
        {
        self->shiftarr[m].re = cos(m*phi0);
        self->shiftarr[m].im = sin(m*phi0);
        }
      }
  if (!self->plan) self->plan=make_real_plan(nph);
  if (nph!=(int)self->plan->length)
    {
    kill_real_plan(self->plan);
    self->plan=make_real_plan(nph);
    }
  GROW(self->work,pshtd_cmplx,self->s_work,nph);
  }