int init_option(int nb, char **ar, t_option *op, int *len) { init_op(op); while (--nb > 0) if (!ft_strisdigit(ar[nb])) { if (!ft_strcmp(ar[nb], "-c") || !ft_strcmp(ar[nb], "-C")) op->color++; else if (!ft_strcmp(ar[nb], "-v") || !ft_strcmp(ar[nb], "-V")) op->etape++; else if (!ft_strcmp(ar[nb], "-f") || !ft_strcmp(ar[nb], "-F")) op->finalpile++; else if (!ft_strcmp(ar[nb], "-o") || !ft_strcmp(ar[nb], "-O")) op->nbop++; else return (0); } else { if (!verif_nbr(ar[nb]) || ft_atol(ar[nb]) > 2147483647 || ft_atol(ar[nb]) < -2147483648 || ft_strlen(ar[nb]) > 13) return (0); else (*len)++; } return (1); }
void init_op_pool(void) { /* initialize op pool */ op_pool = new Op [1024]; free_op_num = 1024; active_op_num = 0; uint32_t op_pool_entries = 0; int ii; for (ii = 0; ii < 1023; ii++) { op_pool[ii].op_pool_next = &op_pool[ii+1]; op_pool[ii].op_pool_id = op_pool_entries++; init_op(&op_pool[ii]); } op_pool[ii].op_pool_next = op_pool_free_head; op_pool[ii].op_pool_id = op_pool_entries++; init_op(&op_pool[ii]); op_pool_free_head = &op_pool[0]; }
int main() { pthread_t server_th; pthread_t client_ths[CLIENT_COUNT]; init_op(&operation); pthread_mutex_init(&print_mutex, NULL); pthread_create(&server_th, NULL, bank_server, NULL); for(int i = 0; i < CLIENT_COUNT; ++i) { int *idx = (int*)malloc(sizeof(int)); *idx = i; init_answer(&answers[i]); pthread_create(&client_ths[i], NULL, bank_client, idx); } pthread_join(server_th, NULL); return 0; }
Op *get_free_op(void) { /* return a free op from op pool */ if (op_pool_free_head == NULL || (free_op_num == 1)) { std::cout <<"ERROR! OP_POOL SIZE is too small!! " << endl; std::cout <<"please check free_op function " << endl; assert(1); exit(1); } free_op_num--; assert(free_op_num); Op *new_op = op_pool_free_head; op_pool_free_head = new_op->op_pool_next; assert(!new_op->valid); init_op(new_op); active_op_num++; return new_op; }
void text_op_compose2(text_op *result, text_op *op1, text_op *op2) { init_op(result); op_iter iter = {}; text_op_component *op2_c = op2->components; size_t num_op2_c; text_op_component inline_components[2]; if (op2_c == NULL) { op2_c = inline_components; num_op2_c = op2->content.type == NONE ? 0 : 2; inline_components[0].type = SKIP; inline_components[0].num = op2->skip; inline_components[1] = op2->content; } else { num_op2_c = op2->num_components; } for (int i = 0; i < num_op2_c; i++) { switch (op2_c[i].type) { case SKIP: { size_t num = op2_c[i].num; while (num > 0) { text_op_component c = take(op1, &iter, num, DELETE); if (c.type == NONE) { c.type = SKIP; c.num = num; } append(result, c); if (c.type != DELETE) { num -= component_length(&c); } } break; } case INSERT: append(result, op2_c[i]); break; case DELETE: { size_t offset = 0; size_t clen = op2_c[i].num; while (offset < clen) { text_op_component c = take(op1, &iter, clen - offset, DELETE); // If its skip, drop it and decrease length. // If its insert, check the strings match, drop it and decrease length. // If its delete, append it. switch (c.type) { case NONE: c.num = clen - offset; case SKIP: { c.type = DELETE; append(result, c); offset += c.num; break; } case INSERT: // op1 has inserted text, then op2 deleted it again. offset += str_num_chars(&c.str); break; case DELETE: append(result, c); break; } } break; default: assert(0); } } } while (iter.idx < (op1->components ? op1->num_components : 2)) { // The op doesn't have skips at the end. Just copy everything. append(result, take(op1, &iter, SIZE_MAX, NONE)); } }
void text_op_transform2(text_op *result, text_op *op, text_op *other, bool isLefthand) { init_op(result); if (op->components == NULL && op->content.type == NONE) { return; } op_iter iter = {}; text_op_component *other_components = other->components; size_t num_other_components; text_op_component inline_components[2]; if (other_components == NULL) { other_components = inline_components; if (other->content.type == NONE) { num_other_components = 0; } else if (other->skip == 0) { num_other_components = 1; inline_components[0] = other->content; } else { num_other_components = 2; inline_components[0].type = SKIP; inline_components[0].num = other->skip; inline_components[1] = other->content; } } else { num_other_components = other->num_components; } for (int i = 0; i < num_other_components; i++) { if (peek_type(op, iter) == NONE) { break; } switch (other_components[i].type) { case SKIP: { size_t num = other_components[i].num; while (num > 0) { text_op_component c = take(op, &iter, num, INSERT); if (c.type == NONE) { break; } append(result, c); if (c.type != INSERT) { num -= c.num; } } break; } case INSERT: { // If isLeftHand and there's an insert next in the current op, the insert should go first. if (isLefthand && peek_type(op, iter) == INSERT) { // The left insert goes first. append(result, take(op, &iter, SIZE_MAX, NONE)); } if (peek_type(op, iter) == NONE) { break; } text_op_component skip = {SKIP}; skip.num = str_num_chars(&other_components[i].str); append(result, skip); break; } case DELETE: { size_t num = other_components[i].num; while (num > 0) { text_op_component c = take(op, &iter, num, INSERT); switch (c.type) { case NONE: num = 0; break; case SKIP: num -= c.num; break; case INSERT: append(result, c); break; case DELETE: // The delete is unnecessary now. num -= c.num; break; } } break; } default: assert(0); } } while (iter.idx < (op->components ? op->num_components : 2)) { // The op doesn't have skips at the end. Just copy everything. append(result, take(op, &iter, SIZE_MAX, NONE)); } // Trim any trailing skips from the result. if (result->components) { while (result->num_components && result->components[result->num_components - 1].type == SKIP) { result->num_components--; } } }