unsigned long fromhex(char source[]) { int digit = 0; unsigned long result = 0; if (source[0] == '0' && (source[1] == 'x' || source[1] == 'X')) { source += 2; } while (1) { char c = source[digit]; if (in_range('0',c,'9')) { result <<= 4; result |= (c - '0'); } else if (in_range('a',c,'f')) { result <<= 4; result |= (c - 'a' + 0xa); } else if (in_range('A',c,'F')) { result <<= 4; result |= (c - 'A' + 0xA); } else { return result; } ++digit; } }
static int parse_nec_bite(ir_event* ev, int delta) { if(ev->stat==0){ if(in_range(delta, 560, 558)){ nec.start=1; }else{ printf("BROKEN PACKAGE: %i\n", delta); return -1; } }else{ if (in_range(delta, 1120-560, 558)){ nec.bites++; }else{ if (in_range(delta, 2240-560, 558)){ int byte=nec.bites/8; int bite=nec.bites%8; nec.byte[byte]|=1<<bite; //printf("BYTE: %i, BITE: %i, CHR: %x\n", byte, bite, nec.byte[byte]); nec.bites++; }else{ printf("BROKEN PACKAGE2: %i (btes: %i)\n", delta, nec.bites); } } } return 0; }
int lovlig_personnr(char *pers_str) { /* Sjekker at personnummeret eller datoen inneholder lovlige tegn og verdier {{{ */ int retval = 1, i; char *p, buf[3]; for (p = pers_str; *p && retval; p++) { if (!isdigit(*p)) { fprintf(stderr, "%s: %s: Kan kun inneholde siffer\n", progname, pers_str); retval = 0; } } if (retval) { strncpy(buf, pers_str, 2); buf[2] = '\0'; i = atoi(buf); if (!in_range(i, 1, 31)) { fprintf(stderr, "%s: %s: Ulovlig dato: \"%s\"\n", progname, pers_str, buf); retval = 0; } strncpy(buf, pers_str+2, 2); buf[2] = '\0'; i = atoi(buf); if (!in_range(i, 1, 12)) { fprintf(stderr, "%s: %s: Ulovlig måned: \"%s\"\n", progname, pers_str, buf); retval = 0; } } return(retval); /* }}} */ } /* lovlig_personnr() */
void event_logic(){ for(ALLEGRO_EVENT event; al_get_next_event(engine_event_queue,&event);){ //Default event handlers switch(event.type){ case ALLEGRO_EVENT_DISPLAY_CLOSE: quit_at_next_frame(); break; case ALLEGRO_EVENT_MOUSE_BUTTON_DOWN: if(event.mouse.button==1){ for(std::list<renderable*>::iterator iter = ENGINE_SCREEN_MANAGER.buttons.begin(); iter != ENGINE_SCREEN_MANAGER.buttons.end(); ++iter){ if( (*iter)->active){ (*iter)->calc_bounds(); if(in_range((*iter)->bounds[0][0],(*iter)->bounds[0][1],mouse_x()) && in_range((*iter)->bounds[1][0],(*iter)->bounds[1][1],mouse_y())){ ((button*)(*iter))->on_click->exec(); } } } } break; case ALLEGRO_EVENT_TIMER: if(event.timer.source==timer) draw_now=true; break; } game_event_hooks(event); } al_rest(0.001); }
void serialize_to(OutputIterator output, BombermanGame& gamestate) { serialize_to(output, static_cast<uint32_t>(gamestate.remaining_time())); for(auto& x : gamestate.current_level) { char deserialized_entity; dispatch(functions( [&](const EmptySpace&){ deserialized_entity = 0; }, [&](const DestructibleWall&){ deserialized_entity = 1; }, [&](const NondestructibleWall&){ deserialized_entity = 2; }, [&](const Bomb&){ deserialized_entity = 3; }), x); *output++ = deserialized_entity; } for(auto& player : gamestate.players) { Point pos = player.position(); assert(in_range(pos.x(), std::numeric_limits<uint16_t>::min(), std::numeric_limits<uint16_t>::max())); assert(in_range(pos.y(), std::numeric_limits<uint16_t>::min(), std::numeric_limits<uint16_t>::max())); serialize_to(output, static_cast<uint16_t>(pos.x())); serialize_to(output, static_cast<uint16_t>(pos.y())); // przerobione pola serialize_to(output, static_cast<uint8_t>(player.move_progress_percent())); assert(in_range(player.direction(), -4, 4)); serialize_to(output, static_cast<uint8_t>(player.direction())); // TODO: bomby serialize_to(output, static_cast<uint8_t>(0)); serialize_to(output, static_cast<uint8_t>(!player.is_hurt())); } }
vm_map_t *get_active_vm_map_by_addr(vm_addr_t addr) { if (in_range(get_user_vm_map(), addr)) return get_user_vm_map(); if (in_range(get_kernel_vm_map(), addr)) return get_kernel_vm_map(); return NULL; }
int main() { int cs; scanf("%d", &cs); while (cs--) { get_position(0); get_position(1); scanf("%d", &n); for (int i = 2; i < n + 2; i++) scanf("%s%d%d", name[i], x + i, y + i); memset(counter, 0, sizeof(int) * (n + 2)); scanf("%d", &m); for (int i = 0; i < m; i++) { scanf("%d", length + i); for (int j = 0; j < length[i]; j++) { char buf[NAME]; scanf("%s", buf); for (int id = 2; id < n + 2; id++) { if (strcmp(name[id], buf) == 0) { line[i][j] = id; pass_by[id][counter[id]] = i; counter[id]++; break; } } } } if (in_range(0, 1, WALK)) { printf("walk there\n"); continue; } for (int i = 2; i < n + 2; i++) distant[i] = n + 1; int back = 0; for (int i = 2; i < n + 2; i++) { if (in_range(0, i, BUS)) { queue[back++] = i; distant[i] = 0; } } for (int front = 0; front < back; front++) { int s = queue[front]; int d = distant[s]; for (int ptr = 0; ptr < counter[s]; ptr++) { int line_id = pass_by[s][ptr]; for (int i = 0; i < length[line_id]; i++) { int t = line[line_id][i]; if (distant[t] > n) { distant[t] = d + 1; queue[back++] = t; } } } } int ret = n + 1; for (int i = 2; i < n + 2; i++) { if (in_range(1, i, BUS) && distant[i] < ret) ret = distant[i]; } if (ret < n + 1) printf("%d\n", ret); else printf("take a taxi\n"); } return 0; }
// // Convert a single Unicode character (in argument wc) into SJIS multibyte // format. The result is placed into the sjis argument. The number of SJIS // bytes is returned by the function. The function returns 0 if the // Unicode character is not mappable from SJIS. // NA_EIDPROC Int32 unicodeToSjisChar(NAWchar wc, unsigned char *sjis, NABoolean allowInvalidCodePoint) { NAWchar t; // single-byte SJIS characters, in [0, 0x7F]. The Unicode range is // also [0, 0x007F] if ( in_range(wc, 0, 0x007F) ) { sjis[0] = (unsigned char)wc; return 1; } // single-byte SJIS characters, in [0xA1, 0xDF]. The Unicode range // is [0xFF61, 0xFF9F] if ( in_range(wc, 0xFF61, 0xFF9F) ) { sjis[0] = (unsigned char)(wc - 0xFF61 + 0xA1); return 1; } // double byte if ( binarySearchU2STable(wc, t) && t >= 0x8140 ) { sjis[0] = (unsigned char)((t>>8) & (0xFF)); sjis[1] = (unsigned char)(t & (0xFF)); return 2; }
op_result add_digit(int row, int col, int digit) { if(in_range(row)){ if(in_range(col)){ if(in_range(digit)){ if(puzzle[row][col] != 0){ return OP_OCCUPIED; } if((row_contains(row,digit))||(col_contains(col,digit))){ return OP_ILLEGAL; } if(region_contains(row,col,digit)){ return OP_ILLEGAL; } puzzle[row][col] = digit; return OP_OK; } } } return OP_BADARGS ; }
void configure(FILE *puzzle_file) { char line[5];//line buffer int row; int column; int value; int line_count = 0; while(( fgets(line,5,puzzle_file)) != NULL){ row = line[0]-'0'; column = line[1]-'0'; value = line[2]-'0'; if(!in_range(row) || !in_range(column) || !in_range(value)){ printf("Illegal format in configuration file at line %d ", line_count); exit(1); } if((puzzle[row][column] != 0)||(row_contains(row,value))||(col_contains(column,value))||(region_contains(row,column,value))){ printf("Illegal placement in configuration file at line %d ",line_count); exit(1); } puzzle[row][column] = value; fixed[row][column] = TRUE; line_count += 1; } }
std::pair<size_t, bool> StreamWriter<StreamType>::append_float(double val) { using Float32 = std::numeric_limits<float>; using Float64 = std::numeric_limits<double>; byte b[8]; std::pair<size_t, bool> rtn(0, false); if(in_range(val, Float32::lowest(), Float32::max())) { // g++ 4.9.1 doesn't work well here //const uint32_t val = toBigEndianFloat32(static_cast<float>(val)); float temp = static_cast<float>(val); //Walkaround const uint32_t val = toBigEndianFloat32(temp); //Walkaround std::memcpy(b, &val, 4); write(Marker::Float32); write(b, 4); rtn = std::make_pair(5, true); } else if(in_range(val, Float64::lowest(), Float64::max())) { const uint64_t temp = toBigEndianFloat64(val); std::memcpy(b, &temp, 8); write(Marker::Float64); write(b, 8); rtn = std::make_pair(9, true); } return rtn; }
static int mode_works(fb_mode_t *m, range_t *hfreq, range_t *vfreq, range_t *dotclock) { float h = hsf(m); float v = vsf(m); float d = dcf(m); int ret = 1; mp_msg(MSGT_VO, MSGL_DBG2, "mode %dx%d:", m->xres, m->yres); if (!in_range(hfreq, h)) { ret = 0; mp_msg(MSGT_VO, MSGL_DBG2, " hsync out of range."); } if (!in_range(vfreq, v)) { ret = 0; mp_msg(MSGT_VO, MSGL_DBG2, " vsync out of range."); } if (!in_range(dotclock, d)) { ret = 0; mp_msg(MSGT_VO, MSGL_DBG2, " dotclock out of range."); } if (ret) mp_msg(MSGT_VO, MSGL_DBG2, " hsync, vsync, dotclock ok.\n"); else mp_msg(MSGT_VO, MSGL_DBG2, "\n"); return ret; }
int ads5121_fuse_prog(cmd_tbl_t *cmdtp, int bank, char *fuseno_bitno) { iim512x_t *iim = &((immap_t *) CONFIG_SYS_IMMR)->iim; int f, i, bitno; u32 stat, err; f = simple_strtol(fuseno_bitno, NULL, 10); if (f == 0 && fuseno_bitno[0] != '0') f = -1; if (!in_range(f, 0, IIM_FMAX, "<frow> must be between 0-31\n\n", cmdtp->usage)) return 1; bitno = -1; for (i = 0; i < 6; i++) { if (fuseno_bitno[i] == '_') { bitno = simple_strtol(&(fuseno_bitno[i+1]), NULL, 10); if (bitno == 0 && fuseno_bitno[i+1] != '0') bitno = -1; break; } } if (!in_range(bitno, 0, 7, "Bit number ranges from 0-7\n" "Example of <frow_bitno>: \"18_4\" sets bit 4 of row 18\n", cmdtp->usage)) return 1; out_be32(&iim->err, in_be32(&iim->err)); out_be32(&iim->prg_p, IIM_PRG_P_SET); out_be32(&iim->ua, IIM_SET_UA(bank, f)); out_be32(&iim->la, IIM_SET_LA(f, bitno)); #ifdef DEBUG printf("Programming disabled with DEBUG defined \n"); printf(""Set up to pro printf("iim.ua = %x; iim.la = %x\n", iim->ua, iim->la); #else out_be32(&iim->fctl, IIM_FCTL_PROG_PULSE | IIM_FCTL_PROG); do udelay(20); while ((stat = in_be32(&iim->stat)) & IIM_STAT_BUSY); out_be32(&iim->prg_p, 0); err = in_be32(&iim->err); if (stat & IIM_STAT_PRGD) { if (!(err & (IIM_ERR_WPE | IIM_ERR_WPE))) { printf("Fuse is successfully set"); if (err) printf(" - however there are other errors"); printf("\n"); } iim->stat = 0; } if (err) { iim_err_msg(err); out_be32(&iim->err, in_be32(&iim->err)); } #endif }
static inline uint8_t read_HEX(SerdReader* reader) { const uint8_t c = peek_byte(reader); if (is_digit(c) || in_range(c, 'A', 'F') || in_range(c, 'a', 'f')) { return eat_byte_safe(reader, c); } else { return r_err(reader, SERD_ERR_BAD_SYNTAX, "invalid hexadecimal digit `%c'\n", c); } }
void node(const osmium::Node& node) { int x = in_range(0, static_cast<int>((180 + node.location().lon()) * m_factor), m_xsize - 1); int y = in_range(0, static_cast<int>(( 90 - node.location().lat()) * m_factor), m_ysize - 1); int n = y * m_xsize + x; if (m_node_count[n] < std::numeric_limits<node_count_type>::max() - 1) { ++m_node_count[n]; } if (m_node_count[n] > m_max_count) { m_max_count = m_node_count[n]; } }
void adjust_position(int* x, int* y, int width, int height) { GdkScreen *screen; gint mon; GdkRectangle srect; screen = gdk_screen_get_default(); mon = gdk_screen_get_monitor_at_point(screen, *x, *y); gdk_screen_get_monitor_geometry(screen, mon, &srect); LOG("srect: %dx%d+%d+%d", srect.width, srect.height, srect.x, srect.y); *x = in_range(*x, srect.x, srect.x + srect.width - width); *y = in_range(*y, srect.y, srect.y + srect.height - height); }
long double get_intersection(point_t a, point_t b, point_t c, point_t d) { if(cmp(a.x, b.x) || cmp(c.x, d.x)) return 1.0e100; point_t x = a - b, y = c - d; if(cmp(x.x * y.y, x.y * y.x)) return 1.0e100; long double k1 = x.y / x.x, k2 = y.y / y.x; long double b1 = a.y - k1 * a.x, b2 = c.y - k2 * c.x; long double px = (b2 - b1) / (k1 - k2); long double py = k1 * px + b1; if(!in_range(px, a.x, b.x) || !in_range(px, c.x, d.x)) return 1.0e100; if(!in_range(py, a.y, b.y) || !in_range(py, c.y, d.y)) return 1.0e100; return py; }
static bool paraboloid_intersect(struct ipoint *i, const struct ray *ray, const struct shape *s) { double a, b, c, delta, sqrt_delta, k1, k2; a = HR2 * (Vx * Vx + Vz * Vz); if (a < epsilon) { k1 = (HR2 / Vy) * (Sx * Sx + Sz * Sz) - (Sy / Vy); if (k1 > 0. && k1 < i->k && in_range(k1, s, ray)) { set_ipoint(i, s, ray, WHICH_SIDE, k1); return true; } return false; } b = 2. * HR2 * (Vx * Sx + Vz * Sz) - Vy; c = HR2 * (Sx * Sx + Sz * Sz) - Sy; delta = b * b - 4. * a * c; if (delta < 0.) { return false; } else if (delta > 0.) { sqrt_delta = sqrt(delta); k1 = (-b - sqrt_delta) / (2. * a); k2 = (-b + sqrt_delta) / (2. * a); if (k2 <= 0. || i->k <= k1) return false; if (k1 > 0. && in_range(k1, s, ray)) { set_ipoint(i, s, ray, OUTSIDE, k1); return true; } if (k2 < i->k && in_range(k2, s, ray)) { set_ipoint(i, s, ray, INSIDE, k2); return true; } } else { k1 = -b / (2. * a); if (k1 > 0. && k1 < i->k && in_range(k1, s, ray)) { set_ipoint(i, s, ray, WHICH_SIDE, k1); return true; } } return false; }
/* -------------------------------- * PQfsizeGroup - Return the size of a field given * the group index and field index * -------------------------------- */ int PQfsizeGroup(PortalBuffer *portal, int group_index, int field_number) { GroupBuffer *gbp; if (!valid_pointer("PQfsizeGroup: invalid portal pointer", portal) || !in_range("PQfsizeGroup: tuple index", group_index, 0, portal->no_groups)) return(-1); if ((gbp = pbuf_findGroup(portal, group_index)) && in_range("PQfsizeGroup: field number", field_number, 0, gbp->no_fields)) return(gbp->types[field_number].adtsize); return(-1); }
/* -------------------------------- * PQfsize - Return the size of a field * -------------------------------- */ int PQfsize(PortalBuffer *portal, int tuple_index, int field_number) { GroupBuffer *gbp; if (!valid_pointer("PQfsize: invalid portal pointer", portal) || !in_range("PQfsize: tuple index", tuple_index, 0, portal->no_tuples)) return(-1); if ((gbp = PQgroup(portal, tuple_index)) && in_range("PQfsize: field number", field_number, 0, gbp->no_fields)) return(gbp->types[field_number].adtsize); return(-1); }
static int add_audio_codec(struct audio *a, struct sdp_media *m, struct aucodec *ac) { if (!in_range(&a->cfg.srate, ac->srate)) { DEBUG_INFO("skip codec with %uHz (audio range %uHz - %uHz)\n", ac->srate, a->cfg.srate.min, a->cfg.srate.max); return 0; } if (!in_range(&a->cfg.channels, ac->ch)) { DEBUG_INFO("skip codec with %uch (audio range %uch-%uch)\n", ac->ch, a->cfg.channels.min, a->cfg.channels.max); return 0; } return sdp_format_add(NULL, m, false, ac->pt, ac->name, ac->srate, ac->ch, ac->fmtp_ench, ac->fmtp_cmph, ac, false, "%s", ac->fmtp); }
template <typename T> void Array<T>::get (T &item, size_t index) { if (!in_range(index)) throw std::out_of_range("out of range exception"); item = array_[index]; }
// get__contour_breadth_first: apply after boundary is clean to get the ordered boundary // the parameter "once" specifies whether to look for only outer contour or for all the // connected boundary segments (in the case of a multiply connected region void Image::get__contour_breadth_first(bool once) { int i; int front, back; point pt, aux, last = start-1; while ( (pt = get_next_boundary_point(last)) ) { vector<point> V; last = pt; *pt = bound; front = back = 0; V.push_back(pt); ++back; while ( front != back ) { pt = V[front]; for (i=0; i<4; ++i) { aux= pt + offset_4neighbor[i]; if (in_range(aux) && is_8boundary_point(aux)) { *aux=bound; V.push_back(aux); ++back; } } ++front; } unscramble(V, boundary_queue); if (once) break; } }
// get__contour_depth_first: apply after boundary is clean to get the ordered boundary // the parameter "once" specifies whether to look for only outer contour or for all the // connected boundary segments (in the case of a multiply connected region void Image::get__contour_depth_first(bool once) { int i; point pt, aux, first, last = start-1; bool is_on_top; pt = first = get_next_boundary_point(last); while ( pt != 0) { last = first = pt; stack<point> S; *pt = bound; S.push(pt); while ( !S.empty()) { pt=S.top(); is_on_top = true; for (i=0; i<4; ++i) { aux= pt + offset_4neighbor[i]; if (in_range(aux) && is_8boundary_point(aux)) { is_on_top = false; *aux=bound; S.push(aux); break; } } if (is_on_top) { assert( are_4neighbors(pt, first) ); stack_to_queue(S, boundary_queue); } } if (once) break; pt = get_next_boundary_point(last); } }
// get_contour_breadth_first: gets ordered contour using BFS void Image::get_contour_breadth_first(bool once) { int i; int front, back; point pt, aux, last = start-1; filter_by_level(white); while ( (pt = get_next_boundary_point(last)) ) { last = pt; vector<struct node> V; *pt = bound; front = back = 0; V.push_back( make_node(pt, -1) ); ++back; while ( front != back ) { pt = V[front].pt; for (i=0; i<4; ++i) { aux= pt + offset_4neighbor[i]; if (in_range(aux) && is_8boundary_point(aux)) { *aux=bound; V.push_back( make_node(aux, front) ); ++back; } } ++front; } make_ordered(V, boundary_queue, back-1); if (once) break; } erase_points_with_level(bound); erase_small_whites(); color_queue(boundary_queue, bound); }
// get_boundary_depth_first: gets unordered boundary using DFS void Image::get_boundary_depth_first() { int i; bool is_on_top; stack<point> S; point pt, aux, last = start-1; filter_by_level(white); while ( (pt=get_next_boundary_point(last)) ) { last = pt; *pt = bound; S.push(pt); while ( !S.empty()) { pt=S.top(); is_on_top = true; for (i=0; i<4; ++i) { aux= pt + offset_4neighbor[i]; if (in_range(aux) && is_8boundary_point(aux)) { *aux=bound; S.push(aux); is_on_top = false; break; } } if (is_on_top) { S.pop(); boundary_queue.push(pt); } } } }
// get_boundary_breadth_first: gets unordered boundary using BFS void Image::get_boundary_breadth_first() { int i; queue<point> Q; point pt, aux, last = start-1; filter_by_level(white); while ( (pt=get_next_boundary_point(last)) ) { last = pt; *pt = bound; Q.push(pt); while ( !Q.empty()) { pt=Q.front(); for (i=0; i<4; ++i) { aux= pt + offset_4neighbor[i]; if (in_range(aux) && is_8boundary_point(aux)) { *aux=bound; Q.push(aux); } } boundary_queue.push(pt); Q.pop(); } } }
void Image::dilate(int n) { int i; point pt, aux; if (boundary_queue.empty()) { std::cerr << "dilate: given an empty boundary queue!!" << endl; return; } for ( ; n>0; --n) { aux = 0; boundary_queue.push(aux); pt = boundary_queue.front(); while (pt !=0) { for (i=0; i<8; ++i) { aux= pt + offset_8neighbor[i]; if (in_range(aux) && *aux==black) { *aux=bound; boundary_queue.push(aux); } } *pt=white; boundary_queue.pop(); pt = boundary_queue.front(); } boundary_queue.pop(); } }
void Image::euclid_transform() { point pt, aux; vect *vectmatrix; vect *Vaux, *Vpt; vect offsets[8] = {{1,0},{1,1},{0,1},{-1,1},{-1,0},{-1,-1},{0,-1},{1,-1}}; vectmatrix = new vect[size]; double d; int i; for (i=0; i<size; ++i) { if (start[i] == white) { (vectmatrix[i]).x = 1000; // so that distance vector is unrealistically big (vectmatrix[i]).y = 0; } else { (vectmatrix[i]).x = 0; (vectmatrix[i]).y = 0; } } while (!boundary_queue.empty()) { pt = boundary_queue.front(); Vpt = vectmatrix + (pt - start); for (i=0; i<8; ++i) { aux= pt + offset_8neighbor[i]; Vaux = Vpt + offset_8neighbor[i]; if (in_range(aux) && (norm(*Vaux) > (d = norm(sum(*Vpt,offsets[i]))))) { *Vaux = sum(*Vpt,offsets[i]); boundary_queue.push(aux); *aux = floor(d); } } boundary_queue.pop(); } }
void Image::distance_transform() { point pt, aux; grey distance = 1; int i, npix; if (boundary_queue.empty()) { std::cerr << "distance_transform: given an empty boundary queue!!" << endl; return; } while (!boundary_queue.empty()) { ++distance; aux = 0; boundary_queue.push(aux); // null pixel separates layers npix = 0; pt = boundary_queue.front(); while (pt !=0) { for (i=0; i<8; ++i) { aux= pt + offset_8neighbor[i]; if (in_range(aux) && *aux==white) { *aux=distance; boundary_queue.push(aux); } } boundary_queue.pop(); ++ npix; pt = boundary_queue.front(); } if (VERBOSE) std::cout << "distance_transform: " << npix << " pixels removed in iteration " << static_cast<int>(distance-bound) << endl; boundary_queue.pop(); // remove null pixel } }