std::string Color::to_ppm(double max){ std::string ri = ntos(round(r * max)); std::string gi = ntos(round(g * max)); std::string bi = ntos(round(b * max)); return ri + " " + gi + " " + bi; }
std::string Color::to_ppm(double oldmax, double newmax, double gamma){ //step 1: normalize to between 0 and 1 double ri = pow(r / oldmax, 1/gamma) * newmax; double gi = pow(g / oldmax, 1/gamma) * newmax; double bi = pow(b / oldmax, 1/gamma) * newmax; return ntos(round(ri)) + " " + ntos(round(gi)) + " " + ntos(round(bi)); }
void *hamt_delete(struct hamt_root *root, uint128_t *hash) { if (unlikely(ston(root->slot) == NULL)) { return NULL; } struct hamt_state s; s.level = 0; s.ptr[0] = &root->slot; void *found_item = __hamt_search(root, hash, &s); if (unlikely(found_item == NULL)) { return NULL; } if (unlikely(s.level == 0)) { root->slot = (struct hamt_slot){0}; goto done; } struct hamt_slot *found_slot = s.ptr[s.level]; s.level--; struct hamt_node *node = ston(*s.ptr[s.level]); int slice = slice_get(*hash, s.level); if (set_count(node->mask) != 2) { *s.ptr[s.level] = ntos(__hamt_del_node(root, node, slice)); goto done; } else { // set_count == 2 struct hamt_slot other_slot = \ __hamt_get_other_slot(node, found_slot); if(!is_leaf(&other_slot)) { uint64_t other_slice = set_first(set_del(node->mask, slice)); __hamt_free_node(root, node); *s.ptr[s.level] = ntos(__hamt_new_node1(root, other_slice, other_slot)); goto done; } else { while (1) { __hamt_free_node(root, node); if (unlikely(s.level == 0)) { root->slot = other_slot; goto done; } s.level--; node = ston(*s.ptr[s.level]); if (set_count(node->mask) != 1) { break; } } slice = slice_get(*hash, s.level); int slot = set_slot_number(node->mask, slice); node->slots[slot] = other_slot; goto done; } } done: return found_item; }
int FAT::flush() { message("FAT flush\n"); // How many entries fit into a disk block? int entries_per_block = disk.block_size() / sizeof(int); // Save the FAT to disk for (int block_num = 0; block_num < fat_num_blocks(); ++block_num) { int index = entries_per_block * block_num; int entries; if (index + entries_per_block > disk.num_blocks()) entries = disk.num_blocks() - index; else entries = entries_per_block; char buf[MAX_BLOCK_SIZE]; for (int pos = 0; pos < entries; ++pos) ntos(buf + sizeof(int)*pos, entry[index + pos]); if (disk.write(block_num, buf) == DISK_ERROR) return DISK_ERROR; } return DISK_OK; }
static struct hamt_node *__hamt_add_slot(struct hamt_root *root, struct hamt_slot *slot_ptr, void *item, uint128_t *item_hash, int level) { uint64_t slice = slice_get(*item_hash, level); struct hamt_node *old_node = ston(*slot_ptr); int old_size = set_count(old_node->mask); int new_size = old_size + 1; struct hamt_node *node = __hamt_new_node(root, set_add(old_node->mask, slice), new_size); *slot_ptr = ntos(node); int slot = set_slot_number(node->mask, slice); memcpy(&node->slots[0], &old_node->slots[0], sizeof(struct hamt_slot)*slot); memcpy(&node->slots[slot+1], &old_node->slots[slot], sizeof(struct hamt_slot)*(old_size-slot)); node->slots[slot] = item_to_slot(item); __hamt_free_node(root, old_node); return node; }
int RPC::call(const char command[5], int block_num, char *buf) const { if (sock < 0) return DISK_ERROR; char packet[NDISK_BLOCK_SIZE + 8]; // Add the command to the packet header memcpy(packet, command, 4); // Add the block_num parameter to the packet header ntos(packet + 4, block_num); // Add the block to the packet if (buf) memcpy(packet + 8, buf, NDISK_BLOCK_SIZE); size_t len = NDISK_BLOCK_SIZE + 8; char *msg = packet; // Write the packet (with 8-byte header) do { ssize_t nwritten = send(sock, msg, len, 0); if (nwritten < 0) return DISK_ERROR; len -= nwritten; msg += nwritten; } while (len > 0); len = NDISK_BLOCK_SIZE + 8; msg = packet; // Read the packet (with 8-byte header) do { ssize_t nread = recv(sock, msg, len, 0); if (nread <= 0) return DISK_ERROR; len -= nread; msg += nread; } while (len > 0); if (buf){ //buf = (char *) malloc(NDISK_BLOCK_SIZE); //buf = (char*) (packet+8); if(strcmp(command, "WRTE")!=0){ //std::cout<<"buf="<<buf<<", command is "<<command<<std::endl; memcpy(buf, packet + 8, NDISK_BLOCK_SIZE); } //free(buf); } return DISK_OK; }
static void __ohamt_delete(struct ohamt_root *root, uint128_t hash, struct ohamt_state *s) { if (unlikely(s->level == 0)) { root->slot = (struct ohamt_slot){0}; goto done; } struct ohamt_slot *found_slot = s->ptr[s->level]; s->level--; struct ohamt_node *node = ston(*s->ptr[s->level]); int slice = slice_get(hash, s->level); if (set_count(node->mask) != 2) { *s->ptr[s->level] = ntos(__ohamt_del_node(root, node, slice)); goto done; } else { // set_count == 2 struct ohamt_slot other_slot = \ __ohamt_get_other_slot(node, found_slot); if(!is_leaf(&other_slot)) { uint64_t other_slice = set_first(set_del(node->mask, slice)); __ohamt_free_node(root, node); *s->ptr[s->level] = ntos(__ohamt_new_node1(root, other_slice, other_slot)); goto done; } else { while (1) { __ohamt_free_node(root, node); if (unlikely(s->level == 0)) { root->slot = other_slot; goto done; } s->level--; node = ston(*s->ptr[s->level]); if (set_count(node->mask) != 1) { break; } } slice = slice_get(hash, s->level); int slot = set_slot_number(node->mask, slice); node->slots[slot] = other_slot; goto done; } } done: return; }
static void arp_request(struct netdevice * device, uint32_t targetIp) { sbuff * buf = ethernet_sbuff_alloc(sizeof(struct arp_packet)); add_ref(buf); struct arp_packet * arp = (struct arp_packet*)buf->head; arp->htype = ntos(1); arp->ptype = ntos(0x0800); arp->hlen = 6; arp->plen = 4; arp->operation = ntos(1); memcpy(arp->senderMac, device->mac, 6); arp->senderIp = ntol(device->ip); bzero(arp->targetMac, 6); arp->targetIp = ntol(targetIp); ethernet_send(buf, 0x0806, BROADCAST_MAC, device); release_ref(buf, sbuff_free); }
static void reply(struct netdevice* dev, mac target, uint32_t ip, int broadcast) { sbuff * buf = raw_sbuff_alloc(sizeof(struct ethernet_frame) + sizeof(struct arp_packet)); add_ref(buf); sbuff_push(buf, sizeof(struct ethernet_frame)); struct arp_packet * arp = (struct arp_packet*)buf->head; arp->htype = ntos(1); arp->ptype = ntos(0x0800); arp->hlen = 6; arp->plen = 4; arp->operation = ntos(2); memcpy(arp->senderMac, dev->mac, 6); arp->senderIp = ntol(dev->ip); memcpy(arp->targetMac, target, 6); arp->targetIp = ntol(ip); ethernet_send(buf, 0x0806, broadcast ? BROADCAST_MAC : target, dev); release_ref(buf, sbuff_free); }
static void __ohamt_insert_leaf(struct ohamt_root *root, struct ohamt_state *s, uint128_t item_hash, uint64_t item, uint128_t leaf_hash, uint64_t leaf) { int leaf_slice = slice_get(leaf_hash, s->level); int item_slice = slice_get(item_hash, s->level); if (leaf_slice != item_slice) { *s->ptr[s->level] = ntos(__ohamt_new_node2(root, item, item_slice, leaf, leaf_slice)); } else { struct ohamt_node *node = __ohamt_new_node1(root, item_slice, item_to_slot(OHAMT_NOT_FOUND)); *s->ptr[s->level] = ntos(node); s->ptr[s->level + 1] = &node->slots[0]; s->level += 1; __ohamt_insert_leaf(root, s, item_hash, item, leaf_hash, leaf); } }
static inline void __hamt_insert_leaf(struct hamt_root *root, struct hamt_state *s, uint128_t *item_hash, void *item, uint128_t *leaf_hash, void *leaf) { int leaf_slice = slice_get(*leaf_hash, s->level); int item_slice = slice_get(*item_hash, s->level); if (leaf_slice != item_slice) { *s->ptr[s->level] = ntos(__hamt_new_node2(root, item, item_slice, leaf, leaf_slice)); } else { struct hamt_node *node = __hamt_new_node1(root, item_slice, item_to_slot(NULL)); *s->ptr[s->level] = ntos(node); s->ptr[s->level + 1] = &node->slots[0]; s->level += 1; __hamt_insert_leaf(root, s, item_hash, item, leaf_hash, leaf); } }
void arp_packet(struct netdevice* dev, const uint8_t * data) { struct arp_packet * arp = (struct arp_packet*) data; if (ntol(arp->targetIp) != dev-> ip) return; int request = ntos(arp->operation) == 1; if (request) { // store(arp->senderMac, ntol(arp->senderIp)); reply(dev, arp->senderMac, ntol(arp->senderIp), 0); } else { arp_store(arp->senderMac, ntol(arp->senderIp)); } }
void init_screen_size(struct session *ses) { int top, bot; struct winsize screen; top = ses->top_row == 0 ? 1 : ses->top_row; bot = ses->bot_row == 0 ? 0 : ses->rows - ses->bot_row; if (ses == gts) { if (ioctl(0, TIOCGWINSZ, &screen) == -1) { ses->rows = SCREEN_HEIGHT; ses->cols = SCREEN_WIDTH; } else { ses->rows = screen.ws_row; ses->cols = screen.ws_col; } SET_BIT(gtd->flags, TINTIN_FLAG_RESETBUFFER); } else { ses->rows = gts->rows; ses->cols = gts->cols; } ses->top_row = top; ses->bot_row = ses->rows - bot; if (HAS_BIT(ses->flags, SES_FLAG_SPLIT)) { init_split(ses, ses->top_row, ses->bot_row); } check_all_events(ses, SUB_ARG|SUB_SEC, 0, 2, "SCREEN RESIZE", ntos(ses->cols), ntos(ses->rows)); }
struct session *new_session(struct session *ses, char *name, char *command, int pid, int socket) { int cnt = 0; struct session *newsession; push_call("new_session(%p,%p,%p,%d,%d)",ses,name,command,pid,socket); for (newsession = gts ; newsession ; newsession = newsession->next) { if (!strcmp(newsession->name, name)) { display_puts(ses, "THERE'S A SESSION WITH THAT NAME ALREADY."); if (close(socket) == -1) { syserr("close in new_session"); } kill(pid, SIGKILL); pop_call(); return ses; } } newsession = (struct session *) calloc(1, sizeof(struct session)); newsession->name = strdup(name); newsession->command = strdup(command); newsession->pid = pid; newsession->group = strdup(gts->group); newsession->flags = gts->flags; LINK(newsession, gts->next, gts->prev); for (cnt = 0 ; cnt < LIST_MAX ; cnt++) { newsession->list[cnt] = copy_list(newsession, gts->list[cnt], cnt); } newsession->rows = gts->rows; newsession->cols = gts->cols; display_printf(ses, "#TRYING TO LAUNCH '%s' RUNNING '%s'.", newsession->name, newsession->command); gtd->ses = newsession; SET_BIT(newsession->flags, SES_FLAG_CONNECTED|SES_FLAG_RUN); DEL_BIT(newsession->flags, SES_FLAG_LOCALECHO); gtd->ses = newsession; gtd->ses->socket = socket; check_all_events(ses, SUB_ARG|SUB_SEC, 0, 3, "SESSION CONNECTED", ses->name, ses->command, ntos(ses->pid)); pop_call(); return gtd->ses; }
int main(void) { unsigned int score, high_score; char score_s[MAXDIGITNUM+1]; ROMEMU(); USE_UI(); timer_pri_set(0,1); random_init(); lcd_init(); key_init(); timer_init(); sound_init(); timer_set(1,100); timer_start(1); ENINT(); lcd_cursor(0,0); lcd_printstr(" 0:Game Start "); lcd_cursor(0,1); lcd_printstr(" *:High Score "); high_score = 0; lcd_cursor(0,0); while(1) { if (key_check(10) == KEYON){ /* *キー:ハイスコア表示 */ lcd_cursor(0,0); /* LCD にハイスコア表示 */ lcd_printstr(" High Score is "); lcd_cursor(0,1); lcd_printstr(" "); lcd_cursor(0,1); lcd_printstr(ntos(high_score,score_s)); } if (key_check(10) == KEYOFF){ lcd_cursor(0,0); lcd_printstr(" 0:Game Start "); lcd_cursor(0,1); lcd_printstr(" *:High Score "); } if (key_check(11) == KEYON){ /* 0キー:ゲームスタート */ zanki = 5; lcd_cursor(0,0); /* LCD に操作方法表示 */ lcd_printstr("*:Sight 0:Trig."); score = game_start(); /* ゲームスタート */ lcd_cursor(0,1); /* 得点表示欄のクリア */ lcd_printstr(" "); if (score > high_score){ /* ハイスコアのとき */ high_score = score; /* ハイスコア登録 */ lcd_cursor(0,0); /* ハイスコア表示 */ lcd_printstr(" High Score !!! "); lcd_cursor(0,1); lcd_printstr(ntos(high_score,score_s)); } else { /* ハイスコアでないとき*/ lcd_cursor(0,0); /* スコアを表示 */ lcd_printstr(" Your Score ... "); lcd_cursor(0,1); lcd_printstr(ntos(score,score_s)); } n_time = 0; while (n_time < WAITFEWSEC); /* 得点表示後にちょっと待つ */ lcd_cursor(0,0); /* LCD にメッセージ表示 */ lcd_printstr(" 0:Game Start "); lcd_cursor(0,1); lcd_printstr(" *:High Score "); } } }
int sys_status(int n,char *result) { int i, val; char s[30]; PROCESS *p; val=0; *result=0; switch(n) { /* case statusBUDDY : check_mem(); break; */ case statusSEMA : for(i=0;i<NSEMA;i++) if(SEMA_ARRAY[i].s_status == VALID) { strcat(result,"semaphore "); ntos(i,s); strcat(result,s); strcat(result," "); strcat(result,SEMA_ARRAY[i].s_name); strcat(result," count "); n_htos(SEMA_ARRAY[i].s_count,s); strcat(result,s); nl; } break; case statusSLEEP : strcat(result,"sleeping"); nl; p = SLEEP; while(p != NULL) { strcat(result,p->name); strcat(result," "); ntos(p->delay,s); strcat(result,s); nl; p = p->s_next; } break; case statusDESCR : strcat(result,"file descriptor : slot/device"); nl; for(i = 0;i<NFD;i++) if (fdtab[i].libre == FDUSED) { strcat(result,"file desc. "); ntos(i,s); strcat(result,s); strcat(result,"/"); ntos(fdtab[i].device, s); strcat(result,s); nl; } strcat(result,"device descriptor : devnum major/minor idata/odata"); nl; for(i = 0;i<NDEV;i++) { strcat(result,"devnum "); ntos(i,s); strcat(result,s); strcat(result," "); ntos(devtab[i].major,s); strcat(result,s); strcat(result,"/"); ntos(devtab[i].minor,s); strcat(result,s); strcat(result," "); ntos(tty[devtab[i].minor].idata,s); strcat(result,s); strcat(result,"/"); ntos(tty[devtab[i].minor].odata,s); strcat(result,s); nl; } break; case statusPROCS : for(i=0;i<NBPROC;i++) if (PROCS_ARRAY[i].status != INEXISTANT) { ntos(i,s); strcat(result,s); strcat(result," : "); strcat(result,PROCS_ARRAY[i].name); strcat(result," priorité "); ntos(PROCS_ARRAY[i].prio,s); strcat(result,s); switch(PROCS_ARRAY[i].status) { case SUSPENDU : strcat(result," suspendu"); break; case SLEEPING : strcat(result," sleeping"); break; case ACTIVABLE : strcat(result," activable"); break; case INEXISTANT : strcat(result," inexistant"); break; case WAITING : strcat(result," waiting sur "); strcat(result,((SEMAPHORE*) PROCS_ARRAY[i].sem)->s_name); break; } nl; } break; default : strcat(result,"choix non valide"); nl; } return 0; }
int main(int argc, char **argv) { int greeting = TRUE; #ifdef SOCKS SOCKSinit(argv[0]); #endif if (signal(SIGTERM, trap_handler) == BADSIG) { syserr("signal SIGTERM"); } if (signal(SIGSEGV, trap_handler) == BADSIG) { syserr("signal SIGSEGV"); } if (signal(SIGHUP, trap_handler) == BADSIG) { syserr("signal SIGHUP"); } if (signal(SIGABRT, abort_handler) == BADSIG) { syserr("signal SIGTERM"); } if (signal(SIGINT, abort_handler) == BADSIG) { syserr("signal SIGINT"); } if (signal(SIGTSTP, suspend_handler) == BADSIG) { syserr("signal SIGSTOP"); } if (signal(SIGPIPE, pipe_handler) == BADSIG) { syserr("signal SIGPIPE"); } if (signal(SIGWINCH, winch_handler) == BADSIG) { syserr("signal SIGWINCH"); } /* if (getenv("HOME") != NULL) { char filename[256]; sprintf(filename, "%s/%s", getenv("HOME"), HISTORY_FILE); read_history(gts, filename); } */ srand(time(NULL)); if (argc > 1) { int c; while ((c = getopt(argc, argv, "e: G h r: s: t: v")) != EOF) { if (c == 'G') { greeting = FALSE; } } optind = 1; } init_tintin(greeting); if (argc > 1) { int c; optind = 1; while ((c = getopt(argc, argv, "e: G h r: s: t: v")) != EOF) { switch (c) { case 'e': gtd->ses = script_driver(gtd->ses, -1, optarg); break; case 'G': break; case 'h': tintin_printf(NULL, "Usage: %s [OPTION]... [FILE]...", argv[0]); tintin_printf(NULL, ""); tintin_printf(NULL, " -e Execute given command."); tintin_printf(NULL, " -G Don't show the greeting screen."); tintin_printf(NULL, " -h This help section."); tintin_printf(NULL, " -r Read given file."); tintin_printf(NULL, " -t Set given title."); tintin_printf(NULL, " -v Enable verbose mode."); restore_terminal(); exit(1); break; case 'r': gtd->ses = do_read(gtd->ses, optarg); break; case 't': printf("\033]0;%s\007", optarg); break; case 's': srand((unsigned int) atoll(optarg)); break; case 'v': do_configure(gtd->ses, "{VERBOSE} {ON}"); break; default: tintin_printf(NULL, "Unknown option '%c'.", c); break; } } if (argv[optind] != NULL) { gtd->ses = do_read(gtd->ses, argv[optind]); } } check_all_events(gts, SUB_ARG|SUB_SEC, 0, 2, "PROGRAM START", CLIENT_NAME, CLIENT_VERSION); check_all_events(gts, SUB_ARG|SUB_SEC, 0, 2, "SCREEN RESIZE", ntos(gts->cols), ntos(gts->rows)); mainloop(); return 0; }
int RPC::dispatch(Disk& disk) { char buf[NDISK_BLOCK_SIZE]; char packet[NDISK_BLOCK_SIZE + 8]; size_t len = NDISK_BLOCK_SIZE + 8; char *msg = packet; if (disk.block_size() != NDISK_BLOCK_SIZE) return DISK_ERROR; // Read the packet (with 8-byte header) do { ssize_t nread = recv(sock, msg, len, 0); if (nread <= 0) return DISK_ERROR; len -= nread; msg += nread; } while (len > 0); // Get the command from the packet's first four bytes char command[5]; memcpy(command, packet, 4); command[4] = '\0'; // Get the block number parameter from the packet int block_num = ston(packet + 4); if (!strcmp(command, "NUMB")) { // Write num blocks into packet data ntos(packet + 8, disk.num_blocks()); } else if (!strcmp(command, "FRMT")) { disk.format(block_num); } else if (!strcmp(command, "READ")) { disk.read(block_num, buf); memcpy(packet + 8, buf, NDISK_BLOCK_SIZE); } else if (!strcmp(command, "WRTE")) { memcpy(buf, packet + 8, NDISK_BLOCK_SIZE); disk.write(block_num, buf); } else { message1("Unknown RPC command %s\n", command); } message2("RPC command %s(%d)\n", command, block_num); len = NDISK_BLOCK_SIZE + 8; msg = packet; // Write the packet (with 8-byte header) do { ssize_t nwritten = send(sock, msg, len, 0); if (nwritten < 0) return DISK_ERROR; len -= nwritten; msg += nwritten; } while (len > 0); return DISK_OK; }
void cleanup_session(struct session *ses) { push_call("cleanup_session(%p)",ses); if (ses == gtd->update) { gtd->update = ses->next; } UNLINK(ses, gts->next, gts->prev); if (ses->socket) { if (close(ses->socket) == -1) { syserr("close in cleanup"); } if (HAS_BIT(ses->flags, SES_FLAG_RUN)) { kill(ses->pid, SIGKILL); } DEL_BIT(ses->flags, SES_FLAG_CONNECTED); } check_all_events(ses, SUB_ARG|SUB_SEC, 0, 3, "SESSION DISCONNECTED", ses->name, ses->command, ntos(ses->pid)); display_printf(gtd->ses, "#SESSION '%s' DIED.", ses->name); if (ses == gtd->ses) { gtd->ses = newactive_session(); } if (ses->logfile) { fclose(ses->logfile); } if (ses->logline) { fclose(ses->logline); } LINK(ses, gtd->dispose_next, gtd->dispose_prev); pop_call(); return; }