t_ray find_refract_vect(t_ray *start_ray, t_hit drawn_pixel, double c_r, int test) { double ref_refract; double new_ref_index; t_ray res; t_vec new_norm; t_vec inv_dir; res.pos = vec_add(start_ray->pos, scalar_product(start_ray->dir, drawn_pixel.t)); inv_dir = scalar_product(start_ray->dir, -1); drawn_pixel.point_norm = scalar_product(drawn_pixel.point_norm, test); ref_refract = dot_product(drawn_pixel.point_norm, inv_dir); ref_refract /= (get_length(drawn_pixel.point_norm) * get_length(inv_dir)); new_ref_index = 1 - c_r * c_r * (1 - ref_refract * ref_refract); if (new_ref_index > 0) { new_ref_index = sqrt(new_ref_index); new_ref_index = c_r * ref_refract - new_ref_index; new_norm = scalar_product(drawn_pixel.point_norm, new_ref_index); res.dir = scalar_product(inv_dir, c_r); res.dir = vec_sub(res.dir, new_norm); } else res.dir = init_vector(0, 0, 0); return (res); }
int main (int argc, char *argv[]) // accumulative ET (Apr - Jul) for each year [km3] { int i,j,nfiles,ndays; int syear,year,month,day,y; double tot_area; double lat,lon,cellsize,area; double prec,evap,runoff,baseflow; double airT,sm1,sm2,sm3,swe; double evap_year[MAXY]; char filename[MAXC]; FILE *fpin,*fplist,*fpout; if(argc!=4){ printf("Usage: %s latlonlist cellsize outfile\n",argv[0]); exit(0); } if((fplist = fopen(argv[1],"r"))==NULL){ printf("ERROR: can't open %s\n", argv[1]); exit(0); } nfiles=get_length(fplist); cellsize=atof(argv[2]); for(j=0;j<MAXY;j++){ evap_year[j]=0; } tot_area=0; for(i=0;i<nfiles;i++){ fscanf(fplist,"%lf %lf",&lat,&lon); area=calc_area(lat,lon,cellsize); tot_area+=area; sprintf(filename,"/raid2/ymao/other/CA_drought_0.0625/run_vic/model_output/fluxes_1920_20140731_old2014_adjustT_P_pivot1920/fluxes_%.5f_%.5f",lat,lon); if((fpin = fopen(filename,"r"))==NULL){ printf("ERROR: can't open %s\n", filename); exit(0); } ndays=get_length(fpin); for(j=0;j<ndays;j++){ fscanf(fpin,"%d %d %d",&year,&month,&day); if(j==0)syear=year; fscanf(fpin,"%lf %lf %lf %lf",&prec,&evap,&runoff,&baseflow); fscanf(fpin,"%lf %lf %lf %lf %lf",&airT,&sm1,&sm2,&sm3,&swe); if(month>=4 && month<=7) // if Apr-Jul, add it to this water year { evap_year[year-syear]+=evap*area/1000/1000; } } fclose(fpin); } fclose(fplist); if((fpout = fopen(argv[3],"w"))==NULL){ printf("ERROR: can't open %s\n", argv[3]); exit(0); } for(y=syear;y<=year;y++){ // print water years 1920-2014 fprintf(fpout,"%d %.4f\n",y,evap_year[y-syear]); } fclose(fpout); printf("%.1f\n",tot_area); return(0); }
static int document_length(lua_State *L) { if (lua_isuserdata(L, 3)) { document doc = lua_touserdata(L,3); return get_length(doc); } if (lua_istable(L,3)) { int total = 0; int s = lua_rawlen(L,3); int i; for (i=1;i<=s;i++) { lua_rawgeti(L, 3, i); document doc = lua_touserdata(L,-1); if (doc == NULL) { lua_pop(L,1); return luaL_error(L, "Invalid document at %d", i); } else { total += get_length(doc); lua_pop(L,1); } } return total; } return luaL_error(L, "Insert need documents"); }
int sort(unsigned int *a, int len) { int i = 0; int j = 0; int tmp = 0; int tmp2 = 0; for (i = 0; i < len-1; i++) { tmp = i; for (j = i+1; j < len; j++) { if (get_length(a[tmp])>get_length(a[j])) { tmp = j; } else if (get_length(a[tmp]) == get_length(a[j])) { if (a[tmp] > a[j]) { tmp =j; } } } tmp2 = a[i]; a[i] = a[tmp]; a[tmp] = tmp2; } return 0; }
/* Deze methode zet het blok op index op vrij, indien mogelijk fuseert het * met omringende vrije blokken tot een groot vrij blok. */ void free_block(long index){ long prev = get_prev(index); long next = get_next(index); if(!get_free(index)){ /* Zet het blok op vrij. */ set_free(index, 1); mem[0] -= get_length(index); mem[1] -= ADMIN_SIZE; } /* Voeg vorige blok samen met het huidige als deze vrij is als een groot * vrij blok. */ if(prev != 0 && get_free(prev)){ set_length(prev, get_length(prev) + get_length(index) + ADMIN_SIZE); set_next(prev, next); if(next != 0){ set_prev(next, prev); } } /* Voeg volgende blok samen met het huidige als deze vrij is als een * groot vrij blok. */ if(next != 0 && get_free(next)){ free_block(next); } }
/* * Write an ASCII disassembly of one instruction at "pc" * in "RAM" into "line" (max length "max_line"), return * number of bytes consumed. */ int arch_pdp11_disasm_instr(cpu_t *cpu, addr_t pc, char *line, unsigned int max_line) { uint16_t opcode = cpu->RAM[pc]; char line2[8]; int addmode = get_addmode(opcode); if (addmode == ADDMODE_BRA) { snprintf(line2, sizeof(line2), "$%02" PRIX64, pc+2 + (int8_t)cpu->RAM[pc+1]); } else { switch (get_length(addmode)) { case 1: snprintf(line2, sizeof(line2), addmode_template[addmode], 0); break; case 2: snprintf(line2, sizeof(line2), addmode_template[addmode], cpu->RAM[pc+1]); break; case 3: snprintf(line2, sizeof(line2), addmode_template[addmode], cpu->RAM[pc+1] | cpu->RAM[pc+2]<<8); break; default: printf("Table error at %s:%d\n", __FILE__, __LINE__); exit(1); } } snprintf(line, max_line, "%s %s", mnemo[get_instr(opcode)], line2); return get_length(get_addmode(cpu->RAM[pc])); }
long mem_get(long request){ long l = request; long lidx = 0; long idx = 2; while(idx != 0){ if(get_length(idx) >= l && get_free(idx)){ /* Gat gevonden waar de aanvraag in past. */ lidx = idx; l = get_length(idx); break; } idx = get_next(idx); } if(lidx == 0){ /* Geen gat groot genoeg. */ return -1; }else if(l == request){ /* Gat precies groot genoeg. */ mem[0] += request; mem[1] += ADMIN_SIZE; set_free(lidx, 0); return lidx + ADMIN_SIZE; }else{ /* Alleen een gat > request. */ return split_block(lidx, request); } }
void test8(){ char wort[10] = ""; strcpy(wort, "12345\n"); printf("%i\n", get_length(wort)); char wort2[6] = ""; strncpy(wort2, wort, 5); printf("%i\n", get_length(wort2)); }
/** * @brief This function is called when a step of the trajectory just occured. * * If updates the height of the jump when a step of the basic movement is made. * * @param step_index index of the step in the trajectory (the first one is 0) * @param success true if the move was made, false if there was an obstacle */ void JumpMovement::notify_step_done(int step_index, bool success) { if (step_index == 1 || step_index == get_length()) { jump_height = 0; } else { int jump_sign = (step_index <= get_length() / 2) ? 1 : -1; int jump_unit = (get_length() <= 16) ? 2 : 1; jump_height += jump_sign * jump_unit; } }
int solve(char *str1, char *str2) { int length1 = get_length(str1); int length2 = get_length(str2); if(length1 != length2) return 0; int i; for(i=0; i < length1; ++i) { if(str1[i] != str2[i]) return 0; } return 1; }
void file_info::to_formatter(pfc::string_formatter& out) const { out << "File info dump:\n"; if (get_length() > 0) out<< "Duration: " << pfc::format_time_ex(get_length(), 6) << "\n"; pfc::string_formatter temp; for(t_size metaWalk = 0; metaWalk < meta_get_count(); ++metaWalk) { meta_format_entry(metaWalk, temp); out << "Meta: " << meta_enum_name(metaWalk) << " = " << temp << "\n"; } for(t_size infoWalk = 0; infoWalk < info_get_count(); ++infoWalk) { out << "Info: " << info_enum_name(infoWalk) << " = " << info_enum_value(infoWalk) << "\n"; } }
//returns i < j bool angle_compare(Point2D pole, Point2D i, Point2D j) { if(pole == i) return 1; if(pole == j) return 0; if(is_left(pole, i , j) == 0) { if( (i.y > pole.y && j.y > pole.y) || (i.y < pole.y && j.y < pole.y) ) { return (get_length(i, pole) < get_length(j, pole)); } else if(i.y == pole.y && j.y == pole.y) { if( (i.x > pole.x && j.x > pole.x) || (i.x < pole.x && j.x < pole.y) ) return (get_length(i, pole) < get_length(j, pole)); else return (i.x < j.x); } else { return (i.y > j.y); } } else { if(is_left(pole, i, j) > 0) { if(i.y >= pole.y) return 1; else { if(j.y < pole.y) return 1; else return 0; } } else { if(i.y < pole.y) return 0; else { if(j.y >= pole.y) return 0; else return 1; } } } }
std::string cassette_image_device::call_display() { const int ANIMATION_FPS = 1; std::string result; // only show the image when a cassette is loaded and the motor is on if (exists() && is_motor_on()) { int n; double position, length; cassette_state uistate; static const char *shapes[] = { u8"\u2500", u8"\u2572", u8"\u2502", u8"\u2571" }; // figure out where we are in the cassette position = get_position(); length = get_length(); uistate = (cassette_state)(get_state() & CASSETTE_MASK_UISTATE); // choose which frame of the animation we are at n = ((int)position / ANIMATION_FPS) % ARRAY_LENGTH(shapes); // play or record const char *status_icon = (uistate == CASSETTE_PLAY) ? u8"\u25BA" : u8"\u25CF"; // Since you can have anything in a BDF file, we will use crude ascii characters instead result = string_format("%s %s %02d:%02d (%04d) [%02d:%02d (%04d)]", shapes[n], // animation status_icon, // play or record ((int)position / 60), ((int)position % 60), (int)position, ((int)length / 60), ((int)length % 60), (int)length); // make sure tape stops at end when playing if ((m_state & CASSETTE_MASK_UISTATE) == CASSETTE_PLAY) { if (m_cassette) { if (get_position() > get_length()) { m_state = (cassette_state)((m_state & ~CASSETTE_MASK_UISTATE) | CASSETTE_STOPPED); } } } } return result; }
int main(int argc, char **argv) { path *pathlist; hash *hashlist; int *visited; int len = 0, i, j, inc_arrays; /* get total number of files to compare and store in len */ if (argc < 2) get_length(".", 0, &len); else for(i=1; i<argc; i++) get_length(argv[i], 0, &len); if((pathlist = (path *)malloc(len*sizeof(path)))==NULL) { printf("Error allocating space. Program ending now.\n"); return 1; } if((hashlist = (hash *)malloc(len*sizeof(hash)))==NULL){ printf("Error allocating space. Program ending now.\n"); return 1; } if((visited = (int *)malloc(len*sizeof(int)))==NULL) { printf("Error allocating space. Program ending now.\n"); return 1; } /* fill arrays */ inc_arrays=0; if (argc < 2) get_data(".", 0, &inc_arrays, visited, pathlist, hashlist); else { for(i=1; i<argc; i++) { get_data(argv[i], 0, &inc_arrays, visited, pathlist, hashlist); } } for(i=0; i<len; i++) { visited[i]=1; for(j=i+1; j<len; j++) if(!visited[j]) if(strcmp(hashlist[i], hashlist[j])==0) { printf("%s %s\n", pathlist[i], pathlist[j]); visited[j]=1; } } free(pathlist); free(hashlist); free(visited); return 0; }
uint64_t project(int64_t* any_val, uint8_t* ty_ser) { #ifndef NDEBUG if (is_forwarding(*any_val)) { printf("project(): Vector argument (%p) is an indirection to %" PRIi64 "\n", any_val, *any_val); fflush(stdout); exit(EXIT_FAILURE); } if (!((void*)any_val >= (void*)fromspace_begin && (void*)any_val < (void*)fromspace_end)) { printf("project(): Vector argument is not in correct heap: %p\n", any_val); fflush(stdout); exit(EXIT_FAILURE); } #endif // special case for (project any (Vectorof Any)) if (*(ty_ser + 1) == 0b00000111 && (((uint8_t) *(any_val + 2)) & 0b111) == 0b00000010) { int any_vec_len = get_length(*any_val); return *(any_val + any_vec_len); } // length of top-level serialization uint8_t ser_len = *ty_ser; // length of serialization in the vec uint8_t ser_vec_len = (uint8_t)(*(any_val + 1)); if (ser_len != ser_vec_len) { // TODO: fail with a helpful error message printf("project(): Types are not equal.\n"); fflush(stdout); exit(EXIT_FAILURE); } if (memcmp((void*)(any_val + 2), (void*)(ty_ser + 1), (size_t)ser_len) != 0) { // TODO: fail with a helpful error message printf("project(): Types are not equal.\n"); fflush(stdout); exit(EXIT_FAILURE); } // Type of Any is the type we expect, just read the field. int any_vec_len = get_length(*any_val); return *(any_val + any_vec_len); }
static int op_query(lua_State *L) { int id = luaL_checkinteger(L,1); document query = lua_touserdata(L,6); if (query == NULL) { return luaL_error(L, "require query document"); } document selector = lua_touserdata(L,7); int flags = luaL_checkinteger(L, 2); size_t nsz = 0; const char *name = luaL_checklstring(L,3,&nsz); int skip = luaL_checkinteger(L, 4); int number = luaL_checkinteger(L, 5); luaL_Buffer b; luaL_buffinit(L,&b); struct buffer buf; buffer_create(&buf); int len = reserve_length(&buf); write_int32(&buf, id); write_int32(&buf, 0); write_int32(&buf, OP_QUERY); write_int32(&buf, flags); write_string(&buf, name, nsz); write_int32(&buf, skip); write_int32(&buf, number); int32_t query_len = get_length(query); int total = buf.size + query_len; int32_t selector_len = 0; if (selector) { selector_len = get_length(selector); total += selector_len; } write_length(&buf, total, len); luaL_addlstring(&b, (const char *)buf.ptr, buf.size); buffer_destroy(&buf); luaL_addlstring(&b, (const char *)query, query_len); if (selector) { luaL_addlstring(&b, (const char *)selector, selector_len); } luaL_pushresult(&b); return 1; }
/* considered a failure. */ int atomic_log_send() { char *buf; list *current; int len; if (fd < 0) { errno = EINVAL; return -1; } len = get_length(); if (len == 0) return 0; buf = (char *)malloc(len); if (buf == NULL) return -1; current = first; len = 0; while (current != NULL) { (void)memcpy(buf+len, current->entry, current->len); len += current->len; current = current->next; } if (my_write(fd, buf, len) != len) { free(buf); errno = EAGAIN; return -1; } free(buf); clear(); return 0; }
static gint compare_mapping_entry (gconstpointer user_data, gconstpointer data) { const struct mapping_entry *entry = data; const gunichar *key = user_data; gunichar src_0; G_STATIC_ASSERT(MAX_KEY_SIZE == 2); src_0 = get_src_char (src_table, entry->src, 0); if (key[0] > src_0) return 1; else if (key[0] < src_0) return -1; if (get_length (entry->src) > 1) { gunichar src_1; src_1 = get_src_char (src_table, entry->src, 1); if (key[1] > src_1) return 1; else if (key[1] < src_1) return -1; } else if (key[1]) return 1; return 0; }
/* ECMA-262 3rd Edition 15.4.4.7 */ static HRESULT Array_push(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei) { jsdisp_t *jsthis; DWORD length = 0; int i, n; HRESULT hres; TRACE("\n"); hres = get_length(ctx, vthis, ei, &jsthis, &length); if(FAILED(hres)) return hres; n = arg_cnt(dp); for(i=0; i < n; i++) { hres = jsdisp_propput_idx(jsthis, length+i, get_arg(dp, i), ei); if(FAILED(hres)) return hres; } hres = set_length(jsthis, ei, length+n); if(FAILED(hres)) return hres; if(retv) { V_VT(retv) = VT_I4; V_I4(retv) = length+n; } return S_OK; }
/* ECMA-262 3rd Edition 15.4.4.5 */ static HRESULT Array_join(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, DISPPARAMS *dp, VARIANT *retv, jsexcept_t *ei) { jsdisp_t *jsthis; DWORD length; HRESULT hres; TRACE("\n"); hres = get_length(ctx, vthis, ei, &jsthis, &length); if(FAILED(hres)) return hres; if(arg_cnt(dp)) { BSTR sep; hres = to_string(ctx, get_arg(dp,0), ei, &sep); if(FAILED(hres)) return hres; hres = array_join(ctx, jsthis, length, sep, retv, ei); SysFreeString(sep); }else { hres = array_join(ctx, jsthis, length, default_separatorW, retv, ei); } return hres; }
/* 1 string collection 2 integer single remove 3 document selector return string package */ static int op_delete(lua_State *L) { document selector = lua_touserdata(L,3); if (selector == NULL) { luaL_error(L, "Invalid param"); } size_t sz = 0; const char * name = luaL_checklstring(L,1,&sz); luaL_Buffer b; luaL_buffinit(L,&b); struct buffer buf; buffer_create(&buf); int len = reserve_length(&buf); write_int32(&buf, 0); write_int32(&buf, 0); write_int32(&buf, OP_DELETE); write_int32(&buf, 0); write_string(&buf, name, sz); write_int32(&buf, lua_tointeger(L,2)); int32_t selector_len = get_length(selector); int total = buf.size + selector_len; write_length(&buf, total, len); luaL_addlstring(&b, (const char *)buf.ptr, buf.size); buffer_destroy(&buf); luaL_addlstring(&b, (const char *)selector, selector_len); luaL_pushresult(&b); return 1; }
/* ECMA-262 3rd Edition 15.4.4.7 */ static HRESULT Array_push(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r) { jsdisp_t *jsthis; DWORD length = 0; unsigned i; HRESULT hres; TRACE("\n"); hres = get_length(ctx, vthis, &jsthis, &length); if(FAILED(hres)) return hres; for(i=0; i < argc; i++) { hres = jsdisp_propput_idx(jsthis, length+i, argv[i]); if(FAILED(hres)) return hres; } hres = set_length(jsthis, length+argc); if(FAILED(hres)) return hres; if(r) *r = jsval_number(length+argc); return S_OK; }
ListNode* rotateRight(ListNode* head, int k) { if (head == NULL) { return head; } int length = get_length(head); k = k % length; if (k == 0) { return head; } ListNode* start = head; ListNode* end = head; while (k > 0) { end = end->next; --k; } while (end->next != NULL) { start = start->next; end = end->next; } ListNode* rotated_head = start->next; start->next = NULL; end->next = head; return rotated_head; }
/*接收应答报文*/ int read_ack(int sockid, struct comm_data *buf) { int readlen; int msglen; readlen=readnet(sockid,(char *)buf, 8); if(readlen!=8) { fprintf(logfp,"readnet(%d,0,8)=%d failed\n",sockid, readlen); return -1; } if(memcmp(buf->flag, MSGFLAG, 4)) { buf->msglen[0]=0; fprintf(logfp,"MSGFLAG is incorrect [%s]!\n",tcpbuf.flag); return -2; } msglen = get_length(buf->msglen,4); if(msglen<32) { fprintf(logfp,"MSGLEN is incorrect[%04d]\n",msglen); return -3; } readlen = readnet(sockid, ((char *)buf)+8, msglen-8); if(readlen != msglen-8) { fprintf(logfp,"readnet(%d,8,%d)=%d failed!\n",connid,msglen-8,readlen); return -4; } printf("\nRCV:%s[%d]\n",(char *)&tcpbuf,msglen); return 0; }
void MPD_CCIT_T6::read(U_INT1 * res) { _line++; _vals = res; Huff_Ccitt_2D_T6::read(_bin); if (_vals) { for (INT i=0 ; i<_tx ; i++) _vals[i] = (_bin[i] ? 0 : _vmax); } _bin[-1] = 0; _bin[_tx] = 0; _bin[_tx+1] = 1; INT a0 = -1; for(;;) { a0 = end_pl_white(a0); if (a0 >= _tx) { return; } while(getbit()) { a0 = get_plage_gray(a0,false); a0 = a0 +get_length(0); } get_plage_gray(a0,false); while (_bin[a0]) a0++; get_plage_gray(a0,true); } }
void delete_at(int id) { if (((id + 1) > get_length()) || id < 0) { printf("Eroare: index indicat in afara diapazonului.\n\n"); return; } int i = -1; Node* tmp = root; Node* prev; while (root->next != NULL) { prev = root; root = root->next; i++; if (i == id) { if (root->next != NULL) { prev->next = root->next; free(root); root = prev->next; } else { prev->next = NULL; free(root); break; } } } root = tmp; save_to_file(); printf("Sters cu success!!"); }
IDL4_INLINE CORBA_long directory_resolve_implementation(CORBA_Object _caller, CORBA_long objID, CORBA_long size, CORBA_char *name, l4_threadid_t *dsvrID, CORBA_long *dobjID, idl4_server_environment *_env) { if (!mounted) return ESUPP; int file_length = get_length(objID); // length of the file char *buffer; read(objID,0,file_length,&buffer); // parsing buffer ext2_dir_entry *parse; parse = (ext2_dir_entry*)buffer; int count = 0; char str[80]; name [size]=0; while (count <file_length){ strncpy(str,parse->name,parse->name_length&0xff); if(!strcmp(str,name)){ *dsvrID=l4_myself(); *dobjID=parse->inode; return ESUCCESS; } count += (int)parse->rec_length; parse=(ext2_dir_entry*)(((char*)parse)+parse->rec_length); // really fine pointers } return ENOTFOUND; }
void PinballMovement::set_direction(int value) { float dist = get_length(x_speed, y_speed); get_dir(value, x_speed, y_speed); x_speed *= dist; y_speed *= dist; }
/* Deze methode wijst length woorden toe aan een proces. De overgebleven * woorden worden gezien als nieuw vrij blok. */ int split_block(long index, long length){ long blockleng = get_length(index); long newidx = index + length + ADMIN_SIZE; long newleng = blockleng - length - ADMIN_SIZE; if(blockleng < length + ADMIN_SIZE + 1){ /* Geen ruimte voor een nieuw blok van minimaal 1 woord. */ return -1; } /* Maak het nieuwe blok. Plaats deze na 'length' woorden. */ new_block(newidx, newleng, index, get_next(index)); /* Als het huidige blok een volgende blok heeft moet de pointer van * dat blok welke naar zijn vorige blok wijst naar het nieuwe blok * gezet worden.*/ if(get_next(index) != 0){ set_prev(get_next(index), newidx); } /* Zet het volgende blok van het huidige blok naar het nieuwe blok. */ set_next(index, newidx); /* Zet de length van het huidige blok en zet hem op toegewezen. */ set_length(index, length); set_free(index, 0); /* Verhoog het aantal loze woorden. */ mem[1] += ADMIN_SIZE; /* Verhoog het aantal toegewezen woorden. */ mem[0] += length; /* De index waar begonnen mag worden met schrijven is het blok index * plus de lengte van de administratie. */ return index + ADMIN_SIZE; }
/* ECMA-262 3rd Edition 15.4.4.5 */ static HRESULT Array_join(script_ctx_t *ctx, vdisp_t *vthis, WORD flags, unsigned argc, jsval_t *argv, jsval_t *r) { jsdisp_t *jsthis; DWORD length; HRESULT hres; TRACE("\n"); hres = get_length(ctx, vthis, &jsthis, &length); if(FAILED(hres)) return hres; if(argc) { const WCHAR *sep; jsstr_t *sep_str; hres = to_flat_string(ctx, argv[0], &sep_str, &sep); if(FAILED(hres)) return hres; hres = array_join(ctx, jsthis, length, sep, r); jsstr_release(sep_str); }else { hres = array_join(ctx, jsthis, length, default_separatorW, r); } return hres; }