void main() { int a=10,b=20; #ifdef MAX printf ("the larger one is %d\n",MAXIMUM(a,b)); #else printf ("the lower one is %d\n",MINIMUM(a,b)); #endif #ifndef MIN printf ("the lower one is %d\n",MINIMUM(a,b)); #else printf ("the larger one is %d\n",MAXIMUM(a,b)); #endif #undef MAX #ifdef MAX printf ("the larger one is %d\n",MAXIMUM(a,b)); #else printf ("the lower one is %d\n",MINIMUM(a,b)); #endif #ifndef MIN printf ("the lower one is %d\n",MINIMUM(a,b)); #else printf ("the larger one is %d\n",MAXIMUM(a,b)); #endif }
static void do_geometry(const char *name, const char *spec) { double re_1, im_1; double re_2, im_2; char comma; char sg_1; char sg_2; char ii_1; char ii_2; char ch; #define PLUS_OR_MINUS(c) ((c) == '+' || (c) == '-') #define IMAGINARY_UNIT(x) ((x) == 'i' || (x) == 'j') if (sscanf(spec, "%lf %c %lf %c %c %lf %c %lf %c %c", &re_1, &sg_1, &im_1, &ii_1, &comma, &re_2, &sg_2, &im_2, &ii_2, &ch) != 9 || !PLUS_OR_MINUS(sg_1) || !PLUS_OR_MINUS(sg_2) || !IMAGINARY_UNIT(ii_1) || !IMAGINARY_UNIT(ii_2) || comma != ',') { fprintf(stderr, "invalid geometry specification.\n"); exit(1); } #define MINIMUM(x, y) ((x) <= (y) ? (x) : (y)) #define MAXIMUM(x, y) ((x) >= (y) ? (x) : (y)) #define SIGN(c) ((c) == '-' ? -1.0 : +1.0) /* Sign-adjust. */ im_1 *= SIGN(sg_1); im_2 *= SIGN(sg_2); /* * We have two edges of the rectangle. Now, find the upper-left * (i.e. the one with minimum real part and maximum imaginary * part) and lower-right (maximum real part, minimum imaginary) * corners of the rectangle. */ upper_left_re = MINIMUM(re_1, re_2); upper_left_im = MAXIMUM(im_1, im_2); lower_right_re = MAXIMUM(re_1, re_2); lower_right_im = MINIMUM(im_1, im_2); }
static int input_kex_dh_gex_request(int type, u_int32_t seq, struct ssh *ssh) { struct kex *kex = ssh->kex; int r; u_int min = 0, max = 0, nbits = 0; debug("SSH2_MSG_KEX_DH_GEX_REQUEST received"); if ((r = sshpkt_get_u32(ssh, &min)) != 0 || (r = sshpkt_get_u32(ssh, &nbits)) != 0 || (r = sshpkt_get_u32(ssh, &max)) != 0 || (r = sshpkt_get_end(ssh)) != 0) goto out; kex->nbits = nbits; kex->min = min; kex->max = max; min = MAXIMUM(DH_GRP_MIN, min); max = MINIMUM(DH_GRP_MAX, max); nbits = MAXIMUM(DH_GRP_MIN, nbits); nbits = MINIMUM(DH_GRP_MAX, nbits); if (kex->max < kex->min || kex->nbits < kex->min || kex->max < kex->nbits || kex->max < DH_GRP_MIN) { r = SSH_ERR_DH_GEX_OUT_OF_RANGE; goto out; } /* Contact privileged parent */ kex->dh = PRIVSEP(choose_dh(min, nbits, max)); if (kex->dh == NULL) { sshpkt_disconnect(ssh, "no matching DH grp found"); r = SSH_ERR_ALLOC_FAIL; goto out; } debug("SSH2_MSG_KEX_DH_GEX_GROUP sent"); if ((r = sshpkt_start(ssh, SSH2_MSG_KEX_DH_GEX_GROUP)) != 0 || (r = sshpkt_put_bignum2(ssh, kex->dh->p)) != 0 || (r = sshpkt_put_bignum2(ssh, kex->dh->g)) != 0 || (r = sshpkt_send(ssh)) != 0) goto out; /* Compute our exchange value in parallel with the client */ if ((r = dh_gen_key(kex->dh, kex->we_need * 8)) != 0) goto out; debug("expecting SSH2_MSG_KEX_DH_GEX_INIT"); ssh_dispatch_set(ssh, SSH2_MSG_KEX_DH_GEX_INIT, &input_kex_dh_gex_init); r = 0; out: return r; }
const TreeNode<T>* BST<T>::successor (const T &v) const { const TreeNode<T> *n = search (v); if (n == NULL) throw std::runtime_error ("the searched value does not exsit in the tree"); //if the node has a right child, it's the successor if (n->right != NULL) return n->right->v; else { const TreeNode<T> *c = n; //current node const TreeNode<T> *p = n->p; //the parent of n while ((p!=NULL) && (p->left!=c)) { c = p; p = p->p; } if (p == NULL) { throw std::runtime_error ("the value is the maximum in the tree, node predecessor"); } else { return MAXIMUM(p); } } }
/* * This adds a single item to a scrolling list, before the current item. */ void insertCDKScrollItem (CDKSCROLL *scrollp, const char *item) { int widestItem = WidestItem (scrollp); char *temp = 0; size_t have = 0; if (allocListArrays (scrollp, scrollp->listSize, scrollp->listSize + 1) && insertListItem (scrollp, scrollp->currentItem) && allocListItem (scrollp, scrollp->currentItem, &temp, &have, scrollp->numbers ? (scrollp->currentItem + 1) : 0, item)) { /* Determine the size of the widest item. */ widestItem = MAXIMUM (scrollp->itemLen[scrollp->currentItem], widestItem); updateViewWidth (scrollp, widestItem); setViewSize (scrollp, scrollp->listSize + 1); resequence (scrollp); } freeChecked (temp); }
int utlSmartCatUtW (wchar_t ** str_p, unsigned *size_p, wchar_t * str2, unsigned size2) { wchar_t *str = *str_p; unsigned size = *size_p; if ((NULL == str2) || (NULL == str_p) || (NULL == size_p)) return 1; if (NULL == str) { size = MAXIMUM (128, size2 + 1); str = utlCalloc (sizeof (wchar_t), size); if (!str) return 1; } else if (size < (wcslen ((wchar_t *) str) + size2 + 1)) { wchar_t *p; size = wcslen ((wchar_t *) str) + size2 + 1 + 128; p = utlRealloc (str, size * sizeof (wchar_t)); if (!p) return 1; str = p; } wcsncat ((wchar_t *) str, (wchar_t *) str2, size2); *str_p = str; *size_p = size; return 0; }
void unipoly::add_to(base &x, base &y) { unipoly& px = x.as_unipoly(); unipoly py; base a; INT d1, d2, d3, i; if (s_kind() != UNIPOLY) { cout << "unipoly::add_to() this not a unipoly\n"; exit(1); } if (x.s_kind() != UNIPOLY) { cout << "unipoly::add_to() x is not a unipoly\n"; exit(1); } d1 = degree(); d2 = px.degree(); d3 = MAXIMUM(d1, d2); py.m_l(d3 + 1); a = s_i(0); a.zero(); for (i = 0; i <= d1; i++) { py[i] = s_i(i); } for (; i <= d3; i++) { py[i] = a; } for (i = 0; i <= d2; i++) { py[i] += px.s_i(i); } py.swap(y); }
/* * This adds a single item to a scrolling list, at the end of the list. */ void addCDKScrollItem (CDKSCROLL *scrollp, const char *item) { int itemNumber = scrollp->listSize; int widestItem = WidestItem (scrollp); char *temp = 0; size_t have = 0; if (allocListArrays (scrollp, scrollp->listSize, scrollp->listSize + 1) && allocListItem (scrollp, itemNumber, &temp, &have, scrollp->numbers ? (itemNumber + 1) : 0, item)) { /* Determine the size of the widest item. */ widestItem = MAXIMUM (scrollp->itemLen[itemNumber], widestItem); updateViewWidth (scrollp, widestItem); setViewSize (scrollp, scrollp->listSize + 1); } freeChecked (temp); }
/* * binc -- * Increase the size of a buffer. * * PUBLIC: void *binc(SCR *, void *, size_t *, size_t); */ void * binc(SCR *sp, void *bp, size_t *bsizep, size_t min) { size_t csize; /* If already larger than the minimum, just return. */ if (min && *bsizep >= min) return (bp); csize = *bsizep + MAXIMUM(min, 256); REALLOC(sp, bp, csize); if (bp == NULL) { /* * Theoretically, realloc is supposed to leave any already * held memory alone if it can't get more. Don't trust it. */ *bsizep = 0; return (NULL); } /* * Memory is guaranteed to be zero-filled, various parts of * nvi depend on this. */ memset((char *)bp + *bsizep, 0, csize - *bsizep); *bsizep = csize; return (bp); }
INT &TDO_upper_bound(INT i, INT j) { INT m, bound; if (i <= 0) { cout << "TDO_upper_bound i <= 0, i = " << i << endl; exit(1); } if (j <= 0) { cout << "TDO_upper_bound j <= 0, j = " << j << endl; exit(1); } m = MAXIMUM(i, j); if (TDO_upper_bounds_v_max == -1) { TDO_refine_init_upper_bounds(12); } if (m > TDO_upper_bounds_v_max) { //cout << "I need TDO_upper_bound " << i << "," << j << endl; TDO_refine_extend_upper_bounds(m); } if (TDO_upper_bound_source(i, j) != 1) { //cout << "I need TDO_upper_bound " << i << "," << j << endl; } bound = TDO_upper_bound_internal(i, j); if (bound == -1) { cout << "TDO_upper_bound = -1 i=" << i << " j=" << j << endl; exit(1); } //cout << "PACKING " << i << " " << j << " = " << bound << endl; return TDO_upper_bound_internal(i, j); }
void compress_effect_run(struct effect *e, ssize_t *frames, sample_t *ibuf, sample_t *obuf) { ssize_t i, k, samples = *frames * e->ostream.channels; sample_t s, gain_target; struct compress_state *state = (struct compress_state *) e->data; for (i = 0; i < samples; i += e->ostream.channels) { s = 0; for (k = 0; k < e->ostream.channels; ++k) if (GET_BIT(e->channel_selector, k)) s = MAXIMUM(fabs(ibuf[i + k]), s); if (s > state->thresh) gain_target = pow(10, (state->thresh_db - (20 * log10(s))) * state->ratio / 20); else gain_target = 1.0; if (state->gain > gain_target) { state->gain *= state->attack; if (state->gain < gain_target) state->gain = gain_target; } else if (state->gain < gain_target) { state->gain *= state->release; if (state->gain > gain_target) state->gain = gain_target; } for (k = 0; k < e->ostream.channels; ++k) { if (GET_BIT(e->channel_selector, k)) obuf[i + k] = ibuf[i + k] * state->gain; else obuf[i + k] = ibuf[i + k]; } } }
int utlSmartMemmove (uint8_t ** str_p, unsigned *size_p, unsigned *alloc_p, uint8_t * str2, unsigned size2) { uint8_t *str = *str_p; unsigned alloc_sz = *alloc_p; unsigned size = *size_p; if (NULL == str2) return 1; if (NULL == str) { alloc_sz = MAXIMUM (128, size2); str = utlCalloc (1, alloc_sz); if (!str) return 1; } else if (alloc_sz < (size + size2)) { uint8_t *p; alloc_sz = size + size2 + 128; p = utlRealloc (str, alloc_sz); if (!p) return 1; str = p; } memmove (str + size, str2, size2); size += size2; *str_p = str; *size_p = size; *alloc_p = alloc_sz; return 0; }
struct TREE_NODE* MAXIMUM(struct TREE_NODE *N) { if(N == NULL) return NULL; if(N->L == NULL && N->R == NULL) return N; int M = N->K; struct TREE_NODE *X = MAXIMUM(N->L); struct TREE_NODE *Y = MAXIMUM(N->R); if(X != NULL) { if(X->K > M) return X; } if(Y != NULL) { if(Y->K > M) return Y; } }
///concat+realloc unterminated 2nd string int utlSmartCatUt (char **str_p, unsigned *size_p, char *str2, unsigned size2) { char *str = *str_p; unsigned size = *size_p; if ((NULL == str2) || (NULL == str_p) || (NULL == size_p)) return 1; if (NULL == str) { size = MAXIMUM (128, size2 + 1); str = utlCalloc (1, size); if (!str) return 1; } else if (size < (strlen (str) + size2 + 1)) { char *p; size = strlen (str) + size2 + 1 + 128; p = utlRealloc (str, size); if (!p) return 1; str = p; } strncat (str, str2, size2); *str_p = str; *size_p = size; return 0; }
/* * drwxr-xr-x 5 markus markus 1024 Jan 13 18:39 .ssh */ char * ls_file(const char *name, const struct stat *st, int remote, int si_units) { int ulen, glen, sz = 0; struct tm *ltime = localtime(&st->st_mtime); char *user, *group; char buf[1024], mode[11+1], tbuf[12+1], ubuf[11+1], gbuf[11+1]; char sbuf[FMT_SCALED_STRSIZE]; time_t now; strmode(st->st_mode, mode); if (!remote) { user = user_from_uid(st->st_uid, 0); } else { snprintf(ubuf, sizeof ubuf, "%u", (u_int)st->st_uid); user = ubuf; } if (!remote) { group = group_from_gid(st->st_gid, 0); } else { snprintf(gbuf, sizeof gbuf, "%u", (u_int)st->st_gid); group = gbuf; } if (ltime != NULL) { now = time(NULL); if (now - (365*24*60*60)/2 < st->st_mtime && now >= st->st_mtime) sz = strftime(tbuf, sizeof tbuf, "%b %e %H:%M", ltime); else sz = strftime(tbuf, sizeof tbuf, "%b %e %Y", ltime); } if (sz == 0) tbuf[0] = '\0'; ulen = MAXIMUM(strlen(user), 8); glen = MAXIMUM(strlen(group), 8); if (si_units) { fmt_scaled((long long)st->st_size, sbuf); snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8s %s %s", mode, (u_int)st->st_nlink, ulen, user, glen, group, sbuf, tbuf, name); } else { snprintf(buf, sizeof buf, "%s %3u %-*s %-*s %8llu %s %s", mode, (u_int)st->st_nlink, ulen, user, glen, group, (unsigned long long)st->st_size, tbuf, name); } return xstrdup(buf); }
INT set_of_sets::largest_set_size() { INT s = INT_MIN; INT i; for (i = 0; i < nb_sets; i++) { s = MAXIMUM(s, Set_size[i]); } return s; }
/* * This function creates the scrolling list information and sets up the needed * variables for the scrolling list to work correctly. */ static int createCDKScrollItemList (CDKSCROLL *scrollp, boolean numbers, CDK_CSTRING2 list, int listSize) { int status = 0; if (listSize > 0) { /* *INDENT-EQLS* */ int widestItem = 0; int x = 0; size_t have = 0; char *temp = 0; if (allocListArrays (scrollp, 0, listSize)) { /* Create the items in the scrolling list. */ status = 1; for (x = 0; x < listSize; x++) { if (!allocListItem (scrollp, x, &temp, &have, numbers ? (x + 1) : 0, list[x])) { status = 0; break; } widestItem = MAXIMUM (scrollp->itemLen[x], widestItem); } freeChecked (temp); if (status) { updateViewWidth (scrollp, widestItem); /* Keep the boolean flag 'numbers' */ scrollp->numbers = numbers; } } } else { status = 1; /* null list is ok - for a while */ } return status; }
/* * rm_overwrite -- * Overwrite the file with varying bit patterns. * * XXX * This is a cheap way to *really* delete files. Note that only regular * files are deleted, directories (and therefore names) will remain. * Also, this assumes a fixed-block file system (like FFS, or a V7 or a * System V file system). In a logging file system, you'll have to have * kernel support. * Returns 1 for success. */ int rm_overwrite(char *file, struct stat *sbp) { struct stat sb, sb2; struct statfs fsb; size_t bsize; int fd; char *buf = NULL; fd = -1; if (sbp == NULL) { if (lstat(file, &sb)) goto err; sbp = &sb; } if (!S_ISREG(sbp->st_mode)) return (1); if (sbp->st_nlink > 1) { warnx("%s (inode %llu): not overwritten due to multiple links", file, (unsigned long long)sbp->st_ino); return (0); } if ((fd = open(file, O_WRONLY|O_NONBLOCK|O_NOFOLLOW, 0)) == -1) goto err; if (fstat(fd, &sb2)) goto err; if (sb2.st_dev != sbp->st_dev || sb2.st_ino != sbp->st_ino || !S_ISREG(sb2.st_mode)) { errno = EPERM; goto err; } if (fstatfs(fd, &fsb) == -1) goto err; bsize = MAXIMUM(fsb.f_iosize, 1024U); if ((buf = malloc(bsize)) == NULL) err(1, "%s: malloc", file); if (!pass(fd, sbp->st_size, buf, bsize)) goto err; if (fsync(fd)) goto err; close(fd); free(buf); return (1); err: warn("%s", file); close(fd); eval = 1; free(buf); return (0); }
void SkillSet::trajectory2D(const Vector2D<int>& startPos, const Vector2D<int>& finalPos, const Vector2D<float>& startVel, const Vector2D<float>& finalVel, const float startAng, const float finalAng, const float startAngVel, const float finalAngVel, const float frameRate, const float maxAcc, const float maxVel, const float maxAngAcc, const float maxAngVel, Vector2D<float>& trajVel, float& angVel) { Vector2D<float> acc; float timeX, timeY; Vector2D<int> delta = finalPos - startPos; float u = PI / 2; float du = -PI / 2; // Performing binary search for the optimum value of alpha which will result in for (int i = 0; i < 10; ++i) { float alpha = u + (du /= 2); Vector2D<float> aTransMax, vTransMax; aTransMax.fromPolar(maxAcc, alpha); vTransMax.fromPolar(maxVel, alpha); trajectory1D(delta.x, startVel.x, finalVel.x, frameRate, aTransMax.x, vTransMax.x, acc.x, timeX); trajectory1D(delta.y, startVel.y, finalVel.y, frameRate, aTransMax.y, vTransMax.y, acc.y, timeY); if (timeX <= timeY) u = alpha; } float trajTime = MAXIMUM(timeX, timeY); float deltaAng = MINIMUM(finalAng - startAng, 0); // forcing deltaAng to lie in (-pi, pi] float angAcc = 0 , timeAng = INFINITY; for (float factor = 0.1f; factor <= 1.05f; factor += 0.1f) // using 1.05f instead of 1.0f to account for errors due to floating point precision { trajectory1D(deltaAng, startAngVel, finalAngVel, frameRate, maxAngAcc * factor, maxAngVel, angAcc, timeAng); if (timeAng < trajTime) break; } trajVel.x = startVel.x + acc.x / frameRate; trajVel.y = startVel.y + acc.y / frameRate; angVel = startAngVel + angAcc / frameRate; } // trajectory2D
/* * This calls alloc_segs which may run out of memory. Alloc_segs will destroy * the table and set errno, so we just pass the error information along. * * Returns 0 on No Error */ static int init_htab(HTAB *hashp, int nelem) { int nbuckets, nsegs, l2; /* * Divide number of elements by the fill factor and determine a * desired number of buckets. Allocate space for the next greater * power of two number of buckets. */ nelem = (nelem - 1) / hashp->FFACTOR + 1; l2 = __log2(MAXIMUM(nelem, 2)); nbuckets = 1 << l2; hashp->SPARES[l2] = l2 + 1; hashp->SPARES[l2 + 1] = l2 + 1; hashp->OVFL_POINT = l2; hashp->LAST_FREED = 2; /* First bitmap page is at: splitpoint l2 page offset 1 */ if (__ibitmap(hashp, OADDR_OF(l2, 1), l2 + 1, 0)) return (-1); hashp->MAX_BUCKET = hashp->LOW_MASK = nbuckets - 1; hashp->HIGH_MASK = (nbuckets << 1) - 1; hashp->HDRPAGES = ((MAXIMUM(sizeof(HASHHDR), MINHDRSIZE) - 1) >> hashp->BSHIFT) + 1; nsegs = (nbuckets - 1) / hashp->SGSIZE + 1; nsegs = 1 << __log2(nsegs); if (nsegs > hashp->DSIZE) hashp->DSIZE = nsegs; return (alloc_segs(hashp, nsegs)); }
INT braun_test_single_type(INT v, INT k, INT ak) { INT i, l, s, m; i = 0; s = 0; for (l = 1; l <= ak; l++) { m = MAXIMUM(k - i, 0); s += m; if (s > v) return FALSE; i++; } return TRUE; }
void knapsack(int no, int wt, int pt[MAX], int weight[MAX]) { int knap[MAX][MAX]={0} ,x[MAX]={0},i,j; for (i = 1; i <= no; i++) for (j = 1; j <= wt; j++) { if ((j-weight[i])<0) knap[i][j]=knap[i-1][j]; else knap[i][j]=MAXIMUM(knap[i-1][j],pt[i]+knap[i-1][j-weight[i]]); } printf("Max profit : %d\n", knap[no][wt]); printf("Edges are : \n"); for (i = no; i >= 1; i--) if (knap[i][wt] != knap[i-1][wt]) { x[i] = 1; wt -= weight[i]; } for (i = 1; i <= no; i++) printf("%d\t",x[i]); printf("\n"); }
void count(INT n, INT k, finite_field *F, INT verbose_level) { INT f_v = (verbose_level >= 1); INT q, h; INT kn = k * n; INT m, N, r; INT *M; INT *Rk; if (f_v) { cout << "count" << endl; } m = MAXIMUM(k, n); q = F->q; N = i_power_j(q, kn); M = NEW_INT(kn); Rk = NEW_INT(m + 1); INT_vec_zero(Rk, m + 1); for (h = 0; h < N; h++) { AG_element_unrank(q, M, 1, kn, h); r = F->rank_of_rectangular_matrix(M, k, n, 0 /* verbose_level */); Rk[r]++; } cout << "Rank distribution:" << endl; for (h = 0; h <= m; h++) { cout << h << " : " << Rk[h] << endl; } cout << "N=" << N << endl; if (f_v) { cout << "count done" << endl; } }
void print_power_history(int start, int end) { int i; for(i=MAXIMUM(0, start); i<MINIMUM(RETENTION_SIZE, end); i++) LOG(stdout, "[%d] History - consumed %f @ %llu (%f)\n", i, power_history[i].power, power_history[i].timestamp, power_history[i].delta / 1000000.0f); }
/* ** This void is the core of HTBTree.c . It will ** 1/ add a new element to the tree at the right place ** so that the tree remains sorted ** 2/ balance the tree to be as fast as possible when reading it */ PUBLIC void HTBTree_add (HTBTree * tree, void * object) { HTBTElement * father_of_element; HTBTElement * added_element; HTBTElement * forefather_of_element; HTBTElement * father_of_forefather; BOOL father_found,top_found; int depth,depth2,corrections; /* father_of_element is a pointer to the structure that is the father of the ** new object "object". ** added_element is a pointer to the structure that contains or will contain ** the new object "object". ** father_of_forefather and forefather_of_element are pointers that are used ** to modify the depths of upper elements, when needed. ** ** father_found indicates by a value NO when the future father of "object" ** is found. ** top_found indicates by a value NO when, in case of a difference of depths ** < 2, the top of the tree is encountered and forbids any further try to ** balance the tree. ** corrections is an integer used to avoid infinite loops in cases ** such as: ** ** 3 3 ** 4 4 ** 5 5 ** ** 3 is used here to show that it need not be the top of the tree. */ /* ** 1/ Adding of the element to the binary tree */ if (tree->top == NULL) { if ((tree->top = (HTBTElement *) HT_MALLOC(sizeof(HTBTElement))) == NULL) HT_OUTOFMEM("HTBTree_add"); tree->top->up = NULL; tree->top->object = object; tree->top->left = NULL; tree->top->left_depth = 0; tree->top->right = NULL; tree->top->right_depth = 0; } else { father_found = YES; father_of_element = tree->top; added_element = NULL; father_of_forefather = NULL; forefather_of_element = NULL; while (father_found) { if (tree->compare(object,father_of_element->object)<0) { if (father_of_element->left != NULL) father_of_element = father_of_element->left; else { father_found = NO; if ((father_of_element->left = (HTBTElement *) HT_MALLOC(sizeof(HTBTElement))) == NULL) HT_OUTOFMEM("HTBTree_add"); added_element = father_of_element->left; added_element->up = father_of_element; added_element->object = object; added_element->left = NULL; added_element->left_depth = 0; added_element->right = NULL; added_element->right_depth = 0; } } if (tree->compare(object,father_of_element->object)>=0) { if (father_of_element->right != NULL) father_of_element = father_of_element->right; else { father_found = NO; if ((father_of_element->right = (HTBTElement *) HT_MALLOC(sizeof(HTBTElement))) == NULL) HT_OUTOFMEM("father_of_element->right "); added_element = father_of_element->right; added_element->up = father_of_element; added_element->object = object; added_element->left = NULL; added_element->left_depth = 0; added_element->right = NULL; added_element->right_depth = 0; } } } /* ** Changing of all depths that need to be changed */ father_of_forefather = father_of_element; forefather_of_element = added_element; do { if (father_of_forefather->left == forefather_of_element) { depth = father_of_forefather->left_depth; father_of_forefather->left_depth = 1 + MAXIMUM(forefather_of_element->right_depth, forefather_of_element->left_depth); depth2 = father_of_forefather->left_depth; } else { depth = father_of_forefather->right_depth; father_of_forefather->right_depth = 1 + MAXIMUM(forefather_of_element->right_depth, forefather_of_element->left_depth); depth2 = father_of_forefather->right_depth; } forefather_of_element = father_of_forefather; father_of_forefather = father_of_forefather->up; } while ((depth != depth2) && (father_of_forefather != NULL)); /* ** 2/ Balancing the binary tree, if necessary ** Bugs in this part have been fixed in March 94 - AS */ top_found = YES; corrections = 0; while ((top_found) && (corrections < 7)) { if ((abs(father_of_element->left_depth - father_of_element->right_depth)) < 2) { if (father_of_element->up != NULL) father_of_element = father_of_element->up; else top_found = NO; } else { /* We start the process of balancing */ corrections = corrections + 1; /* ** corrections is an integer used to avoid infinite ** loops in cases such as: ** ** 3 3 ** 4 4 ** 5 5 ** ** 3 is used to show that it need not be the top of the tree ** But let's avoid these two exceptions anyhow ** with the two following conditions (March 94 - AS) */ if ((father_of_element->left == NULL) && (father_of_element->right->right == NULL) && (father_of_element->right->left->left == NULL) && (father_of_element->right->left->right == NULL)) corrections = 7; if ((father_of_element->right == NULL) && (father_of_element->left->left == NULL) && (father_of_element->left->right->right == NULL) && (father_of_element->left->right->left == NULL)) corrections = 7; if (father_of_element->left_depth > father_of_element->right_depth) { added_element = father_of_element->left; father_of_element->left_depth = added_element->right_depth; added_element->right_depth = 1 + MAXIMUM(father_of_element->right_depth, father_of_element->left_depth); if (father_of_element->up != NULL) { /* Bug fixed in March 94 */ BOOL first_time; father_of_forefather = father_of_element->up; forefather_of_element = added_element; first_time = YES; do { if (father_of_forefather->left == forefather_of_element->up) { depth = father_of_forefather->left_depth; if (first_time) { father_of_forefather->left_depth = 1 + MAXIMUM(forefather_of_element->left_depth, forefather_of_element->right_depth); first_time = NO; } else father_of_forefather->left_depth = 1 + MAXIMUM(forefather_of_element->up->left_depth, forefather_of_element->up->right_depth); depth2 = father_of_forefather->left_depth; } else { depth = father_of_forefather->right_depth; if (first_time) { father_of_forefather->right_depth = 1 + MAXIMUM(forefather_of_element->left_depth, forefather_of_element->right_depth); first_time = NO; } else father_of_forefather->right_depth = 1 + MAXIMUM(forefather_of_element->up->left_depth, forefather_of_element->up->right_depth); depth2 = father_of_forefather->right_depth; } forefather_of_element = forefather_of_element->up; father_of_forefather = father_of_forefather->up; } while ((depth != depth2) && (father_of_forefather != NULL)); father_of_forefather = father_of_element->up; if (father_of_forefather->left == father_of_element) { /* ** 3 3 ** 4 5 ** When tree 5 6 becomes 7 4 ** 7 8 8 6 ** ** 3 is used to show that it may not be the top of the ** tree. */ father_of_forefather->left = added_element; father_of_element->left = added_element->right; added_element->right = father_of_element; } if (father_of_forefather->right == father_of_element) { /* ** 3 3 ** 4 5 ** When tree 5 6 becomes 7 4 ** 7 8 8 6 ** ** 3 is used to show that it may not be the top of the ** tree */ father_of_forefather->right = added_element; father_of_element->left = added_element->right; added_element->right = father_of_element; } added_element->up = father_of_forefather; } else { /* ** ** 1 2 ** When tree 2 3 becomes 4 1 ** 4 5 5 3 ** ** 1 is used to show that it is the top of the tree */ added_element->up = NULL; father_of_element->left = added_element->right; added_element->right = father_of_element; } father_of_element->up = added_element; if (father_of_element->left != NULL) father_of_element->left->up = father_of_element; } else { added_element = father_of_element->right; father_of_element->right_depth = added_element->left_depth; added_element->left_depth = 1 + MAXIMUM(father_of_element->right_depth, father_of_element->left_depth); if (father_of_element->up != NULL) /* Bug fixed in March 94 */ { BOOL first_time; father_of_forefather = father_of_element->up; forefather_of_element = added_element; first_time = YES; do { if (father_of_forefather->left == forefather_of_element->up) { depth = father_of_forefather->left_depth; if (first_time) { father_of_forefather->left_depth = 1 + MAXIMUM(forefather_of_element->left_depth, forefather_of_element->right_depth); first_time = NO; } else father_of_forefather->left_depth = 1 + MAXIMUM(forefather_of_element->up->left_depth, forefather_of_element->up->right_depth); depth2 = father_of_forefather->left_depth; } else { depth = father_of_forefather->right_depth; if (first_time) { father_of_forefather->right_depth = 1 + MAXIMUM(forefather_of_element->left_depth, forefather_of_element->right_depth); first_time = NO; } else father_of_forefather->right_depth = 1 + MAXIMUM(forefather_of_element->up->left_depth, forefather_of_element->up->right_depth); depth2 = father_of_forefather->right_depth; } father_of_forefather = father_of_forefather->up; forefather_of_element = forefather_of_element->up; } while ((depth != depth2) && (father_of_forefather != NULL)); father_of_forefather = father_of_element->up; if (father_of_forefather->left == father_of_element) { /* ** 3 3 ** 4 6 ** When tree 5 6 becomes 4 8 ** 7 8 5 7 ** ** 3 is used to show that it may not be the top of the ** tree. */ father_of_forefather->left = added_element; father_of_element->right = added_element->left; added_element->left = father_of_element; } if (father_of_forefather->right == father_of_element) { /* ** 3 3 ** 4 6 ** When tree 5 6 becomes 4 8 ** 7 8 5 7 ** ** 3 is used to show that it may not be the top of the ** tree */ father_of_forefather->right = added_element; father_of_element->right = added_element->left; added_element->left = father_of_element; } added_element->up = father_of_forefather; } else { /* ** ** 1 3 ** When tree 2 3 becomes 1 5 ** 4 5 2 4 ** ** 1 is used to show that it is the top of the tree. */ added_element->up = NULL; father_of_element->right = added_element->left; added_element->left = father_of_element; } father_of_element->up = added_element; if (father_of_element->right != NULL) father_of_element->right->up = father_of_element; } } } while (father_of_element->up != NULL) { father_of_element = father_of_element->up; } tree->top = father_of_element; } }
//////////////////////////////////////////////////////////////////////////////// //! Run a simple test for CUDA //////////////////////////////////////////////////////////////////////////////// void runTest( int argc, char** argv) { int max_rows, max_cols, penalty,idx, index; int *input_itemsets, *output_itemsets, *referrence; int *matrix_cuda, *matrix_cuda_out, *referrence_cuda; int size; // the lengths of the two sequences should be able to divided by 16. // And at current stage max_rows needs to equal max_cols if (argc == 3) { max_rows = atoi(argv[1]); max_cols = atoi(argv[1]); penalty = atoi(argv[2]); } else{ usage(argc, argv); } max_rows = max_rows + 1; max_cols = max_cols + 1; referrence = (int *)malloc( max_rows * max_cols * sizeof(int) ); input_itemsets = (int *)malloc( max_rows * max_cols * sizeof(int) ); output_itemsets = (int *)malloc( max_rows * max_cols * sizeof(int) ); if (!input_itemsets) fprintf(stderr, "error: can not allocate memory"); srand ( 7 ); for (int i = 0 ; i < max_cols; i++){ for (int j = 0 ; j < max_rows; j++){ input_itemsets[i*max_cols+j] = 0; } } printf("Start Needleman-Wunsch\n"); for( int i=1; i< max_rows ; i++){ //please define your own sequence. input_itemsets[i*max_cols] = rand() % 10 + 1; } for( int j=1; j< max_cols ; j++){ //please define your own sequence. input_itemsets[j] = rand() % 10 + 1; } for (int i = 1 ; i < max_cols; i++){ for (int j = 1 ; j < max_rows; j++){ referrence[i*max_cols+j] = blosum62[input_itemsets[i*max_cols]][input_itemsets[j]]; } } #pragma acc data copy(input_itemsets[0:max_rows*max_cols]) \ copyin(referrence[0:max_rows*max_cols]) { #pragma acc parallel loop for( int i = 1; i< max_rows ; i++) input_itemsets[i*max_cols] = -i * penalty; #pragma acc parallel loop for( int j = 1; j< max_cols ; j++) input_itemsets[j] = -j * penalty; //Compute top-left matrix printf("Processing top-left matrix\n"); for( int i = 0 ; i < max_cols-2 ; i++){ #pragma acc parallel loop for( idx = 0 ; idx <= i ; idx++){ index = (idx + 1) * max_cols + (i + 1 - idx); input_itemsets[index]= MAXIMUM( input_itemsets[index-1-max_cols]+ referrence[index], input_itemsets[index-1] - penalty, input_itemsets[index-max_cols] - penalty); } } //Compute bottom-right matrix printf("Processing bottom-right matrix\n"); for( int i = max_cols - 4 ; i >= 0 ; i--){ #pragma acc parallel loop for( idx = 0 ; idx <= i ; idx++){ index = ( max_cols - idx - 2 ) * max_cols + idx + max_cols - i - 2 ; input_itemsets[index]= MAXIMUM( input_itemsets[index-1-max_cols]+ referrence[index], input_itemsets[index-1] - penalty, input_itemsets[index-max_cols] - penalty); } } } /* end pragma acc data */ //#define TRACEBACK #ifdef TRACEBACK FILE *fpo = fopen("result.txt","w"); fprintf(fpo, "print traceback value GPU:\n"); for (int i = max_rows - 2, j = max_rows - 2; i>=0, j>=0;){ int nw, n, w, traceback; if ( i == max_rows - 2 && j == max_rows - 2 ) fprintf(fpo, "%d ", input_itemsets[ i * max_cols + j]); //print the first element if ( i == 0 && j == 0 ) break; if ( i > 0 && j > 0 ){ nw = input_itemsets[(i - 1) * max_cols + j - 1]; w = input_itemsets[ i * max_cols + j - 1 ]; n = input_itemsets[(i - 1) * max_cols + j]; } else if ( i == 0 ){ nw = n = LIMIT; w = input_itemsets[ i * max_cols + j - 1 ]; } else if ( j == 0 ){ nw = w = LIMIT; n = input_itemsets[(i - 1) * max_cols + j]; } else{ } //traceback = maximum(nw, w, n); int new_nw, new_w, new_n; new_nw = nw + referrence[i * max_cols + j]; new_w = w - penalty; new_n = n - penalty; traceback = MAXIMUM(new_nw, new_w, new_n); if(traceback == new_nw) traceback = nw; if(traceback == new_w) traceback = w; if(traceback == new_n) traceback = n; fprintf(fpo, "%d ", traceback); if(traceback == nw ) {i--; j--; continue;} else if(traceback == w ) {j--; continue;} else if(traceback == n ) {i--; continue;} else ; } fclose(fpo); #endif free(referrence); free(input_itemsets); free(output_itemsets); }
static void setup(void) { if (in.name == NULL) { in.name = "stdin"; in.fd = STDIN_FILENO; } else { in.fd = open(in.name, O_RDONLY, 0); if (in.fd < 0) err(1, "%s", in.name); } getfdtype(&in); if (files_cnt > 1 && !(in.flags & ISTAPE)) errx(1, "files is not supported for non-tape devices"); if (out.name == NULL) { /* No way to check for read access here. */ out.fd = STDOUT_FILENO; out.name = "stdout"; } else { #define OFLAGS \ (O_CREAT | (ddflags & (C_SEEK | C_NOTRUNC) ? 0 : O_TRUNC)) out.fd = open(out.name, O_RDWR | OFLAGS, DEFFILEMODE); /* * May not have read access, so try again with write only. * Without read we may have a problem if output also does * not support seeks. */ if (out.fd < 0) { out.fd = open(out.name, O_WRONLY | OFLAGS, DEFFILEMODE); out.flags |= NOREAD; } if (out.fd < 0) err(1, "%s", out.name); } getfdtype(&out); /* * Allocate space for the input and output buffers. If not doing * record oriented I/O, only need a single buffer. */ if (!(ddflags & (C_BLOCK|C_UNBLOCK))) { if ((in.db = malloc(out.dbsz + in.dbsz - 1)) == NULL) err(1, "input buffer"); out.db = in.db; } else { in.db = malloc(MAXIMUM(in.dbsz, cbsz) + cbsz); if (in.db == NULL) err(1, "input buffer"); out.db = malloc(out.dbsz + cbsz); if (out.db == NULL) err(1, "output buffer"); } in.dbp = in.db; out.dbp = out.db; /* Position the input/output streams. */ if (in.offset) pos_in(); if (out.offset) pos_out(); if (pledge("stdio", NULL) == -1) err(1, "pledge"); /* * Truncate the output file; ignore errors because it fails on some * kinds of output files, tapes, for example. */ if ((ddflags & (C_OF | C_SEEK | C_NOTRUNC)) == (C_OF | C_SEEK)) (void)ftruncate(out.fd, out.offset * out.dbsz); /* * If converting case at the same time as another conversion, build a * table that does both at once. If just converting case, use the * built-in tables. */ if (ddflags & (C_LCASE|C_UCASE)) { #ifdef NO_CONV /* Should not get here, but just in case... */ errx(1, "case conv and -DNO_CONV"); #else /* NO_CONV */ u_int cnt; if (ddflags & C_ASCII || ddflags & C_EBCDIC) { if (ddflags & C_LCASE) { for (cnt = 0; cnt < 0377; ++cnt) casetab[cnt] = tolower(ctab[cnt]); } else { for (cnt = 0; cnt < 0377; ++cnt) casetab[cnt] = toupper(ctab[cnt]); } } else { if (ddflags & C_LCASE) { for (cnt = 0; cnt < 0377; ++cnt) casetab[cnt] = tolower(cnt); } else { for (cnt = 0; cnt < 0377; ++cnt) casetab[cnt] = toupper(cnt); } } ctab = casetab; #endif /* NO_CONV */ } /* Statistics timestamp. */ clock_gettime(CLOCK_MONOTONIC, &st.start); }
static void multi_op(void *ivec, void *iovec, int *n, MPI_Datatype *dptr) { /* multi_op: O(G*I) * routine to handle multiple operations for a global reduction */ int j; int *invec = (int *) ivec; int *inoutvec = (int *) iovec; int ctr; double value, value1, value2; int k; int nval; ASSERT(*dptr == MPI_INT); for (j = 0; j < *n;) { inoutvec[j] = invec[j]; nval = invec[j++]; inoutvec[j] = invec[j]; switch (invec[j++]) { case SUM: for (k = 0; k < nval; ++k) { inoutvec[j] += invec[j]; j += 1; } break; case MAX: for (k = 0; k < nval; ++k) { inoutvec[j] = MAXIMUM(inoutvec[j],invec[j]); j += 1; } break; #ifdef DEVELOPMENT case MAXBYTE: for (k = 0; k < nval; ++k) { inoutvec[j] = max_byte(inoutvec[j],invec[j]); j += 1; } break; #endif case MAXLOC: for (k = 0; k < nval; ++k) { if (invec[j] > inoutvec[j]) { inoutvec[j] = invec[j]; inoutvec[j+1] = invec[j+1]; } else if (invec[j] == inoutvec[j] && invec[j+1] < inoutvec[j+1]) { inoutvec[j+1] = invec[j+1]; } j += 2; } break; case MIN: for (k = 0; k < nval; ++k) { inoutvec[j] = MINIMUM(inoutvec[j],invec[j]); j += 1; } break; #ifdef DEVELOPMENT case MINBYTE: for (k = 0; k < nval; ++k) { inoutvec[j] = min_byte(inoutvec[j],invec[j]); j += 1; } break; #endif case MINLOC: for (k = 0; k < nval; ++k) { if (invec[j] < inoutvec[j]) { inoutvec[j] = invec[j]; inoutvec[j+1] = invec[j+1]; } else if (invec[j] == inoutvec[j] && invec[j+1] < inoutvec[j+1]) { inoutvec[j+1] = invec[j+1]; } j += 2; } break; case DSUM: for (k = 0; k < nval; ++k) { ctr = 0; value = int2dbl(&invec[j],&ctr); ctr = 0; value += int2dbl(&inoutvec[j],&ctr); dbl2int(value,inoutvec,&j); } break; case DMAX: for (k = 0; k < nval; ++k) { ctr = 0; value1 = int2dbl(&invec[j],&ctr); ctr = 0; value2 = int2dbl(&inoutvec[j],&ctr); value = MAXIMUM(value1,value2); dbl2int(value,inoutvec,&j); } break; case DMIN: for (k = 0; k < nval; ++k) { ctr = 0; value1 = int2dbl(&invec[j],&ctr); ctr = 0; value2 = int2dbl(&inoutvec[j],&ctr); value = MINIMUM(value1,value2); dbl2int(value,inoutvec,&j); } break; } } }
/* * This creates a button widget. */ CDKBUTTON *newCDKButton (CDKSCREEN *cdkscreen, int xplace, int yplace, const char *text, tButtonCallback callback, boolean Box, boolean shadow) { /* *INDENT-EQLS* */ CDKBUTTON *button = 0; int parentWidth = getmaxx (cdkscreen->window); int parentHeight = getmaxy (cdkscreen->window); int boxWidth = 0; int boxHeight; int xpos = xplace; int ypos = yplace; if ((button = newCDKObject (CDKBUTTON, &my_funcs)) == 0) return (0); setCDKButtonBox (button, Box); boxHeight = 1 + 2 * BorderOf (button); /* Translate the char * to a chtype. */ button->info = char2Chtype (text, &button->infoLen, &button->infoPos); boxWidth = MAXIMUM (boxWidth, button->infoLen) + 2 * BorderOf (button); /* Create the string alignments. */ button->infoPos = justifyString (boxWidth - 2 * BorderOf (button), button->infoLen, button->infoPos); /* * Make sure we didn't extend beyond the dimensions of the window. */ boxWidth = (boxWidth > parentWidth ? parentWidth : boxWidth); boxHeight = (boxHeight > parentHeight ? parentHeight : boxHeight); /* Rejustify the x and y positions if we need to. */ alignxy (cdkscreen->window, &xpos, &ypos, boxWidth, boxHeight); /* *INDENT-EQLS* Create the button. */ ScreenOf (button) = cdkscreen; ObjOf (button)->fn = &my_funcs; button->parent = cdkscreen->window; button->win = newwin (boxHeight, boxWidth, ypos, xpos); button->shadowWin = (WINDOW *)NULL; button->xpos = xpos; button->ypos = ypos; button->boxWidth = boxWidth; button->boxHeight = boxHeight; button->callback = callback; ObjOf (button)->inputWindow = button->win; ObjOf (button)->acceptsFocus = TRUE; initExitType (button); button->shadow = shadow; /* Is the window NULL? */ if (button->win == (WINDOW *)NULL) { destroyCDKObject (button); return (0); } keypad (button->win, TRUE); /* If a shadow was requested, then create the shadow window. */ if (shadow) button->shadowWin = newwin (boxHeight, boxWidth, ypos + 1, xpos + 1); /* Register this baby. */ registerCDKObject (cdkscreen, vBUTTON, button); /* Return the button pointer. */ return (button); }
int main (int argc, char **argv) { /* *INDENT-EQLS* */ CDKSCREEN *cdkScreen = 0; CDKMATRIX *widget = 0; CDKBUTTONBOX *buttonWidget = 0; chtype *holder = 0; char *buttons = 0; char *CDK_WIDGET_COLOR = 0; char *temp = 0; chtype filler = A_NORMAL | '.'; int rows = -1; int cols = -1; int buttonCount = 0; int selection = 0; int shadowHeight = 0; FILE *fp = stderr; char **rowTitles; char **colTitles; char **rowTemp = 0; char **colTemp = 0; char **kolTemp = 0; char **buttonList = 0; int *colWidths; int *colTypes; int count, infoLines, x, y, j1, j2; CDK_PARAMS params; boolean boxWidget; boolean shadowWidget; char *defaultValue; char *myColTitles; char *myColTypes; char *myColWidths; char *myFiller; char *myRowTitles; char *outputFile; char *title; int vrows; int xpos; int ypos; CDKparseParams (argc, argv, ¶ms, "c:d:r:t:w:v:B:F:O:T:" CDK_MIN_PARAMS); /* *INDENT-EQLS* */ xpos = CDKparamValue (¶ms, 'X', CENTER); ypos = CDKparamValue (¶ms, 'Y', CENTER); boxWidget = CDKparamValue (¶ms, 'N', TRUE); shadowWidget = CDKparamValue (¶ms, 'S', FALSE); vrows = CDKparamValue (¶ms, 'v', -1); myColTitles = CDKparamString (¶ms, 'c'); defaultValue = CDKparamString (¶ms, 'd'); myRowTitles = CDKparamString (¶ms, 'r'); myColTypes = CDKparamString (¶ms, 't'); myColWidths = CDKparamString (¶ms, 'w'); buttons = CDKparamString (¶ms, 'B'); myFiller = CDKparamString (¶ms, 'F'); outputFile = CDKparamString (¶ms, 'O'); title = CDKparamString (¶ms, 'T'); /* If the user asked for an output file, try to open it. */ if (outputFile != 0) { if ((fp = fopen (outputFile, "w")) == 0) { fprintf (stderr, "%s: Can not open output file %s\n", argv[0], outputFile); ExitProgram (CLI_ERROR); } } /* Make sure all the needed command line parameters were provided. */ if ((myRowTitles == 0) || (myColTitles == 0) || (myColWidths == 0) || (vrows == -1)) { fprintf (stderr, "Usage: %s %s\n", argv[0], FPUsage); ExitProgram (CLI_ERROR); } /* Convert the char * titles to a char **, offset by one */ rowTemp = CDKsplitString (myRowTitles, '\n'); rows = (int)CDKcountStrings ((CDK_CSTRING2)rowTemp); rowTitles = (char **)calloc ((size_t) rows + 1, sizeof (char *)); for (x = 0; x < rows; x++) { rowTitles[x + 1] = rowTemp[x]; } colTemp = CDKsplitString (myColTitles, '\n'); cols = (int)CDKcountStrings ((CDK_CSTRING2)colTemp); colTitles = (char **)calloc ((size_t) cols + 1, sizeof (char *)); for (x = 0; x < cols; x++) { colTitles[x + 1] = colTemp[x]; } /* Convert the column widths. */ kolTemp = CDKsplitString (myColWidths, '\n'); count = (int)CDKcountStrings ((CDK_CSTRING2)kolTemp); colWidths = (int *)calloc ((size_t) count + 1, sizeof (int)); for (x = 0; x < count; x++) { colWidths[x + 1] = atoi (kolTemp[x]); } /* If they passed in the column types, convert them. */ if (myColTypes != 0) { char **ss = CDKsplitString (myColTypes, '\n'); count = (int)CDKcountStrings ((CDK_CSTRING2)ss); colTypes = (int *)calloc ((size_t) MAXIMUM (cols, count) + 1, sizeof (int)); for (x = 0; x < count; x++) { colTypes[x + 1] = char2DisplayType (ss[x]); } CDKfreeStrings (ss); } else { /* If they didn't set default values. */ colTypes = (int *)calloc ((size_t) cols + 1, sizeof (int)); for (x = 0; x < cols; x++) { colTypes[x + 1] = vMIXED; } } cdkScreen = initCDKScreen (NULL); /* Start color. */ initCDKColor (); /* Check if the user wants to set the background of the main screen. */ if ((temp = getenv ("CDK_SCREEN_COLOR")) != 0) { holder = char2Chtype (temp, &j1, &j2); wbkgd (cdkScreen->window, holder[0]); wrefresh (cdkScreen->window); freeChtype (holder); } /* Get the widget color background color. */ if ((CDK_WIDGET_COLOR = getenv ("CDK_WIDGET_COLOR")) == 0) { CDK_WIDGET_COLOR = 0; } /* If the set the filler character, set it now. */ if (myFiller != 0) { holder = char2Chtype (myFiller, &j1, &j2); filler = holder[0]; freeChtype (holder); } /* Create the matrix widget. */ widget = newCDKMatrix (cdkScreen, xpos, ypos, rows, cols, vrows, cols, title, (CDK_CSTRING2)rowTitles, (CDK_CSTRING2)colTitles, colWidths, colTypes, 1, 1, filler, COL, boxWidget, TRUE, shadowWidget); free (rowTitles); free (colTitles); /* Make sure we could create the widget. */ if (widget == 0) { /* Shut down curses and CDK. */ destroyCDKScreen (cdkScreen); endCDK (); fprintf (stderr, "Error: Cannot create the matrix. " "Is the window too small?\n"); ExitProgram (CLI_ERROR); } /* * If the user sent in a file of default values, read it and * stick the values read in from the file into the matrix. */ if (defaultValue != 0) { size_t limit = (size_t) ((rows + 1) * (cols + 1)); char **info = (char **)calloc (limit, sizeof (char *)); char **lineTemp = 0; /* Read the file. */ infoLines = CDKreadFile (defaultValue, &lineTemp); if (infoLines > 0) { int *subSize = (int *)calloc ((size_t) infoLines + 1, sizeof (int)); /* For each line, split on a CTRL-V. */ for (x = 0; x < infoLines; x++) { char **ss = CDKsplitString (lineTemp[x], CTRL ('V')); subSize[x + 1] = (int)CDKcountStrings ((CDK_CSTRING2)ss); for (y = 0; y < subSize[x + 1]; y++) { MY_INFO (x, y) = ss[y]; } free (ss); } CDKfreeStrings (lineTemp); setCDKMatrixCells (widget, (CDK_CSTRING2)info, rows, cols, subSize); for (x = 0; x < infoLines; x++) { for (y = 0; y < subSize[x + 1]; y++) { freeChar (MY_INFO (x, y)); } } free (info); free (subSize); } } /* Split the buttons if they supplied some. */ if (buttons != 0) { /* Split the button list up. */ buttonList = CDKsplitString (buttons, '\n'); buttonCount = (int)CDKcountStrings ((CDK_CSTRING2)buttonList); /* We need to create a buttonbox widget. */ buttonWidget = newCDKButtonbox (cdkScreen, getbegx (widget->win), (getbegy (widget->win) + widget->boxHeight - 1), 1, widget->boxWidth - 1, NULL, 1, buttonCount, (CDK_CSTRING2)buttonList, buttonCount, A_REVERSE, boxWidget, FALSE); setCDKButtonboxULChar (buttonWidget, ACS_LTEE); setCDKButtonboxURChar (buttonWidget, ACS_RTEE); /* * We need to set the lower left and right * characters of the widget. */ setCDKMatrixLLChar (widget, ACS_LTEE); setCDKMatrixLRChar (widget, ACS_RTEE); /* * Bind the Tab key in the widget to send a * Tab key to the button box widget. */ bindCDKObject (vMATRIX, widget, KEY_TAB, widgetCB, buttonWidget); bindCDKObject (vMATRIX, widget, CDK_NEXT, widgetCB, buttonWidget); bindCDKObject (vMATRIX, widget, CDK_PREV, widgetCB, buttonWidget); /* Check if the user wants to set the background of the widget. */ setCDKButtonboxBackgroundColor (buttonWidget, CDK_WIDGET_COLOR); /* Draw the button widget. */ drawCDKButtonbox (buttonWidget, boxWidget); } /* * If the user asked for a shadow, we need to create one. Do this instead * of using the shadow parameter because the button widget is not part of * the main widget and if the user asks for both buttons and a shadow, we * need to create a shadow big enough for both widgets. Create the shadow * window using the widgets shadowWin element, so screen refreshes will draw * them as well. */ if (shadowWidget == TRUE) { /* Determine the height of the shadow window. */ shadowHeight = (buttonWidget == (CDKBUTTONBOX *)NULL ? widget->boxHeight : widget->boxHeight + buttonWidget->boxHeight - 1); /* Create the shadow window. */ widget->shadowWin = newwin (shadowHeight, widget->boxWidth, getbegy (widget->win) + 1, getbegx (widget->win) + 1); /* Make sure we could have created the shadow window. */ if (widget->shadowWin != 0) { widget->shadow = TRUE; /* * We force the widget and buttonWidget to be drawn so the * buttonbox widget will be drawn when the widget is activated. * Otherwise the shadow window will draw over the button widget. */ drawCDKMatrix (widget, ObjOf (widget)->box); eraseCDKButtonbox (buttonWidget); drawCDKButtonbox (buttonWidget, ObjOf (buttonWidget)->box); } } /* Check if the user wants to set the background of the widget. */ setCDKMatrixBackgroundColor (widget, CDK_WIDGET_COLOR); /* Let them play. */ activateCDKMatrix (widget, 0); /* Print out the matrix cells. */ if (widget->exitType == vNORMAL) { for (x = 0; x < widget->rows; x++) { for (y = 0; y < widget->cols; y++) { char *data = getCDKMatrixCell (widget, x, y); if (data != 0) { fprintf (fp, "%s%c", data, CTRL ('V')); } else { fprintf (fp, "%c", CTRL ('V')); } } fprintf (fp, "\n"); } } /* If there were buttons, get the button selected. */ if (buttonWidget != 0) { selection = buttonWidget->currentButton; destroyCDKButtonbox (buttonWidget); } /* cleanup (not really needed) */ CDKfreeStrings (buttonList); free (colTypes); free (colWidths); CDKfreeStrings (rowTemp); CDKfreeStrings (colTemp); CDKfreeStrings (kolTemp); destroyCDKMatrix (widget); destroyCDKScreen (cdkScreen); endCDK (); /* do this late, in case it was stderr */ fclose (fp); ExitProgram (selection); }