static int breakargs(const char *line, char **argv) { int argc = 0; while(*line) { while(*line && isspace(CTOI(*line))) line++; if ( *line == '"' ) /* Windows-95 quoted arguments */ { const char *start = line+1; const char *end = start; while( *end && *end != '"' ) end++; if ( *end == '"' ) { argv[argc++] = strndup(start, end-start); line = end+1; continue; } } if ( *line ) { const char *start = line; while(*line && !isspace(CTOI(*line))) line++; argv[argc++] = strndup(start, line-start); } } argv[argc] = NULL; /* add trailing NULL pointer to argv */ return argc; }
static void quoted_name(const char *name, char *plname) { int needquote = TRUE; if ( islower(CTOI(name[0])) ) { const char *s = name+1; for( ; *s && (isalnum(CTOI(*s)) || *s == '_'); s++) ; if ( *s == '\0' ) needquote = FALSE; } if ( !needquote ) strcpy(plname, name); else { char *o = plname; *o++ = '\''; for( ; *name; name++) { if ( *name == '\'' ) *o++ = *name; *o++ = *name; } *o++ = '\''; *o = '\0'; } }
TNETS_VALUE tnets_render_number(char* payload, size_t len) { size_t i; int number = 0; // TODO: use a type that can handle // larger numbers without wrapping. unsigned short int negative = 0; if (payload[0] == '-') { negative = 1; payload++; len--; } for(i = 0; i < len; i++) { if (IS_NUMERIC(payload[i])) { number *= 10; number += CTOI(payload[i]); } else { TNETS_PARSER_ERROR("Non-numeric character in a tnets number"); } } if (negative) { number *= -1; } return TNETS_WRAP_NUMBER(number); }
size_t tnets_size_spec(char *data, size_t len, char **payload) { size_t i; size_t size = 0; // only scan 11 characters (10 for length spec + 1 for colon) size_t scan_limit = (len < 11) ? len : 11; for (i = 0; i < scan_limit; i++) { if (IS_NUMERIC(data[i])) { size *= 10; size += CTOI(data[i]); } else if (data[i] == ':') { if (i + size > len + 1) { TNETS_PARSER_ERROR("length spec longer than given string"); } if (payload != NULL) { *payload = data + i + 1; // add one to skip over the colon } return size; } else { TNETS_PARSER_ERROR("non-numeric character in length spec"); } } TNETS_PARSER_ERROR("length spec more than 10 characters"); return 0; }
/* * Quick string to unsigned long conversion function. This function performs * no overflow checking and is only meant for internal mdb use. It allows * the caller to specify the length of the string in bytes and a base. */ ulong_t strntoul(const char *s, size_t nbytes, int base) { ulong_t n; int c; for (n = 0; nbytes != 0 && (c = *s) != '\0'; s++, nbytes--) n = n * base + CTOI(c); return (n); }
/* * Convert a string to an unsigned integer value using the specified base. * In the event of overflow or an invalid character, we generate an * error message and longjmp back to the main loop using yyerror(). */ uintmax_t strtonum(const char *s, int base) { uintmax_t multmax = (uintmax_t)ULLONG_MAX / (uintmax_t)(uint_t)base; uintmax_t val = 0; int c, i, neg = 0; switch (c = *s) { case '-': neg++; /*FALLTHRU*/ case '+': c = *++s; } if (c == '\0') goto done; if ((val = CTOI(c)) >= base) yyerror("digit '%c' is invalid in current base\n", c); for (c = *++s; c != '\0'; c = *++s) { if (val > multmax) goto oflow; if ((i = CTOI(c)) >= base) yyerror("digit '%c' is invalid in current base\n", c); val *= base; if ((uintmax_t)ULLONG_MAX - val < (uintmax_t)i) goto oflow; val += i; } done: return (neg ? -val : val); oflow: yyerror("specified value exceeds maximum immediate value\n"); return ((uintmax_t)ULLONG_MAX); }
double calc_ic_column(const char *text, size_t length, size_t offset, size_t spacing) { unsigned foundc[ALPHACHARS] = {0}, total_chars = 0; unsigned long pre_sum = 0; for (size_t i = offset; i < length; i += spacing, total_chars++) foundc[CTOI(text[i])]++; for (unsigned i = 0; i < ALPHACHARS; i++) pre_sum += foundc[i] * (foundc[i] - 1); return (double)pre_sum / ((double)total_chars * (double)(total_chars - 1) / ALPHACHARS); }
// Função para fazer o load das texturas de acordo com o indice int LoadBMP(char *file, int id){ GLubyte *image; GLubyte Header[0x54]; GLuint DataPos, imageSize; GLsizei Width, Height; int nb = 0; // Abre o arquivo e efetua a leitura do Header do arquivo BMP FILE * fp_arquivo = fopen(file, "rb"); if (!fp_arquivo) return -1; if (fread(Header, 1, 0x36, fp_arquivo) != 0x36) SAIR; if (Header[0] != 'B' || Header[1] != 'M') SAIR; if (CTOI(Header[0x1E]) != 0) SAIR; if (CTOI(Header[0x1C]) != 24) SAIR; // Recupera a informação dos atributos de // altura e largura da imagem Width = CTOI(Header[0x12]); Height = CTOI(Header[0x16]); (CTOI(Header[0x0A]) == 0) ? (DataPos = 0x36) : (DataPos = CTOI(Header[0x0A])); imageSize = Width*Height * 3; // Efetura a Carga da Imagem image = (GLubyte *)malloc(imageSize); int retorno; retorno = fread(image, 1, imageSize, fp_arquivo); if (retorno != imageSize) { free(image); SAIR; } // Inverte os valores de R e B int t, i; for (i = 0; i < (int)imageSize; i += 3) { t = image[i]; image[i] = image[i + 2]; image[i + 2] = t; } // Tratamento da textura para o OpenGL glBindTexture(GL_TEXTURE_2D, texture_id[id]); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); // Faz a geraçao da textura na memória- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, Width, Height, 0, GL_RGB, GL_UNSIGNED_BYTE, image); fclose(fp_arquivo); free(image); return 1; }
/******************************* * PROLOG OPTIONS * *******************************/ static void getPrologOptions() { FILE *fd; char cmd[512]; sprintf(cmd, "%s --dump-runtime-variables", pl); if ( verbose ) printf("\teval `%s`\n", cmd); if ( (fd = popen(cmd, "r")) ) { char buf[256]; while( fgets(buf, sizeof(buf), fd) ) { char name[100]; char value[256]; char *v; if ( sscanf(buf, "%[^=]=%[^;\n]", name, value) == 2 ) { v = value; if ( *v == '"' ) { char *e = ++v; while(*e && *e != '"') e++; while(e>v && isspace(CTOI(e[-1]))) e--; *e = '\0'; } if ( streq(name, "CC") ) defaultProgram(&cc, v); else if ( streq(name, "PLBASE") ) defaultPath(&plbase, v); else if ( streq(name, "PLARCH") ) defaultPath(&plarch, v); else if ( streq(name, "PLLIBS") ) /* Always required. */ pllibs = strdup(v); else if ( streq(name, "PLLIB") ) defaultProgram(&pllib, v); else if ( streq(name, "PLLDFLAGS") ) appendArgList(&ldoptions, v); else if ( streq(name, "PLCFLAGS") ) { appendArgList(&coptions, v); appendArgList(&cppoptions, v); } else if ( streq(name, "PLSOEXT") ) soext = strdup(v); else if ( streq(name, "PLTHREADS") && streq(v, "yes") ) { ensureOption(&coptions, "-D_REENTRANT"); ensureOption(&cppoptions, "-D_REENTRANT"); #ifdef _THREAD_SAFE /* FreeBSD */ ensureOption(&coptions, "-D_THREAD_SAFE"); ensureOption(&cppoptions, "-D_THREAD_SAFE"); #endif } else continue; if ( verbose ) fprintf(stderr, "\t\t%s=\"%s\"\n", name, v); } else { fprintf(stderr, "Unparsed Prolog option: %s\n", buf); } } pclose(fd); #if defined(__WINDOWS__) && defined(HOST_OS_WINDOWS) sprintf(buf, "%s/bin/%s", plbase, PROG_PL); #else sprintf(buf, "%s/bin/%s/%s", plbase, plarch, PROG_PL); #endif defaultPath(&plexe, buf); } else { fprintf(stderr, "%s: failed to run %s: %s", plld, cmd, oserror()); error(1); } }
// Fonction pour charger une texture à partir d'un fichier *.bmp int LoadBMP(char *File) { unsigned char *Data; FILE *fichier; unsigned char Header[0x36]; GLuint DataPos,DataSize; GLint Components; GLsizei Width,Height; GLenum Format,Type; GLuint Name[1]; //Lit le fichier et son header fichier = fopen(File,"rb");if (!fichier) return -1; if (fread(Header,1,0x36,fichier)!=0x36) EXIT; if (Header[0]!='B' || Header[1]!='M') EXIT; if (CTOI(Header[0x1E])!=0) EXIT; if (CTOI(Header[0x1C])!=24) EXIT; //Récupère les infos du fichier DataPos = CTOI(Header[0x0A]); DataSize = CTOI(Header[0x22]); //Récupère les infos de l'image Width = CTOI(Header[0x12]); Height = CTOI(Header[0x16]); Type = GL_UNSIGNED_BYTE; Format = GL_RGB; Components = 3; //!!!! if (DataSize==0) DataSize=Width*Height*Components; if (DataPos==0) DataPos=0x36; //Charge l'image fseek(fichier,DataPos,0); Data = new unsigned char[DataSize]; if (!Data) EXIT; if (fread(Data,1,DataSize,fichier)!=DataSize) { delete Data; fclose(fichier); return -1; } fclose(fichier); //Inverse R et B unsigned char t; for (int x=0;x<Width*Height;x++) { t=Data[x*3]; Data[x*3]=Data[x*3+2]; Data[x*3+2]=t; } //Envoie la texture à OpenGL glPixelStorei(GL_UNPACK_ALIGNMENT,1); glGenTextures(1, Name); glBindTexture(GL_TEXTURE_2D, Name[0]); glTexImage2D ( GL_TEXTURE_2D, //target 0, //mipmap level Components, //nb couleurs Width, //largeur Height, //hauteur 0, //largeur du bord Format, //type des couleurs Type, //codage de chaque composante Data //Image ); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT); glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST); return Name[0]; }
static int strtoint(const char *s_arg, uint64_t *out, uint32_t base, int sign) { const unsigned char *s = (const unsigned char *)s_arg; uint64_t val = 0; uint64_t multmax; unsigned c, i; int neg = 0; int bad_digit = 0; int bad_char = 0; int overflow = 0; if (s == NULL || base == 1 || base > MAX_BASE) { uu_set_error(UU_ERROR_INVALID_ARGUMENT); return (-1); } while ((c = *s) != 0 && isspace(c)) s++; switch (c) { case '-': if (!sign) overflow = 1; /* becomes underflow below */ neg = 1; /*FALLTHRU*/ case '+': c = *++s; break; default: break; } if (c == '\0') { uu_set_error(UU_ERROR_EMPTY); return (-1); } if (base == 0) { if (c != '0') base = 10; else if (s[1] == 'x' || s[1] == 'X') base = 16; else base = 8; } if (base == 16 && c == '0' && (s[1] == 'x' || s[1] == 'X')) c = *(s += 2); if ((val = CTOI(c)) >= base) { if (IS_DIGIT(c)) bad_digit = 1; else bad_char = 1; val = 0; } multmax = (uint64_t)UINT64_MAX / (uint64_t)base; for (c = *++s; c != '\0'; c = *++s) { if ((i = CTOI(c)) >= base) { if (isspace(c)) break; if (IS_DIGIT(c)) bad_digit = 1; else bad_char = 1; i = 0; } if (val > multmax) overflow = 1; val *= base; if ((uint64_t)UINT64_MAX - val < (uint64_t)i) overflow = 1; val += i; } while ((c = *s) != 0) { if (!isspace(c)) bad_char = 1; s++; } if (sign) { if (neg) { if (val > -(uint64_t)INT64_MIN) overflow = 1; } else { if (val > INT64_MAX) overflow = 1; } } if (neg) val = -val; if (bad_char | bad_digit | overflow) { if (bad_char) uu_set_error(UU_ERROR_INVALID_CHAR); else if (bad_digit) uu_set_error(UU_ERROR_INVALID_DIGIT); else if (overflow) { if (neg) uu_set_error(UU_ERROR_UNDERFLOW); else uu_set_error(UU_ERROR_OVERFLOW); } return (-1); } *out = val; return (0); }