void IniLoader::Entry( const wxString& var, Fixed100& value, const Fixed100 defvalue ) { // Note: the "easy" way would be to convert to double and load/save that, but floating point // has way too much rounding error so we really need to do things out manually.. >_< wxString readval( value.ToString() ); if( m_Config ) m_Config->Read( var, &readval ); value = Fixed100::FromString( readval, value ); }
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; }
buzztok_t buzzlex_nexttok(buzzlex_t lex) { buzzlex_file_t lexf = buzzlex_getfile(lex); do { /* Look for a non-space character */ do { /* Keep reading until you find a non-space character or end of stream */ while(lexf->cur_c < lexf->buf_size && buzzlex_isspace(lexf->buf[lexf->cur_c])) { nextchar(); } /* End of stream? */ if(lexf->cur_c >= lexf->buf_size) { /* Done with current file, go back to previous */ buzzdarray_pop(lex); if(buzzdarray_isempty(lex)) /* No file to go back to, done parsing */ return NULL; lexf = buzzlex_getfile(lex); } else /* Non-space character found */ break; } while(1); /* Non-space character found */ /* If the current character is a '#' ignore the rest of the line */ if(lexf->buf[lexf->cur_c] == '#') { do { nextchar(); } while(lexf->cur_c < lexf->buf_size && lexf->buf[lexf->cur_c] != '\n'); /* End of stream? */ if(lexf->cur_c >= lexf->buf_size) { /* Done with current file, go back to previous */ buzzdarray_pop(lex); if(buzzdarray_isempty(lex)) /* No file to go back to, done parsing */ return NULL; lexf = buzzlex_getfile(lex); } else { /* New line and carry on */ ++lexf->cur_line; lexf->cur_col = 0; ++lexf->cur_c; } } else if(strncmp(lexf->buf + lexf->cur_c, "include", 7) == 0) { /* Manage file inclusion */ lexf->cur_c += 7; lexf->cur_col += 7; /* Skip whitespace */ while(lexf->cur_c < lexf->buf_size && buzzlex_isspace(lexf->buf[lexf->cur_c])) { nextchar(); } /* End of file or not-string opening -> syntax error */ if(lexf->cur_c >= lexf->buf_size || !buzzlex_isquote(lexf->buf[lexf->cur_c])) { fprintf(stderr, "%s:%" PRIu64 ":%" PRIu64 ": Syntax error: expected string after include\n", lexf->fname, lexf->cur_line, lexf->cur_col); return NULL; } /* Read string */ char quote = lexf->buf[lexf->cur_c]; size_t start = lexf->cur_c + 1; nextchar(); while(lexf->cur_c < lexf->buf_size && lexf->buf[lexf->cur_c] != quote && lexf->buf[lexf->cur_c] != '\n') { nextchar(); } /* End of file or newline -> syntax error */ if(lexf->cur_c >= lexf->buf_size || lexf->buf[lexf->cur_c] == '\n') { fprintf(stderr, "%s:%" PRIu64 ":%" PRIu64 ": Syntax error: expected end of string\n", lexf->fname, lexf->cur_line, lexf->cur_col); return NULL; } /* Copy data into a new string */ char* fname = (char*)malloc(lexf->cur_c - start + 1); strncpy(fname, lexf->buf + start, lexf->cur_c - start); fname[lexf->cur_c - start] = '\0'; /* Get to next character in this file */ nextchar(); /* Create new file structure */ buzzlex_file_t f = buzzlex_file_new(fname); if(!f) { fprintf(stderr, "%s:%" PRIu64 ":%" PRIu64 ": Can't read '%s'\n", lexf->fname, lexf->cur_line, lexf->cur_col, fname); free(fname); return NULL; } free(fname); /* Make sure the file hasn't been already included */ if(buzzdarray_find(lex, buzzlex_file_cmp, &f) < buzzdarray_size(lex)) { buzzlex_file_destroy(0, &f, NULL); } else { /* Push file structure */ buzzdarray_push(lex, &f); lexf = buzzlex_getfile(lex); } } else /* The character must be parsed */ break; } while(1); /* If we get here it's because we read potential token character */ uint64_t tokstart = lexf->cur_col - 1; char c = lexf->buf[lexf->cur_c]; nextchar(); /* Consider the 1-char non-alphanumeric cases first */ switch(c) { case '\n': { buzztok_t tok = buzzlex_newtok(BUZZTOK_STATEND, NULL, lexf->cur_line, tokstart, lexf->fname); ++lexf->cur_line; lexf->cur_col = 0; return tok; } casetokchar(';', BUZZTOK_STATEND); casetokchar('{', BUZZTOK_BLOCKOPEN); casetokchar('}', BUZZTOK_BLOCKCLOSE); casetokchar('(', BUZZTOK_PAROPEN); casetokchar(')', BUZZTOK_PARCLOSE); casetokchar('[', BUZZTOK_IDXOPEN); casetokchar(']', BUZZTOK_IDXCLOSE); casetokchar(',', BUZZTOK_LISTSEP); casetokchar('.', BUZZTOK_DOT); } /* If we get here, it's because we found either a constant, an * identifier, a keyword, an assignment, a comparison operator, * an arithmetic operator, or an unexpected character */ if(isdigit(c)) { /* It's a constant */ readval(buzzlex_isnumber); return buzzlex_newtok(BUZZTOK_CONST, val, lexf->cur_line, tokstart, lexf->fname); } else if(isalpha(c)) { /* It's either a keyword or an identifier */ readval(buzzlex_isid); /* Go through the possible keywords */ checkkeyword("var", BUZZTOK_VAR); checkkeyword("if", BUZZTOK_IF); checkkeyword("else", BUZZTOK_ELSE); checkkeyword("function", BUZZTOK_FUN); checkkeyword("return", BUZZTOK_RETURN); checkkeyword("for", BUZZTOK_FOR); checkkeyword("while", BUZZTOK_WHILE); checkkeyword("and", BUZZTOK_ANDOR); checkkeyword("or", BUZZTOK_ANDOR); checkkeyword("not", BUZZTOK_NOT); checkkeyword("nil", BUZZTOK_NIL); /* No keyword found, consider it an id */ return buzzlex_newtok(BUZZTOK_ID, val, lexf->cur_line, tokstart, lexf->fname); } else if(c == '=') { /* Either an assignment or a comparison */ if(lexf->cur_c < lexf->buf_size && lexf->buf[lexf->cur_c] == '=') { /* It's a comparison */ nextchar(); return buzzlex_newtok(BUZZTOK_CMP, strdup("=="), lexf->cur_line, tokstart, lexf->fname); } else { /* It's an assignment */ return buzzlex_newtok(BUZZTOK_ASSIGN, NULL, lexf->cur_line, tokstart, lexf->fname); } } else if(c == '!') { /* Comparison operator? */ if(lexf->cur_c < lexf->buf_size && lexf->buf[lexf->cur_c] == '=') { /* It's a comparison */ nextchar(); return buzzlex_newtok(BUZZTOK_CMP, strdup("!="), lexf->cur_line, tokstart, lexf->fname); } else { /* Syntax error */ fprintf(stderr, "%s:%" PRIu64 ":%" PRIu64 ": Syntax error: expected '=' after '!'\n", lexf->fname, lexf->cur_line, tokstart); return NULL; } } else if((c == '<') || (c == '>')) { /* It's a comparison operator */ size_t start = lexf->cur_c - 1; /* Include the '=' if present */ if(lexf->cur_c < lexf->buf_size && lexf->buf[lexf->cur_c] == '=') { nextchar(); } char* val = (char*)malloc(lexf->cur_c - start + 1); strncpy(val, lexf->buf + start, lexf->cur_c - start); val[lexf->cur_c - start] = 0; return buzzlex_newtok(BUZZTOK_CMP, val, lexf->cur_line, tokstart, lexf->fname); } else if(buzzlex_isarith(c)) { /* Arithmetic operator */ char* val = (char*)malloc(2); strncpy(val, lexf->buf + lexf->cur_c - 1, 1); val[1] = 0; switch(c) { case '+': case '-': { return buzzlex_newtok(BUZZTOK_ADDSUB, val, lexf->cur_line, tokstart, lexf->fname); } case '*': case '/': { return buzzlex_newtok(BUZZTOK_MULDIV, val, lexf->cur_line, tokstart, lexf->fname); } case '%': { return buzzlex_newtok(BUZZTOK_MOD, val, lexf->cur_line, tokstart, lexf->fname); } case '^': { return buzzlex_newtok(BUZZTOK_POW, val, lexf->cur_line, tokstart, lexf->fname); } default: return NULL; } } else if(buzzlex_isquote(c)) { /* String - eat any character until you find the next matching quote */ size_t start = lexf->cur_c; char last1 = 0, last2 = 0; while(lexf->cur_c < lexf->buf_size && /* Not end of stream */ ((lexf->buf[lexf->cur_c] != c) || /* Matching quote not found */ (lexf->buf[lexf->cur_c] == c && /* Matching quote found, but preceded by \ and not \\ */ last1 == '\\' && last2 != '\\'))) { /* Remember the last two characters read */ last2 = last1; last1 = lexf->buf[lexf->cur_c]; /* Keep parsing the string */ if(lexf->buf[lexf->cur_c] != '\n') { nextchar(); } else { fprintf(stderr, "%s:%" PRIu64 ":%" PRIu64 ": Syntax error: string closing quote not found\n", lexf->fname, lexf->cur_line, tokstart); return NULL; } } /* End of stream? Syntax error */ if(lexf->cur_c >= lexf->buf_size) { fprintf(stderr, "%s:%" PRIu64 ":%" PRIu64 ": Syntax error: string closing quote not found\n", lexf->fname, lexf->cur_line, tokstart); return NULL; } /* We have a valid string */ char* val = buzzlex_newstring(lexf->buf + start, lexf->cur_c - start); nextchar(); return buzzlex_newtok(BUZZTOK_STRING, val, lexf->cur_line, tokstart, lexf->fname); } else { /* Unknown character */ fprintf(stderr, "%s:%" PRIu64 ":%" PRIu64 ": Syntax error: unknown character '%c' (octal: %o; hex: %x)\n", lexf->fname, lexf->cur_line, tokstart, c, c, c); return NULL; } }
buzztok_t buzzlex_nexttok(buzzlex_t lex) { do { /* Keep reading until you find a non-space character or end of stream */ while(lex->cur_c < lex->buf_size && buzzlex_isspace(lex->buf[lex->cur_c])) { nextchar(); } /* End of stream? No token */ if(lex->cur_c >= lex->buf_size) return NULL; /* If the current character is a '#' ignore the rest of the line */ if(lex->buf[lex->cur_c] == '#') { do { nextchar(); } while(lex->cur_c < lex->buf_size && lex->buf[lex->cur_c] != '\n'); /* End of stream? No token */ if(lex->cur_c >= lex->buf_size) return NULL; /* New line and carry on */ ++lex->cur_line; lex->cur_col = 0; ++lex->cur_c; } else { /* The current character must be parsed */ break; } } while(1); /* If we get here it's because we read a non-space character */ char c = lex->buf[lex->cur_c]; nextchar(); /* Consider the 1-char non-alphanumeric cases first */ switch(c) { case '\n': { buzztok_t tok = buzzlex_newtok(BUZZTOK_STATEND, NULL, lex->cur_line, lex->cur_col); ++lex->cur_line; lex->cur_col = 0; return tok; } casetokchar(';', BUZZTOK_STATEND); casetokchar('{', BUZZTOK_BLOCKOPEN); casetokchar('}', BUZZTOK_BLOCKCLOSE); casetokchar('(', BUZZTOK_PAROPEN); casetokchar(')', BUZZTOK_PARCLOSE); casetokchar('[', BUZZTOK_IDXOPEN); casetokchar(']', BUZZTOK_IDXCLOSE); casetokchar(',', BUZZTOK_LISTSEP); casetokchar('.', BUZZTOK_DOT); } /* If we get here, it's because we found either a constant, an * identifier, a keyword, an assignment, a comparison operator, * an arithmetic operator, or an unexpected character */ if(isdigit(c)) { /* It's a constant */ readval(buzzlex_isnumber); return buzzlex_newtok(BUZZTOK_CONST, val, lex->cur_line, lex->cur_col); } else if(isalpha(c)) { /* It's either a keyword or an identifier */ readval(buzzlex_isid); /* Go through the possible keywords */ checkkeyword("var", BUZZTOK_VAR); checkkeyword("if", BUZZTOK_IF); checkkeyword("else", BUZZTOK_ELSE); checkkeyword("function", BUZZTOK_FUN); checkkeyword("return", BUZZTOK_RETURN); checkkeyword("for", BUZZTOK_FOR); checkkeyword("while", BUZZTOK_WHILE); checkkeyword("and", BUZZTOK_ANDOR); checkkeyword("or", BUZZTOK_ANDOR); checkkeyword("not", BUZZTOK_NOT); checkkeyword("nil", BUZZTOK_NIL); /* No keyword found, consider it an id */ return buzzlex_newtok(BUZZTOK_ID, val, lex->cur_line, lex->cur_col); } else if(c == '=') { /* Either an assignment or a comparison */ if(lex->cur_c < lex->buf_size && lex->buf[lex->cur_c] == '=') { /* It's a comparison */ nextchar(); return buzzlex_newtok(BUZZTOK_CMP, strdup("=="), lex->cur_line, lex->cur_col); } else { /* It's an assignment */ return buzzlex_newtok(BUZZTOK_ASSIGN, NULL, lex->cur_line, lex->cur_col); } } else if(c == '!') { /* Comparison operator? */ if(lex->cur_c < lex->buf_size && lex->buf[lex->cur_c] == '=') { /* It's a comparison */ nextchar(); return buzzlex_newtok(BUZZTOK_CMP, strdup("!="), lex->cur_line, lex->cur_col); } else { /* Syntax error */ fprintf(stderr, "%s:%llu:%llu: Syntax error: expected '=' after '!'\n", lex->fname, lex->cur_line, lex->cur_col); return NULL; } } else if((c == '<') || (c == '>')) { /* It's a comparison operator */ size_t start = lex->cur_c - 1; /* Include the '=' if present */ if(lex->cur_c < lex->buf_size && lex->buf[lex->cur_c] == '=') { nextchar(); } char* val = (char*)malloc(lex->cur_c - start + 1); strncpy(val, lex->buf + start, lex->cur_c - start); val[lex->cur_c - start] = 0; return buzzlex_newtok(BUZZTOK_CMP, val, lex->cur_line, lex->cur_col); } else if(buzzlex_isarith(c)) { /* Arithmetic operator */ char* val = (char*)malloc(2); strncpy(val, lex->buf + lex->cur_c - 1, 1); val[1] = 0; switch(c) { case '+': case '-': { return buzzlex_newtok(BUZZTOK_ADDSUB, val, lex->cur_line, lex->cur_col); } case '*': case '/': { return buzzlex_newtok(BUZZTOK_MULDIV, val, lex->cur_line, lex->cur_col); } case '%': { return buzzlex_newtok(BUZZTOK_MOD, val, lex->cur_line, lex->cur_col); } case '^': { return buzzlex_newtok(BUZZTOK_POW, val, lex->cur_line, lex->cur_col); } default: return NULL; } } else if(buzzlex_isquote(c)) { /* String - eat any character until you find the next matching quote */ size_t start = lex->cur_c; while(lex->cur_c < lex->buf_size && lex->buf[lex->cur_c] != c) { if(lex->buf[lex->cur_c] != '\n') { nextchar(); } else { ++lex->cur_line; lex->cur_col = 0; ++lex->cur_c; } } /* End of stream? Syntax error */ if(lex->cur_c >= lex->buf_size) { fprintf(stderr, "%s:%llu:%llu: Syntax error: string closing quote not found\n", lex->fname, lex->cur_line, lex->cur_col); return NULL; } /* Valid string */ char* val = (char*)malloc(lex->cur_c - start + 1); strncpy(val, lex->buf + start, lex->cur_c - start); val[lex->cur_c - start] = '\0'; nextchar(); return buzzlex_newtok(BUZZTOK_STRING, val, lex->cur_line, lex->cur_col); } else { /* Unknown character */ fprintf(stderr, "%s:%llu:%llu: Syntax error: unknown character '%c' (octal: %o; hex: %x)\n", lex->fname, lex->cur_line, lex->cur_col, c, c, c); return NULL; } }
/** * prints the device signature * * @return true on success; exit on error */ int read_info() { long i, j; char s[256]; FILE *fp; ///get bootloader REVISON sendcommand(REVISION); i = readval(); if(i == -2) { printf("Bootloader Version unknonwn (Fail)\n"); } else { printf("Bootloader : V%lX.%lX\n", i>>8, i&0xFF); } ///get SIGNATURE sendcommand(SIGNATURE); i = readval(); if (i == -2) { printf("Reading device SIGNATURE failed!\n\n"); exit (0); } if((fp = fopen(devices_file_p, "r")) != NULL) { while(fgets(s, 256, fp)) { if(sscanf(s, "%lX : %s", &j, s) == 2) { // valid entry if(i == j) { break; } } *s = 0; } fclose(fp); } else { sscanf ("(?)" , "%s", s); printf("File \"%s\" not found!\n",DEVICES_FILE); } if (!*s) sprintf (s,"Device not in \"%s\"",DEVICES_FILE); printf("Target : %06lX %s\n", i, s); ///get BUFFSIZE sendcommand(BUFFSIZE); i = readval(); if (i == -2) { printf("Reading BUFFSIZE failed!\n\n"); exit (0); } buffersize = i; printf("Buffer : %6ld Bytes\n", i ); ///get USERFLASH sendcommand(USERFLASH); i = readval(); if (i == -2) { printf("Reading FLASHSIZE failed!\n\n"); exit (0); } if( i > MAXFLASH) { printf("Device an flashsize do not match!\n"); exit (0); } flashsize = i; printf("Size available: %6ld Bytes\n\n", i ); return 1; }//int read_info()