void free_ops(struct cdbb *a) { int i; for (i = 0; i < a->tnum; i++) { if (a->ops[i].sv) free(a->ops[i].sv); array_reset(&a->ops[i].n); array_reset(&a->ops[i].v); } }
void article_reset(article_t* article) { stream_destroy(&article->stream); array_reset(article->stack); array_reset(article->words); array_reset(article->sentences); article->num_words = 0; }
int sv_stat_save(char *path, sv_stat_t *svst) { int fd; array_t new_path = ARRAY_INIT(1); int r; char status[SUPERVISE_STATUS_SIZE]; uint32_t tmp; /* Pack the svstat data into the status byte array. */ taia_pack(status, &svst->timestamp); tmp = svst->pid; ((uint32_t *)status)[3] = htobe32(tmp); status[16] = svst->paused; status[17] = svst->mode; /* Construct the path for the .new file into the array which *MUST* be freed * before returning. */ array_append(&new_path, path, strlen(path)); array_append(&new_path, SUPERVISE_STATUS_NEW_EXT, strlen(SUPERVISE_STATUS_NEW_EXT)); array_append_null(&new_path); if (array_failed(&new_path)) { array_reset(&new_path); errno = ENOMEM; return -1; } /* Open the tmp file, write the status byte array to it, verify the entire * array was written, and finally rename it to status. */ fd = open_trunc(array_start(&new_path)); if (fd == -1) { array_reset(&new_path); return ESVOPENSTATNEW; } r = write(fd, status, SUPERVISE_STATUS_SIZE); if ((r == -1) || (r != SUPERVISE_STATUS_SIZE)) { close(fd); array_reset(&new_path); return ESVWRITESTATNEW; } close(fd); if (rename(array_start(&new_path), path) == -1) { array_reset(&new_path); return ESVWRITESTAT; } array_reset(&new_path); return 0; }
static void data_array_reset(data_unset *d) { data_array *ds = (data_array *)d; /* reused array elements */ buffer_reset(ds->key); array_reset(ds->value); }
void array_free(ARRAY *self) { assert(self); array_reset(self); free(self); self = NULL; }
int pkgdb_loadfiles(struct pkgdb *db, struct pkg *pkg) { sqlite3_stmt *stmt; struct pkg_file *f; int ret; const char sql[] = "" "SELECT path, sha256 " "FROM files " "WHERE package_id = ?1 " "ORDER BY PATH ASC"; const char sqldir[] = "" "SELECT path " "FROM pkg_dirs " "WHERE origin = ?1 " "ORDER by path DESC"; if (pkg->type != PKG_INSTALLED) return (ERROR_BAD_ARG("pkg")); if (pkg->flags & PKG_LOAD_FILES) return (EPKG_OK); array_init(&pkg->files, 10); if (sqlite3_prepare_v2(db->sqlite, sql, -1, &stmt, NULL) != SQLITE_OK) return (ERROR_SQLITE(db->sqlite)); sqlite3_bind_int64(stmt, 1, pkg->rowid); while ((ret = sqlite3_step(stmt)) == SQLITE_ROW) { pkg_file_new(&f); strlcpy(f->path, sqlite3_column_text(stmt, 0), sizeof(f->path)); strlcpy(f->sha256, sqlite3_column_text(stmt, 1), sizeof(f->sha256)); array_append(&pkg->files, f); } sqlite3_finalize(stmt); if (ret != SQLITE_DONE) { array_reset(&pkg->files, &free); return (ERROR_SQLITE(db->sqlite)); } if (sqlite3_prepare_v2(db->sqlite, sqldir, -1, &stmt, NULL) != SQLITE_OK) return (ERROR_SQLITE(db->sqlite)); sqlite3_bind_text(stmt, 1, pkg_get(pkg, PKG_ORIGIN), -1, SQLITE_STATIC); while ((ret = sqlite3_step(stmt)) == SQLITE_ROW) { pkg_file_new(&f); strlcpy(f->path, sqlite3_column_text(stmt, 0), sizeof(f->path)); array_append(&pkg->files, f); } sqlite3_finalize(stmt); pkg->flags |= PKG_LOAD_FILES; return (EPKG_OK); }
static void data_config_reset(data_unset *d) { data_config *ds = (data_config *)d; /* reused array elements */ buffer_reset(ds->key); buffer_reset(ds->comp_key); array_reset(ds->value); }
/* Reset the array, fill it with the path arguments, and a null terminator. */ static int array_path(array_t *sa, char *path1, char *path2) { array_reset(sa); array_append(sa, path1, strlen(path1)); array_append(sa, "/", 1); array_append(sa, path2, strlen(path2)); array_append_null(sa); return array_failed(sa); }
static void http_senddata( const int64 sock, struct ot_workstruct *ws ) { struct http_data *cookie = io_getcookie( sock ); ssize_t written_size; if( !cookie ) { io_close(sock); return; } /* whoever sends data is not interested in its input-array */ if( ws->keep_alive && ws->header_size != ws->request_size ) { size_t rest = ws->request_size - ws->header_size; if( array_start(&cookie->request) ) { memmove( array_start(&cookie->request), ws->request + ws->header_size, rest ); array_truncate( &cookie->request, 1, rest ); } else array_catb(&cookie->request, ws->request + ws->header_size, rest ); } else array_reset( &cookie->request ); written_size = write( sock, ws->reply, ws->reply_size ); if( ( written_size < 0 ) || ( ( written_size == ws->reply_size ) && !ws->keep_alive ) ) { array_reset( &cookie->request ); free( cookie ); io_close( sock ); return; } if( written_size < ws->reply_size ) { char * outbuf; tai6464 t; if( !( outbuf = malloc( ws->reply_size - written_size ) ) ) { array_reset( &cookie->request ); free(cookie); io_close( sock ); return; } memcpy( outbuf, ws->reply + written_size, ws->reply_size - written_size ); iob_addbuf_free( &cookie->batch, outbuf, ws->reply_size - written_size ); /* writeable short data sockets just have a tcp timeout */ if( !ws->keep_alive ) { taia_uint( &t, 0 ); io_timeout( sock, t ); io_dontwantread( sock ); } io_wantwrite( sock ); } }
int decode(FILE* f) { uint64_t node_index; int ret; int unknown_node = 0; while (1) { ret = bit_read(my_bitio_d, actual_bits_counter, &node_index); if (ret<0) { fprintf(stderr, "Error in bit_read()\n"); return -1; } //EOF symbol reached if (node_index == 0) break; if (array_elem_counter < dictionary_size-1) { //there is some space avialable in the tree ret = array_add(node_index, -1); //browse the tree ret = find_path(node_index, unknown_node, f); if (ret == -1) { return -1; } unknown_node = 1; } else { //the tree is full ++array_elem_counter; //emit the last sequence of char ret = find_path(node_index, unknown_node, f); if (ret == -1) { return -1; } //reset the tree array_reset(); //at the next iteration we have not a node with an unspecified char unknown_node = 0; } } return 0; }
ssize_t http_sendiovecdata( const int64 sock, struct ot_workstruct *ws, int iovec_entries, struct iovec *iovector ) { struct http_data *cookie = io_getcookie( sock ); char *header; int i; size_t header_size, size = iovec_length( &iovec_entries, &iovector ); tai6464 t; /* No cookie? Bad socket. Leave. */ if( !cookie ) { iovec_free( &iovec_entries, &iovector ); HTTPERROR_500; } /* If this socket collected request in a buffer, free it now */ array_reset( &cookie->request ); /* If we came here, wait for the answer is over */ cookie->flag &= ~STRUCT_HTTP_FLAG_WAITINGFORTASK; /* Our answers never are 0 vectors. Return an error. */ if( !iovec_entries ) { HTTPERROR_500; } /* Prepare space for http header */ header = malloc( SUCCESS_HTTP_HEADER_LENGTH + SUCCESS_HTTP_HEADER_LENGTH_CONTENT_ENCODING ); if( !header ) { iovec_free( &iovec_entries, &iovector ); HTTPERROR_500; } if( cookie->flag & STRUCT_HTTP_FLAG_GZIP ) header_size = sprintf( header, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\nContent-Encoding: gzip\r\nContent-Length: %zd\r\n\r\n", size ); else if( cookie->flag & STRUCT_HTTP_FLAG_BZIP2 ) header_size = sprintf( header, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\nContent-Encoding: bzip2\r\nContent-Length: %zd\r\n\r\n", size ); else header_size = sprintf( header, "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\nContent-Length: %zd\r\n\r\n", size ); iob_reset( &cookie->batch ); iob_addbuf_free( &cookie->batch, header, header_size ); /* Will move to ot_iovec.c */ for( i=0; i<iovec_entries; ++i ) iob_addbuf_munmap( &cookie->batch, iovector[i].iov_base, iovector[i].iov_len ); free( iovector ); /* writeable sockets timeout after 10 minutes */ taia_now( &t ); taia_addsec( &t, &t, OT_CLIENT_TIMEOUT_SEND ); io_timeout( sock, t ); io_dontwantread( sock ); io_wantwrite( sock ); return 0; }
void array_free(struct array *a, void (*free_elm)(void*)) { if (a->data == NULL) return; array_reset(a, free_elm); free(a->data); a->data = NULL; a->cap = 0; }
int sv_stat(sv_stat_t *svst, char *path) { array_t full_path = ARRAY_INIT(1); int ok_fd; struct stat st; int status_fd; int32_t tmp; /* Check if 'down' exist indicating that the service isn't started when * supervise starts up. */ if (array_path(&full_path, path, "down")) { array_reset(&full_path); errno = ENOMEM; return -1; } if (stat(array_start(&full_path), &st) == -1) { if (errno != ENOENT) { array_reset(&full_path); return ESVSTATDOWN; } svst->autostart = 1; } else svst->autostart = 0; /* Attempt to open the supervise/ok fifo to see if supervise is actually * running. */ if (array_path(&full_path, path, SUPERVISE_OK_PATH)) { array_reset(&full_path); errno = ENOMEM; return -1; } ok_fd = open_write(array_start(&full_path)); if (ok_fd == -1) { array_reset(&full_path); if (errno == ENODEV) return ESVNOTRUN; return ESVOPENOK; } close(ok_fd); /* Load the contents of the supervise/status file. */ if (array_path(&full_path, path, SUPERVISE_STATUS_PATH)) { array_reset(&full_path); errno = ENOMEM; return -1; } if (sv_stat_load(svst, array_start(&full_path)) == -1) { array_reset(&full_path); return -1; } array_reset(&full_path); return 0; }
static void handle_dead( const int64 socket ) { struct http_data* h=io_getcookie( socket ); if( h ) { if( h->flag & STRUCT_HTTP_FLAG_IOB_USED ) iob_reset( &h->batch ); if( h->flag & STRUCT_HTTP_FLAG_ARRAY_USED ) array_reset( &h->request ); if( h->flag & STRUCT_HTTP_FLAG_WAITINGFORTASK ) mutex_workqueue_canceltask( socket ); free( h ); } io_close( socket ); }
void add_entry(struct cdbb *a, struct taia *k, char *v, size_t vs) { char pk[TAIA_PACK]; array dayidx; memset(&dayidx, 0, sizeof(array)); fmt_day_idx(&dayidx, k); taia_pack(pk, k); /* entry + idx */ cdbb_add(a, pk, TAIA_PACK, v, vs); cdbb_add(a, dayidx.p, array_bytes(&dayidx), pk, TAIA_PACK); blog_modified(a); array_reset(&dayidx); }
int pkgdb_loaddeps(struct pkgdb *db, struct pkg *pkg) { sqlite3_stmt *stmt; struct pkgdb_it it; struct pkg *p; int ret; const char sql[] = "" "SELECT p.rowid, p.origin, p.name, p.version, p.comment, p.desc, " "p.message, p.arch, p.osversion, p.maintainer, p.www, " "p.prefix, p.flatsize " "FROM packages AS p, deps AS d " "WHERE p.origin = d.origin " "AND d.package_id = ?1;"; if (pkg->type != PKG_INSTALLED) return (ERROR_BAD_ARG("pkg")); if (pkg->flags & PKG_LOAD_DEPS) return (EPKG_OK); array_init(&pkg->deps, 10); if (sqlite3_prepare_v2(db->sqlite, sql, -1, &stmt, NULL) != SQLITE_OK) return (ERROR_SQLITE(db->sqlite)); sqlite3_bind_int64(stmt, 1, pkg->rowid); it.stmt = stmt; it.db = db; p = NULL; while ((ret = pkgdb_it_next(&it, &p, PKG_LOAD_BASIC)) == EPKG_OK) { array_append(&pkg->deps, p); p = NULL; } sqlite3_finalize(stmt); if (ret != EPKG_END) { array_reset(&pkg->deps, &pkg_free_void); return (ret); } pkg->flags |= PKG_LOAD_DEPS; return (EPKG_OK); }
int sv_start_log(char *path) { array_t sa = ARRAY_INIT(1); char *path_log; int r; array_append(&sa, path, strlen(path)); if (path[strlen(path) - 1] != '/') array_append(&sa, "/", 1); array_append(&sa, "log", 3); array_append_null(&sa); path_log = array_start(&sa); r = sv_start(path_log); array_reset(&sa); return r; }
int sv_restart_log(char *path, int timeout) { array_t sa = ARRAY_INIT(1); char *path_log; int r; /* Create a dynamic storage array to contain the full path to the log * directory which MUST be freed before returning. */ array_append(&sa, path, strlen(path)); if (path[strlen(path) - 1] == '/') array_append(&sa, "/", 1); array_append(&sa, "log", 3); array_append_null(&sa); path_log = array_start(&sa); r = sv_restart(path_log, timeout); array_reset(&sa); return r; }
void array_setSize(ARRAY *self, size_t size) { assert(self); if (self->size) { array_reset(self); } /* get memory */ self->size = size; self->array = (char **)malloc((self->size + 1) * sizeof(char *)); if (!self->array) { message_error("unable to get memory"); } /* clear memory */ size_t i; for (i = 0; i <= self->size; ++i) { self->array[i] = NULL; } }
int pkgdb_loadscripts(struct pkgdb *db, struct pkg *pkg) { sqlite3_stmt *stmt; struct pkg_script *s; int ret; const char sql[] = "" "SELECT script, type " "FROM scripts " "WHERE package_id = ?1"; if (pkg->type != PKG_INSTALLED) return (ERROR_BAD_ARG("pkg")); if (pkg->flags & PKG_LOAD_SCRIPTS) return (EPKG_OK); array_init(&pkg->scripts, 6); if (sqlite3_prepare_v2(db->sqlite, sql, -1, &stmt, NULL) != SQLITE_OK) return (ERROR_SQLITE(db->sqlite)); sqlite3_bind_int64(stmt, 1, pkg->rowid); while ((ret = sqlite3_step(stmt)) == SQLITE_ROW) { pkg_script_new(&s); sbuf_set(&s->data, sqlite3_column_text(stmt, 0)); s->type = sqlite3_column_int(stmt, 1); array_append(&pkg->scripts, s); } sqlite3_finalize(stmt); if (ret != SQLITE_DONE) { array_reset(&pkg->scripts, &pkg_script_free_void); return (ERROR_SQLITE(db->sqlite)); } pkg->flags |= PKG_LOAD_SCRIPTS; return (EPKG_OK); }
int pkgdb_loadconflicts(struct pkgdb *db, struct pkg *pkg) { sqlite3_stmt *stmt; struct pkg_conflict *c; int ret; const char sql[] = "" "SELECT name " "FROM conflicts " "WHERE package_id = ?1;"; if (pkg->type != PKG_INSTALLED) return (ERROR_BAD_ARG("pkg")); if (pkg->flags & PKG_LOAD_CONFLICTS) return (EPKG_OK); array_init(&pkg->conflicts, 5); if (sqlite3_prepare_v2(db->sqlite, sql, -1, &stmt, NULL) != SQLITE_OK) return (ERROR_SQLITE(db->sqlite)); sqlite3_bind_int64(stmt, 1, pkg->rowid); while ((ret = sqlite3_step(stmt)) == SQLITE_ROW) { pkg_conflict_new(&c); sbuf_set(&c->glob, sqlite3_column_text(stmt, 0)); array_append(&pkg->conflicts, c); } sqlite3_finalize(stmt); if (ret != SQLITE_DONE) { array_reset(&pkg->conflicts, &pkg_conflict_free_void); return (ERROR_SQLITE(db->sqlite)); } pkg->flags |= PKG_LOAD_CONFLICTS; return (EPKG_OK); }
int pkgdb_loadoptions(struct pkgdb *db, struct pkg *pkg) { sqlite3_stmt *stmt; struct pkg_option *o; int ret; const char sql[] = "" "SELECT option, value " "FROM options " "WHERE package_id = ?1"; if (pkg->type != PKG_INSTALLED) return (ERROR_BAD_ARG("pkg")); if (pkg->flags & PKG_LOAD_OPTIONS) return (EPKG_OK); array_init(&pkg->options, 5); if (sqlite3_prepare_v2(db->sqlite, sql, -1, &stmt, NULL) != SQLITE_OK) return (ERROR_SQLITE(db->sqlite)); sqlite3_bind_int64(stmt, 1, pkg->rowid); while ((ret = sqlite3_step(stmt)) == SQLITE_ROW) { pkg_option_new(&o); sbuf_set(&o->opt, sqlite3_column_text(stmt, 0)); sbuf_set(&o->value, sqlite3_column_text(stmt, 1)); array_append(&pkg->options, o); } sqlite3_finalize(stmt); if (ret != SQLITE_DONE) { array_reset(&pkg->options, &pkg_option_free_void); return (ERROR_SQLITE(db->sqlite)); } pkg->flags |= PKG_LOAD_OPTIONS; return (EPKG_OK); }
static void connection_handle_errdoc_init(server *srv, connection *con) { /* modules that produce headers required with error response should * typically also produce an error document. Make an exception for * mod_auth WWW-Authenticate response header. */ buffer *www_auth = NULL; if (401 == con->http_status) { data_string *ds = (data_string *)array_get_element(con->response.headers, "WWW-Authenticate"); if (NULL != ds) { www_auth = buffer_init_buffer(ds->value); } } con->response.transfer_encoding = 0; buffer_reset(con->physical.path); array_reset(con->response.headers); chunkqueue_reset(con->write_queue); if (NULL != www_auth) { response_header_insert(srv, con, CONST_STR_LEN("WWW-Authenticate"), CONST_BUF_LEN(www_auth)); buffer_free(www_auth); } }
int main() { array_t array = ARRAY_INIT(1); char buf[FMT_ULONG_MAX + 1]; int r; for (;;) { array_reset(&array); r = bio_get_line(&array, bio_0); if (r <= 0) break; buf[fmt_ulong(buf, r)] = 0; array_append_null(&array); bio_put_str(bio_1, buf); bio_put_str(bio_1, " - "); bio_put_str(bio_1, array_start(&array)); bio_put_str(bio_1, "\n"); bio_flush(bio_1); } return 0; }
void array_loadFromString(ARRAY *self, const char *str, const char sep) { assert(self); array_reset(self); size_t i; size_t count; /* count tokens */ count = 0; for (i = 0; i < strlen(str); ++i) { if (str[i] == sep) ++count; } ++count; /* init memory */ array_setSize(self, count); /* separate & copy tokens */ int start = 0; int stop; count = 0; char *tok; for (i = 0; i < strlen(str) + 1; ++i) { if (str[i] == sep || str[i] == '\0') { stop = i; tok = (char *)malloc(stop - start + 1); memcpy(tok, str + start, stop - start); tok[stop - start] = '\0'; self->array[count++] = strdup(tok); free(tok); start = stop + 1; } } self->array[count] = NULL; }
GLOBAL struct source_desc * var_random_srcmodule(transform_info_ptr tinfo, char **args) { int n_harmonics, n_noisedips, i, n_dipoles; char **inargs=args; DATATYPE *memptr; float random_pos=0.0, random_moment=0.0; double freq_default=0.1; struct source_desc *sourcep; struct dipole_desc *dipolep; if (args==(char **)NULL) { ERREXIT(tinfo->emethods, "var_random_srcmodule: Usage: var_random_dipoles ( n_harmonics n_noisedips )\n"); } if (*inargs!=NULL) n_harmonics=atoi(*inargs++); if (*inargs!=NULL) n_noisedips=atoi(*inargs++); if (inargs-args!=2) { ERREXIT(tinfo->emethods, "var_random_srcmodule: Need at least two arguments ( n_harmonics n_noisedips )\n"); } n_dipoles=n_harmonics+n_noisedips; if ((sourcep=(struct source_desc *)malloc(sizeof(struct source_desc)+(sizeof(struct dipole_desc)+3*3*sizeof(DATATYPE))*n_dipoles))==NULL) { ERREXIT(tinfo->emethods, "var_random_srcmodule: Error allocating memory\n"); } sourcep->nrofsources=n_dipoles; dipolep=sourcep->dipoles=(struct dipole_desc *)(sourcep+1); memptr=(DATATYPE *)(dipolep+n_dipoles); for (i=0; i<n_dipoles; i++, dipolep++, memptr+=9) { dipolep->position.start=memptr; dipolep->dip_moment.start=memptr+3; dipolep->initial_moment.start=memptr+6; /*{{{ Read switchable options: FREQ_DEFAULT, RANDOM_POS, RANDOM_MOMENT keyword*/ if (*inargs!=NULL && strcmp(*inargs, "FREQ_DEFAULT")==0) { if (*++inargs!=NULL) { freq_default=atof(*inargs); inargs++; } } if (*inargs!=NULL && strcmp(*inargs, "RANDOM_POS")==0) { if (*++inargs!=NULL) { random_pos=atof(*inargs); inargs++; } } if (*inargs!=NULL && strcmp(*inargs, "RANDOM_MOMENT")==0) { if (*++inargs!=NULL) { random_moment=atof(*inargs); inargs++; } } /*}}} */ dipolep->position.nr_of_elements=dipolep->dip_moment.nr_of_elements=dipolep->initial_moment.nr_of_elements=3; dipolep->position.nr_of_vectors=dipolep->dip_moment.nr_of_vectors=dipolep->initial_moment.nr_of_vectors=1; dipolep->position.element_skip=dipolep->dip_moment.element_skip=dipolep->initial_moment.element_skip=1; dipolep->position.vector_skip=dipolep->dip_moment.vector_skip=dipolep->initial_moment.vector_skip=3; array_setreadwrite(&dipolep->position); array_setreadwrite(&dipolep->dip_moment); array_setreadwrite(&dipolep->initial_moment); array_reset(&dipolep->position); array_reset(&dipolep->dip_moment); array_reset(&dipolep->initial_moment); /* The parameter is the frequency relative to half the sampling frequency: */ dipolep->time[1]=M_PI*readval(tinfo, &inargs, freq_default); if (dipolep->time[1]<=0) { ERREXIT1(tinfo->emethods, "var_random_srcmodule: Frequency==%d\n", MSGPARM(dipolep->time[1])); } if (random_pos>0.0) { float x, y, z; sphere_random(random_pos, &x, &y, &z); array_write(&dipolep->position, readval(tinfo, &inargs, x)); array_write(&dipolep->position, readval(tinfo, &inargs, y)); array_write(&dipolep->position, readval(tinfo, &inargs, z)); } else { array_write(&dipolep->position, readval(tinfo, &inargs, 1)); array_write(&dipolep->position, readval(tinfo, &inargs, 1)); array_write(&dipolep->position, readval(tinfo, &inargs, 7.5)); } if (random_moment>0.0) { float x, y, z; sphere_random(random_moment, &x, &y, &z); array_write(&dipolep->dip_moment, readval(tinfo, &inargs, x)); array_write(&dipolep->dip_moment, readval(tinfo, &inargs, y)); array_write(&dipolep->dip_moment, readval(tinfo, &inargs, z)); } else { array_write(&dipolep->dip_moment, readval(tinfo, &inargs, 1)); array_write(&dipolep->dip_moment, readval(tinfo, &inargs, 0)); array_write(&dipolep->dip_moment, readval(tinfo, &inargs, 0)); } if (i<n_harmonics) { /*{{{ Set harmonic behaviour*/ dipolep->time_function_init= &harmonic_time_init; dipolep->time_function= &harmonic_time; dipolep->time_function_exit= &harmonic_time_exit; /*}}} */ } else { /*{{{ Set noise behaviour*/ dipolep->time_function_init= &noise_time_init; dipolep->time_function= &noise_time; dipolep->time_function_exit= &noise_time_exit; /*}}} */ } } return sourcep; }
/*{{{ is_inside(array *index, array *inpoints, DATATYPE x, DATATYPE y) {*/ LOCAL int is_inside(array *index, array *inpoints, DATATYPE x, DATATYPE y) { array x1, x2, x3; DATATYPE x3data[2], d, x3dist, hold, xminline=0, yminline=0; DATATYPE dist, mindist= -1, xline, yline; /*{{{ Prepare x3: x3 is an array that holds x and y*/ x3data[0]=x; x3data[1]=y; x3.nr_of_elements=2; x3.nr_of_vectors=x3.element_skip=1; x3.start=x3data; array_setreadwrite(&x3); array_reset(&x3); /*}}} */ /*{{{ x3dist:=distance between x3 and (xmean, ymean)*/ hold=x-xmean; x3dist=hold*hold; hold=y-ymean; x3dist+=hold*hold; /*}}} */ /*{{{ Select qhull segment at minimum distance from x*/ array_reset(inpoints); array_reset(index); do { x1=x2= *inpoints; x1.current_vector=array_scan(index); x2.current_vector=array_scan(index); array_setto_vector(&x1); array_setto_vector(&x2); x1.nr_of_elements=x2.nr_of_elements=2; d=array_parameter_linedist(&x1, &x2, &x3); /* Let the end points participate in the vote as well */ if (d>1) d=1; if (d<0) d=0; xline=array_scan(&x1)*(1.0-d)+array_scan(&x2)*d; yline=array_scan(&x1)*(1.0-d)+array_scan(&x2)*d; hold=xline-array_scan(&x3); dist=hold*hold; hold=yline-array_scan(&x3); dist+=hold*hold; if (mindist<=0 || dist<mindist) { mindist=dist; xminline=xline; yminline=yline; } } while (index->message!=ARRAY_ENDOFSCAN); /*}}} */ /*{{{ dist:=(line-mean)^2*/ hold=xminline-xmean; dist=hold*hold; hold=yminline-ymean; dist+=hold*hold; /*}}} */ return (dist>x3dist); }
int connection_reset(server *srv, connection *con) { plugins_call_connection_reset(srv, con); connection_response_reset(srv, con); con->is_readable = 1; con->bytes_written = 0; con->bytes_written_cur_second = 0; con->bytes_read = 0; con->bytes_header = 0; con->loops_per_request = 0; con->request.http_method = HTTP_METHOD_UNSET; con->request.http_version = HTTP_VERSION_UNSET; con->request.http_if_modified_since = NULL; con->request.http_if_none_match = NULL; #define CLEAN(x) \ if (con->x) buffer_reset(con->x); CLEAN(request.uri); CLEAN(request.request_line); CLEAN(request.pathinfo); CLEAN(request.request); /* CLEAN(request.orig_uri); */ CLEAN(uri.scheme); /* CLEAN(uri.authority); */ /* CLEAN(uri.path); */ CLEAN(uri.path_raw); /* CLEAN(uri.query); */ CLEAN(parse_request); CLEAN(server_name); #if defined USE_OPENSSL && ! defined OPENSSL_NO_TLSEXT CLEAN(tlsext_server_name); #endif #undef CLEAN #define CLEAN(x) \ if (con->x) con->x->used = 0; #undef CLEAN #define CLEAN(x) \ con->request.x = NULL; CLEAN(http_host); CLEAN(http_range); CLEAN(http_content_type); #undef CLEAN con->request.content_length = 0; con->request.te_chunked = 0; array_reset(con->request.headers); array_reset(con->environment); chunkqueue_reset(con->request_content_queue); /* The cond_cache gets reset in response.c */ /* config_cond_cache_reset(srv, con); */ con->header_len = 0; con->error_handler_saved_status = 0; /*con->error_handler_saved_method = HTTP_METHOD_UNSET;*/ /*(error_handler_saved_method value is not valid unless error_handler_saved_status is set)*/ config_setup_connection(srv, con); return 0; }
int main(int argc, char **argv) { FILE *fp; surfspline_desc sspline; int err, npoints, itempart=0; int binary_output=FALSE, matlab_output=FALSE, mtv_output=FALSE, interpolate_only=FALSE; float fbuf, external_value=0.0; DATATYPE f, x, y, xmin, xmax, ymin, ymax, xstep, ystep; array index; pid_t pid; char outname[L_tmpnam], inname[L_tmpnam], **inargs, *infile; /*{{{ Read command line args*/ for (inargs=argv+1; inargs-argv<argc && **inargs=='-'; inargs++) { switch(inargs[0][1]) { case 'B': binary_output=TRUE; break; case 'I': if (inargs[0][2]!='\0') { itempart=atoi(*inargs+2); } break; case 'i': interpolate_only=TRUE; if (inargs[0][2]!='\0') { external_value=atof(*inargs+2); } break; case 'M': matlab_output=TRUE; break; case 'm': mtv_output=TRUE; break; default: fprintf(stderr, "%s: Ignoring unknown option %s\n", argv[0], *inargs); continue; } } if (argc-(inargs-argv)!=3) { fprintf(stderr, "Usage: %s array_filename degree npoints\n" "Options:\n" " -Iitem: Use item number item as z-axis (2+item'th column); default: 0\n" " -i[value]: Interpolate only; set external values to value (default: 0.0)\n" " -B: Binary output (gnuplot floats)\n" " -M: Matlab output (x, y vectors and z matrix)\n" " -m: Plotmtv output\n" , argv[0]); return -1; } infile= *inargs++; if ((fp=fopen(infile, "r"))==NULL) { fprintf(stderr, "Can't open %s\n", infile); return -2; } array_undump(fp, &sspline.inpoints); fclose(fp); if (sspline.inpoints.message==ARRAY_ERROR) { fprintf(stderr, "Error in array_undump\n"); return -3; } if (sspline.inpoints.nr_of_elements<3+itempart) { fprintf(stderr, "Not enough columns in array %s\n", *(inargs-1)); return -3; } sspline.degree=atoi(*inargs++);; npoints=atoi(*inargs++); /*}}} */ /*{{{ Get the index of qhull point pairs by running qhull*/ tmpnam(outname); tmpnam(inname); if ((pid=fork())==0) { /* I'm the child */ #define LINEBUF_SIZE 128 char linebuf[LINEBUF_SIZE]; array tmp_array; /*{{{ Dump xy positions to tmp file inname*/ tmp_array.nr_of_elements=2; tmp_array.nr_of_vectors=sspline.inpoints.nr_of_vectors; tmp_array.element_skip=1; if (array_allocate(&tmp_array)==NULL) { fprintf(stderr, "Error allocating tmp_array\n"); return -4; } array_reset(&sspline.inpoints); do { array_write(&tmp_array, array_scan(&sspline.inpoints)); array_write(&tmp_array, array_scan(&sspline.inpoints)); array_nextvector(&sspline.inpoints); } while (tmp_array.message!=ARRAY_ENDOFSCAN); if ((fp=fopen(inname, "w"))==NULL) { fprintf(stderr, "Can't open %s\n", inname); return -5; } array_dump(fp, &tmp_array, ARRAY_ASCII); fclose(fp); array_free(&tmp_array); /*}}} */ snprintf(linebuf, LINEBUF_SIZE, "qhull C0 i b <%s >%s", inname, outname); execl("/bin/sh", "sh", "-c", linebuf, 0); #undef LINEBUF_SIZE } else { wait(NULL); } unlink(inname); if ((fp=fopen(outname, "r"))==NULL) { fprintf(stderr, "Can't open %s\n", outname); return -6; } array_undump(fp, &index); fclose(fp); unlink(outname); /*}}} */ /*{{{ Find min, max and mean coordinates*/ array_transpose(&sspline.inpoints); array_reset(&sspline.inpoints); xmin=array_min(&sspline.inpoints); ymin=array_min(&sspline.inpoints); array_reset(&sspline.inpoints); xmax=array_max(&sspline.inpoints); ymax=array_max(&sspline.inpoints); array_reset(&sspline.inpoints); xmean=array_mean(&sspline.inpoints); ymean=array_mean(&sspline.inpoints); xstep=(xmax-xmin)/npoints; ystep=(ymax-ymin)/npoints; array_transpose(&sspline.inpoints); array_reset(&sspline.inpoints); /*}}} */ /*{{{ Copy itempart to 3rd location if necessary*/ /* Note: For this behavior it is essential that array_surfspline * will accept vectors of any size >=3 and only process the first three * elements ! */ if (itempart>0) { DATATYPE hold; do { sspline.inpoints.current_element=2+itempart; hold=READ_ELEMENT(&sspline.inpoints); sspline.inpoints.current_element=2; WRITE_ELEMENT(&sspline.inpoints, hold); array_nextvector(&sspline.inpoints); } while (sspline.inpoints.message!=ARRAY_ENDOFSCAN); } /*}}} */ /*{{{ Do surface spline*/ if ((err=array_surfspline(&sspline))!=0) { fprintf(stderr, "Error %d in array_surfspline\n", err); return err; } xmin-=xstep; xmax+=2*xstep; ymin-=ystep; ymax+=2*ystep; if (binary_output) { /*{{{ Gnuplot binary output*/ int n_xval=(xmax-xmin)/xstep+1, n; /* Number of x values */ fbuf=n_xval; fwrite(&fbuf, sizeof(float), 1, stdout); for (fbuf=xmin, n=0; n<n_xval; fbuf+=xstep, n++) { /* x values */ fwrite(&fbuf, sizeof(float), 1, stdout); } for (y=ymin; y<=ymax; y+=ystep) { fbuf=y; fwrite(&fbuf, sizeof(float), 1, stdout); for (x=xmin, n=0; n<n_xval; x+=xstep, n++) { if (interpolate_only && !is_inside(&index, &sspline.inpoints, x, y)) { f=external_value; } else { f=array_fsurfspline(&sspline, x, y); } fbuf=f; fwrite(&fbuf, sizeof(float), 1, stdout); } } /*}}} */ } else if (matlab_output) { /*{{{ Matlab output*/ array xm, ym, zm; xm.nr_of_elements=ym.nr_of_elements=1; xm.nr_of_vectors=zm.nr_of_elements=(xmax-xmin)/xstep+1; ym.nr_of_vectors=zm.nr_of_vectors=(ymax-ymin)/ystep+1; xm.element_skip=ym.element_skip=zm.element_skip=1; array_allocate(&xm); array_allocate(&ym); array_allocate(&zm); if (xm.message==ARRAY_ERROR || ym.message==ARRAY_ERROR || zm.message==ARRAY_ERROR) { fprintf(stderr, "Error allocating output arrays\n"); return -7; } x=xmin; do { array_write(&xm, x); x+=xstep; } while (xm.message!=ARRAY_ENDOFSCAN); y=ymin; do { array_write(&ym, y); x=xmin; do { if (interpolate_only && !is_inside(&index, &sspline.inpoints, x, y)) { f=external_value; } else { f=array_fsurfspline(&sspline, x, y); } array_write(&zm, f); x+=xstep; } while (zm.message==ARRAY_CONTINUE); y+=ystep; } while (zm.message!=ARRAY_ENDOFSCAN); array_dump(stdout, &xm, ARRAY_MATLAB); array_dump(stdout, &ym, ARRAY_MATLAB); array_dump(stdout, &zm, ARRAY_MATLAB); array_free(&xm); array_free(&ym); array_free(&zm); /*}}} */ } else if (mtv_output) { /*{{{ Plotmtv output*/ array zm; DATATYPE zmin=FLT_MAX, zmax= -FLT_MAX; zm.nr_of_elements=(xmax-xmin)/xstep+1; zm.nr_of_vectors=(ymax-ymin)/ystep+1; zm.element_skip=1; array_allocate(&zm); if (zm.message==ARRAY_ERROR) { fprintf(stderr, "Error allocating output arrays\n"); return -7; } y=ymin; do { x=xmin; do { if (interpolate_only && !is_inside(&index, &sspline.inpoints, x, y)) { f=external_value; } else { f=array_fsurfspline(&sspline, x, y); } array_write(&zm, f); if (f<zmin) zmin=f; if (f>zmax) zmax=f; x+=xstep; } while (zm.message==ARRAY_CONTINUE); y+=ystep; } while (zm.message!=ARRAY_ENDOFSCAN); /*{{{ Print file header*/ printf( "# Output of Spline_Gridder (C) Bernd Feige 1995\n\n" "$ DATA=CONTOUR\n\n" "%% toplabel = \"Spline_Gridder output\"\n" "%% subtitle = \"File: %s\"\n\n" "%% interp = 0\n" "%% contfill = on\n" "%% meshplot = off\n\n" "%% xmin = %g xmax = %g\n" "%% ymin = %g ymax = %g\n" "%% zmin = %g zmax = %g\n" "%% nx = %d\n" "%% ny = %d\n" , infile, xmin, xmax, ymin, ymax, zmin, zmax, zm.nr_of_elements, zm.nr_of_vectors); /*}}} */ array_dump(stdout, &zm, ARRAY_MATLAB); array_free(&zm); /*}}} */ } else { /*{{{ Gnuplot output*/ for (x=xmin; x<=xmax; x+=xstep) { for (y=ymin; y<=ymax; y+=ystep) { if (interpolate_only && !is_inside(&index, &sspline.inpoints, x, y)) { f=external_value; } else { f=array_fsurfspline(&sspline, x, y); } printf("%g %g %g\n", x, y, f); } printf("\n"); } /*}}} */ } /*}}} */ return 0; }
int http_response_finish_header(server *srv, connection *con) { if (NULL == srv || NULL == con || NULL == con -> write_queue) { return -1; } /* * 这个header有安全问题。通产默认不添加。 */ //http_response_insert_header(srv, con, CONST_STR_LEN("Server"), CONST_STR_LEN("Swiftd/0.1 written by hcy")); buffer *b = chunkqueue_get_prepend_buffer(con -> write_queue); if (NULL == b) { log_error_write(srv, __FILE__, __LINE__, "s", "chunkqueue_get_prepend_buffer failed."); return -1; } buffer_reset(b); /* * Status-Line: * HTTP-Version SP Status-Code SP Reason-Phrase CRLF */ buffer_copy_string_len(b, CONST_STR_LEN("HTTP/1.1")); buffer_append_string_len(b, CONST_STR_LEN(" ")); //SP buffer_append_long(b, con -> http_status); buffer_append_string_len(b, CONST_STR_LEN(" ")); //SP buffer_append_string(b, get_http_status_name(con -> http_status)); buffer_append_string_len(b, CONST_STR_LEN(CRLF)); //CRLF = '\r\n' defined in base.h if(con -> keep_alive) { http_response_insert_header(srv, con, CONST_STR_LEN("Connection") , CONST_STR_LEN("keep-alive")); } else { http_response_insert_header(srv, con, CONST_STR_LEN("Connection") , CONST_STR_LEN("close")); } /* * Headers: * Key:Value CRLF */ size_t i; data_string *ds; for (i = 0; i < con -> response.headers -> used; ++i) { ds = (data_string *)con -> response.headers -> data[i]; if (NULL == ds) { continue; } buffer_append_string_buffer(b, ds -> key); buffer_append_string_len(b, CONST_STR_LEN(": ")); buffer_append_string_buffer(b, ds -> value); buffer_append_string_len(b, CONST_STR_LEN(CRLF)); } /* * header 和 message body之间的CRLF。 */ buffer_append_string_len(b, CONST_STR_LEN(CRLF)); /* * 最后一个'\0'不能发送出去!! */ -- b -> used; log_error_write(srv, __FILE__, __LINE__, "sb", "Response Headers:", b); array_reset(con -> response.headers); return 0; }