int parse_config_file(const char *fn) { FILE *f; char *line = NULL; size_t linelen = 0; char *name; char *val; struct opt *opt; struct header *hdr; int lineno = 1; unsigned h; f = fopen(fn, "r"); if (!f) return -1; hdr = NULL; while (getline(&line, &linelen, f) > 0) { char *s = strchr(line, '#'); if (s) *s = 0; s = strstrip(line); if (*s == '[') { char *p = strchr(s, ']'); if (p == NULL) parse_error(lineno, "Header without ending ]"); nothing(p + 1, lineno); *p = 0; hdr = new_header(hdr, s + 1); } else if ((val = strchr(line, '=')) != NULL) { *val++ = 0; name = strstrip(s); val = strstrip(val); opt = xalloc(sizeof(struct opt)); opt->name = xstrdup(name); opt->val = xstrdup(val); h = hash(name); if (!hdr) hdr = new_header(hdr, "global"); //printf("[%s] \"%s\" = \"%s\"\n", hdr->name, name, val); if (hdr->optslast[h] == NULL) hdr->opts[h] = opt; else hdr->optslast[h]->next = opt; hdr->optslast[h] = opt; } else if (!empty(s)) { parse_error(lineno, "config file line not field nor header"); } lineno++; } fclose(f); free(line); return 0; }
ID3v2_tag* new_tag() { ID3v2_tag* tag = (ID3v2_tag*) malloc(sizeof(ID3v2_tag)); tag->tag_header = new_header(); tag->frames = new_frame_list(); return tag; }
ID3v2_header* get_tag_header(const char* file_name) { char buffer[ID3_HEADER]; FILE* file = fopen(file_name, "rb"); if(file == NULL) { perror("Error opening file"); return NULL; } fread(buffer, ID3_HEADER, 1, file); fclose(file); ID3v2_header* tag_header = new_header(); if( ! _has_id3v2tag(buffer)) { if (tag_header != NULL) free (tag_header); return NULL; } int position = 0; memcpy(tag_header->tag, buffer, ID3_HEADER_TAG); tag_header->major_version = buffer[position += ID3_HEADER_TAG]; tag_header->minor_version = buffer[position += ID3_HEADER_VERSION]; tag_header->flags = buffer[position += ID3_HEADER_REVISION]; tag_header->tag_size = syncint_decode(btoi(buffer, ID3_HEADER_SIZE, position += ID3_HEADER_FLAGS)); return tag_header; }
connection_ptr accept_locked(boost::unique_lock<mutex_type> & header_lock) { connection_ptr res; util::mpi_environment::scoped_try_lock l; if(l.locked) { MPI_Status status; if(request_done_locked(hdr_request_, &status)) { header h = new_header(); l.unlock(); header_lock.unlock(); res.reset( new connection_type( status.MPI_SOURCE , h , pp_ ) ); return res; } } return res; }
void set_tag(const char* file_name, ID3v2_tag* tag) { if(tag == NULL) { return; } int padding = 2048; int old_size = tag->tag_header->tag_size; // Set the new tag header tag->tag_header = new_header(); memcpy(tag->tag_header->tag, "ID3", 3); tag->tag_header->major_version = '\x03'; tag->tag_header->minor_version = '\x00'; tag->tag_header->flags = '\x00'; tag->tag_header->tag_size = get_tag_size(tag) + padding; // Create temp file and prepare to write FILE* file; FILE* temp_file; file = fopen(file_name, "r+b"); temp_file = tmpfile(); // Write to file write_header(tag->tag_header, temp_file); ID3v2_frame_list* frame_list = tag->frames->start; while(frame_list != NULL) { write_frame(frame_list->frame, temp_file); frame_list = frame_list->next; } // Write padding int i; for(i = 0; i < padding; i++) { putc('\x00', temp_file); } fseek(file, old_size + 10, SEEK_SET); int c; while((c = getc(file)) != EOF) { putc(c, temp_file); } // Write temp file data back to original file fseek(temp_file, 0, SEEK_SET); fseek(file, 0, SEEK_SET); while((c = getc(temp_file)) != EOF) { putc(c, file); } fclose(file); fclose(temp_file); }
/* * send_nak() : sendet ein Negative-Acknowledge fuer einen fehlerhaft * empfangenen Block und fordert damit den Sender zum * Retransmit auf. ZCONNECT (in diesem Teil der Online- * Phase) hat ein Empfangsfenster von 1, d.h. der Empfaenger * weiss immer genau, welches Paket jetzt vom Sender kommen * muss. */ static void send_nak(void) { header_p p; p = new_header("NAK0", HD_STATUS); wr_packet(p, stdout); free_para(p); }
/* * send_tme(phase) : sendet ein Transmission End fuer einen korrekt * gesendeten Block aus der angegebenen Phase [1 .. 4] */ static void send_tme(int phase) { char tmp[5]; header_p p; sprintf(tmp, "TME%d", phase); p = new_header(tmp, HD_STATUS); wr_packet(p, stdout); free_para(p); }
void add_char(char *name, char *sz) { struct HEADERKEY *p = new_header(); p->name = name; p->type = CHARSTAR; if( sz ) p->alen = atoi(sz); else p->alen = 1; }
header read_header (void) { register header the_header = ((header) NULL); register line_list *next_line = ((line_list *) NULL); init_linebuffer (&lb); do { long length; register char *line; readline (&lb, stdin); line = lb.buffer; length = strlen (line); if (length == 0) break; if (has_keyword (line)) { register header old = the_header; the_header = new_header (); if (old == ((header) NULL)) { the_header->next = the_header; the_header->previous = the_header; } else { the_header->previous = old; the_header->next = old->next; old->next = the_header; } next_line = &(the_header->text); } if (next_line == ((line_list *) NULL)) { /* Not a valid header */ exit (EXIT_FAILURE); } *next_line = new_list (); (*next_line)->string = alloc_string (length); strcpy (((*next_line)->string), line); next_line = &((*next_line)->continuation); *next_line = NIL; } while (true); if (! the_header) fatal ("input message has no header"); return the_header->next; }
void add_notchar(int v, char *name) { struct HEADERKEY *p = new_header(); p->name = name; if ( v == CHARSTAR ) p->type = BYTE; else p->type = v; p->alen = 1; }
void add_array(int v, char *name, char *sz) { struct HEADERKEY *p = new_header(); p->name = name; if ( v == CHARSTAR ) p->type = BYTE; else p->type = v; if( sz ) p->alen = atoi(sz); else p->alen = 1; }
/* * send_eot(phase) : sendet ein Acknowledge fuer einen korrekt empfangenen * Block aus der angegebenen Phase [5 .. 6] (3 Bloecke) */ static void send_eot(int phase) { char tmp[5]; header_p p; sprintf(tmp, "EOT%d", phase); p = new_header(tmp, HD_STATUS); wr_packet(p, stdout); sleep(1); wr_packet(p, stdout); sleep(1); wr_packet(p, stdout); free_para(p); }
int header_field_cb (http_parser *p, const char *buf, size_t len, char partial) { uint32_t i; header *h; client_t *client = get_client(p); request *req = client->req; char temp[len]; buffer_result ret = MEMORY_ERROR; if (req->last_header_element != FIELD){ if(LIMIT_REQUEST_FIELDS <= req->num_headers){ client->bad_request_code = 400; return -1; } req->num_headers++; } i = req->num_headers; h = req->headers[i]; key_upper(temp, buf, len); if(h){ ret = write2buf(h->field, temp, len); }else{ req->headers[i] = h = new_header(128, LIMIT_REQUEST_FIELD_SIZE, 1024, LIMIT_REQUEST_FIELD_SIZE); rack_header_type type = check_header_type(temp); if(type == OTHER){ ret = write2buf(h->field, "HTTP_", 5); } ret = write2buf(h->field, temp, len); //printf("%s \n", getString(h->field)); } switch(ret){ case MEMORY_ERROR: client->bad_request_code = 500; return -1; case LIMIT_OVER: client->bad_request_code = 400; return -1; default: break; } req->last_header_element = FIELD; return 0; }
static esl_status_t esl_event_base_add_header(esl_event_t *event, esl_stack_t stack, const char *header_name, char *data) { esl_event_header_t *header = NULL; esl_ssize_t hlen = -1; int exists = 0, fly = 0; char *index_ptr; int index = 0; char *real_header_name = NULL; if ((index_ptr = strchr(header_name, '['))) { index_ptr++; index = atoi(index_ptr); real_header_name = DUP(header_name); if ((index_ptr = strchr(real_header_name, '['))) { *index_ptr++ = '\0'; } header_name = real_header_name; } if (index_ptr || (stack & ESL_STACK_PUSH) || (stack & ESL_STACK_UNSHIFT)) { if (!(header = esl_event_get_header_ptr(event, header_name)) && index_ptr) { header = new_header(header_name); if (esl_test_flag(event, ESL_EF_UNIQ_HEADERS)) { esl_event_del_header(event, header_name); } fly++; } if ((header = esl_event_get_header_ptr(event, header_name))) { if (index_ptr) { if (index > -1 && index <= 4000) { if (index < header->idx) { FREE(header->array[index]); header->array[index] = DUP(data); } else { int i; char **m; m = realloc(header->array, sizeof(char *) * (index + 1)); esl_assert(m); header->array = m; for (i = header->idx; i < index; i++) { m[i] = DUP(""); } m[index] = DUP(data); header->idx = index + 1; if (!fly) { exists = 1; } goto redraw; } } goto end; } else { if ((stack & ESL_STACK_PUSH) || (stack & ESL_STACK_UNSHIFT)) { exists++; stack &= ~(ESL_STACK_TOP | ESL_STACK_BOTTOM); } else { header = NULL; } } } } if (!header) { if (esl_strlen_zero(data)) { esl_event_del_header(event, header_name); FREE(data); goto end; } if (esl_test_flag(event, ESL_EF_UNIQ_HEADERS)) { esl_event_del_header(event, header_name); } if (strstr(data, "ARRAY::")) { esl_event_add_array(event, header_name, data); FREE(data); goto end; } header = new_header(header_name); } if ((stack & ESL_STACK_PUSH) || (stack & ESL_STACK_UNSHIFT)) { char **m = NULL; esl_size_t len = 0; char *hv; int i = 0, j = 0; if (header->value && !header->idx) { m = malloc(sizeof(char *)); esl_assert(m); m[0] = header->value; header->value = NULL; header->array = m; header->idx++; m = NULL; } i = header->idx + 1; m = realloc(header->array, sizeof(char *) * i); esl_assert(m); if ((stack & ESL_STACK_PUSH)) { m[header->idx] = data; } else if ((stack & ESL_STACK_UNSHIFT)) { for (j = header->idx; j > 0; j--) { m[j] = m[j-1]; } m[0] = data; } header->idx++; header->array = m; redraw: len = 0; for(j = 0; j < header->idx; j++) { len += strlen(header->array[j]) + 2; } if (len) { len += 8; hv = realloc(header->value, len); esl_assert(hv); header->value = hv; esl_snprintf(header->value, len, "ARRAY::"); for(j = 0; j < header->idx; j++) { esl_snprintf(header->value + strlen(header->value), len - strlen(header->value), "%s%s", j == 0 ? "" : "|:", header->array[j]); } } } else { header->value = data; } if (!exists) { header->hash = esl_ci_hashfunc_default(header->name, &hlen); if ((stack & ESL_STACK_TOP)) { header->next = event->headers; event->headers = header; if (!event->last_header) { event->last_header = header; } } else { if (event->last_header) { event->last_header->next = header; } else { event->headers = header; header->next = NULL; } event->last_header = header; } } end: esl_safe_free(real_header_name); return ESL_SUCCESS; }
void run() { util::mpi_environment::scoped_lock l; new_header(); }
struct header * pc_wav_to_feasd(char *prefix, int num_read, FILE *file) { pc_wav_hd *wav_hd; double start_time; double sf; struct header *ohd; wav_hd = get_hd(prefix, num_read, file); if (wav_hd == NULL) { error_msg("pc_wav_to_feasd", "read header failed."); return NULL; } switch (wav_hd->FormatTag) { case WAVE_FORMAT_PCM: if (wav_hd->spec.pcm.BitsPerSample != 16) { error_msg("pc_wav_to_feasd", "only 16-bit audio supported."); return NULL; } break; default: error_msg("pc_wav_to_feasd", "only WAVE_FORMAT_PCM supported."); return NULL; } ohd = new_header(FT_FEA); if (ohd == NULL) { error_msg("pc_wav_to_feasd", "couldn't create FEA header."); return NULL; } strcpy(ohd->common.prog, "pc_wav_to_feasd"); add_comment(ohd, "Converted from PC WAVE header\n"); ohd->common.tag = NO; ohd->common.edr = NO; start_time = 0.0; sf = (double) wav_hd->SamplesPerSec; init_feasd_hd(ohd, SHORT, (int) wav_hd->Channels, &start_time, NO, sf); *add_genhd_l("FormatTag", (long *) NULL, 1, ohd) = (long) wav_hd->FormatTag; *add_genhd_l("Channels", (long *) NULL, 1, ohd) = (long) wav_hd->Channels; *add_genhd_l("SamplesPerSec", (long *) NULL, 1, ohd) = (long) wav_hd->SamplesPerSec; *add_genhd_l("AvgBytesPerSec", (long *) NULL, 1, ohd) = (long) wav_hd->AvgBytesPerSec; *add_genhd_l("BlockAlign", (long *) NULL, 1, ohd) = (long) wav_hd->BlockAlign; *add_genhd_l("BitsPerSample", (long *) NULL, 1, ohd) = (long) wav_hd->spec.pcm.BitsPerSample; *add_genhd_l("pc_wav_hd_ptr", (long *) NULL, 1, ohd) = (long) wav_hd; return ohd; }
static struct header * FieldList_to_fea(FieldList list, struct fea_data **rec, char **fnames, int copy_sources) { struct header *hdr; int i, j; FieldSpec *fld; char **codes; FieldList subfields; FieldSpec *subf; char *name; void *ptr; int spstype; long *dim; struct fea_header *fea; struct header *source; if (list == NULL) return NULL; hdr = new_header(FT_FEA); for (i = 0; (fld = list[i]) != NULL; i++) { codes = NULL; if (fld->occurrence != VIRTUAL && (subfields = fld->subfields) != NULL) { for (j = 0; (subf = subfields[j]) != NULL; j++) { if (strcmp(subf->name, "enumStrings") != 0) { if (debug_level) fprintf(stderr, "FieldList_to_fea: subfields " "not supported in ESPS FEA files.\n"); } else if (fld->type != SHORT) { if (debug_level) fprintf(stderr, "FieldList_to_fea: Non-SHORT field " "has subfield enumStrings.\n"); } else if (subf->type != CHAR) { if (debug_level) fprintf(stderr, "FieldList_to_fea: enumStrings not CHAR.\n"); } else if (subf->rank != 2) { if (debug_level) fprintf(stderr, "FieldList_to_fea: enumStrings " "not of rank 2.\n"); } else codes = StrArrayFromRect(subf->dim, subf->data); } } if (FieldIsTag(fld) && FindStr(ESPS_TAG, fnames)) hdr->common.tag = TRUE; else if (FieldIsFeaSubtype(fld)) hdr->hd.fea->fea_type = *(short *) fld->data; else { name = fld->name; switch(fld->occurrence) { case GLOBAL: { int size = (int) FieldLength(fld); ptr = fld->data; if (debug_level >= 2) fprintf(stderr, "FieldList_to_fea: " "global field[%d]: \"%s\".\n", i, name); if (fld->rank > 1) { if (debug_level) fprintf(stderr, "FieldList_to_fea: rank %d globals " "not supported in ESPS FEA files.\n", fld->rank); } if (size == 0) { if (debug_level) fprintf(stderr, "FieldList_to_fea: empty globals " "not supported in ESPS FEA files.\n"); } else if (codes != NULL) (void) add_genhd_e(name, (short *) ptr, size, codes, hdr); else switch (fld->type) { case DOUBLE: (void) add_genhd_d(name, (double *) ptr, size, hdr); break; case FLOAT: (void) add_genhd_f(name, (float *) ptr, size, hdr); break; case LONG: (void) add_genhd_l(name, (long *) ptr, size, hdr); break; case SHORT: (void) add_genhd_s(name, (short *) ptr, size, hdr); break; case SCHAR: case UCHAR: case CHAR: (void) add_genhd_c(name, (char *) ptr, size, hdr); break; default: if (debug_level) fprintf(stderr, "FieldList_to_fea: global type %d " "not supported in ESPS FEA files.\n", fld->type); } } break; case REQUIRED: { long size = FieldLength(fld); if (debug_level >= 2) fprintf(stderr, "FieldList_to_fea: " "required field[%d]: \"%s\".\n", i, name); if (FindStr(name, fnames)) { spstype = (codes != NULL) ? CODED : ElibTypeToEsps(fld->type); if (spstype != UNDEF) { dim = (long *) malloc(fld->rank * sizeof(long)); for (j = 0; j < fld->rank; j++) dim[j] = fld->dim[j]; add_fea_fld(name, size, fld->rank, dim, spstype, codes, hdr); } } } break; case OPTIONAL: if (debug_level) fprintf(stderr, "FieldList_to_fea: optional fields " "not supported in ESPS FEA files.\n"); break; case VIRTUAL: if (copy_sources) { if (strncmp(name, "source_", 7) != 0) { if (debug_level) fprintf(stderr, "Field_List_to_fea: VIRTUAL " "field other than source_<n>.\n"); } else if ((subfields = fld->subfields) != NULL || fld->type == CHAR) { size_t len; char *data; source = FieldList_to_fea(subfields, NULL, NULL, TRUE); len = FieldLength(fld); data = (char *) malloc(len + 1); strncpy(data, fld->data, len); data[len] = '\0'; add_source_file(hdr, data, source); } } break; case INCLUDED: if (debug_level) fprintf(stderr, "FieldList_to_fea: included fields " "not supported in ESPS FEA files.\n"); break; default: spsassert(0, "FieldList_to_fea: " "unrecognized occurrence code.\n"); break; } } } if (rec != NULL) { *rec = allo_fea_rec(hdr); fea = hdr->hd.fea; if (hdr->common.tag) { fld = FindField(list, ESPS_TAG); fld->data = &(*rec)->tag; } for (i = 0; i < (int) fea->field_count; i++) { name = fea->names[i]; fld = FindField(list, name); fld->data = get_fea_ptr(*rec, name, hdr); } } return hdr; }