void x11_fplot(const XDpy * dpy, XAnim * anim, XFall * fall, color_t * pal, uint pal_len, int x, int y, uint w, uint h, double *xdat, double *ydat, uint l, double samp_rate, const char *xtitle, const char *ytitle, const char *fx_title, const char *fy_title, int is_log, box2d * taxx, box2d * faxx, uint axxhint) { int x1, y1, w1, h1; int x2, y2, w2, h2; int x3, y3, w3, h3; w1 = w; w2 = w; w3 = w; h1 = h / 3; h2 = h / 3; h3 = h - h1 - h2; x1 = x; x2 = x; x3 = x; y1 = y; y2 = y + h1; y3 = y + 2 * h1; if (taxx) { double xmin, xmax; double ymin, ymax; /* get extremes and spans */ darray_extremes(xdat, l, &xmin, &xmax); darray_extremes(ydat, l, &ymin, &ymax); if (axxhint == 0) { box2d_set(taxx, xmin, ymin, xmax, ymax); } else { /* extend it */ taxx->l.x = MIN2(taxx->l.x, xmin); taxx->l.y = MIN2(taxx->l.y, ymin); taxx->u.x = MAX2(taxx->u.x, xmax); taxx->u.y = MAX2(taxx->u.y, ymax); } x11_plot(dpy, anim, x1, y1, w1, h1, xdat, ydat, l, taxx->l.x, taxx->u.x, taxx->l.y, taxx->u.y, xtitle, ytitle); } else { x11_plot_autoaxis(dpy, anim, x1, y1, w1, h1, xdat, ydat, l, xtitle, ytitle); } double *xi = darray_calloc(l); /* zeros */ double *tre = darray_malloc(l); /* real part of time domain data */ double *tim = darray_malloc(l); /* imag part of time domain data */ double *abs = darray_malloc(l); uint lo2 = l / 2; double *fscale = darray_malloc(lo2); darray_ramp(fscale, lo2, 0, samp_rate / 2 / lo2); double *ydat_f = darray_malloc(l); uint i; /* windowing filter */ for (i = 0; i < l; i++) { ydat_f[i] = ydat[i] * double_blackman(i, l); } if (IS_BINPOW(l)) { /* only if length is a power of two */ /* can we perform an FFT */ darray_FFT(tre, tim, ydat_f, xi, l); darray_eud2D(abs, tre, tim, l); if (is_log) { uint i; for (i = 0; i < l; i++) { abs[i] = log10(abs[i]); } } if (faxx) { double xmin, xmax; double ymin, ymax; /* get extremes and spans */ darray_extremes(fscale, lo2, &xmin, &xmax); darray_extremes(abs, l, &ymin, &ymax); if (axxhint == 0) { box2d_set(faxx, xmin, ymin, xmax, ymax); } else { /* extend it */ faxx->l.x = MIN2(faxx->l.x, xmin); faxx->l.y = MIN2(faxx->l.y, ymin); faxx->u.x = MAX2(faxx->u.x, xmax); faxx->u.y = MAX2(faxx->u.y, ymax); } x11_plot(dpy, anim, x2, y2, w2, h2, fscale, abs, lo2, faxx->l.x, faxx->u.x, faxx->l.y, faxx->u.y, fx_title, fy_title); } else { x11_plot_autoaxis(dpy, anim, x2, y2, w2, h2, fscale, abs, lo2, fx_title, fy_title); } double ymin, ymax; darray_extremes(abs, l, &ymin, &ymax); double yspan = ymax - ymin; color_t * cols = (color_t*)malloc(lo2 * sizeof(color_t)); /* Waterfall */ for (i = 0; i < lo2; i++) { uint val = uint_clamp(0, (uint)((abs[i] - ymin) / yspan * (pal_len - 1)), pal_len - 1); cols[i] = pal[val]; } x11_appFall(dpy, anim, fall, cols, lo2); free(cols); x11_dWFall(dpy, anim, fall, x3, y3); } else { leprintf("%s: n=%u is not a power of two. Returning.\n", __FUNCTION__, l); } free(xi); free(tre); free(tim); free(abs); free(fscale); free(ydat_f); }
int main() { struct body_s *bod, *out; struct buf_s *buf; int n = 0,i=0; ssize_t len = 0; uint16_t cmdid = 0; int32_t num=-16; struct darray_s *da; struct head_s *head; bod = (struct body_s*)calloc(PACKET_SIZE, sizeof(struct body_s)); out = (struct body_s*)calloc(1, sizeof(struct body_s)); printf("boy size:%d\n", sizeof(struct body_s)); printf("num:%lld, zigzag:%lld\n", (int64_t)num, zigzag((int64_t)num)); head = (struct head_s*)calloc(1, sizeof(struct head_s)); if (head == NULL) return 0; head->cmdid = 0xF001; head->seq = 0x10001; head->uid = 0x1000; head->aid = 0x0001; buf = buf_calloc(1024); len = enpacket(head, (u_char*)NULL, 0, 0, NULL, 0, buf); printf("packet buf:%p, len:%d\n", buf->buf_pos, (int)buf->buf_len); hexdump((u_char *)buf->buf_pos, buf->buf_len); memset(head, 0, sizeof(struct head_s)); len = dehead(buf, head); if (len==-1) { printf("errno:%d, errstr:%s\n", errno, strerror(errno)); exit(1); } printf("cmdid:%x\n", head->cmdid); switch (head->cmdid){ case 0xF001: //out->a17 = (u_char*)calloc(1,10); da = darray_calloc(1, sizeof(*bod)); if (depacket_da(buf, NULL, 0, da) == -1) perror("ERR DEBODY\n"); printf("da used %d\n",da->da_used); body_handler(head, (struct body_s*)da->da_values); darray_free(da); break; default: break; } free(bod); free(out); free(head); buf_free(buf); }
int main(int argc, char **argv) { FILE *fd, *fdw; char *orgbuf,*buf, *p, *ptr, *tt; int size, n, j; struct darray_s *darr, *darr_line; struct darray_s **item; char filename[50], outfile[50]; if (argc < 2) { printf("format error: csv2json {filename}\n"); exit(0); } strcpy(filename, argv[1]); strcpy(outfile, argv[1]); strcat(outfile, ".json"); printf("filename:%s, outfile:%s\n", filename, outfile); fd = fopen(filename, "r+"); if (fd == NULL) { perror("open input file err"); exit(0); } //fdw = fopen(strcat(argv[1],".json"), "w"); fdw = fopen(outfile, "w"); if (fdw == NULL) { perror("open write file err"); exit(0); } /* load data into dynamic array */ orgbuf = buf = (char*)calloc(1, LINE); darr = (u_char *)darray_calloc(linecount_fd(fd), sizeof(buf)); while (fgets(buf, LINE, fd)) { // printf("buf:%s\n", buf); darr_line = darray_calloc(strcount(buf, ',')+1, LINE); //printf("%p\n", darr_line); tt = darray_pushback(darr); //printf("1 values: %p\n", tt); memcpy(tt, &darr_line, sizeof(darr_line)); trim(buf); while (p = strchr(buf, ',')) { size = p-buf; ptr = darray_pushback(darr_line); //printf("2 buf:%s, size:%d, ptr:%p\n", buf, size, ptr); strncpy(ptr, buf, size); buf = p+1; trim(buf); } ptr = darray_pushback(darr_line); size = strlen(buf); //printf("2 buf:%s, size:%d, ptr:%p\n", buf, size, ptr); strncpy(ptr, buf, size); buf = orgbuf; memset(buf, 0, LINE); } /* write json file */ item = darr->da_values; if (darray_count(darr) > 3) fprintf(fdw, "["); for (n=3; n<darray_count(darr); n++) { fprintf(fdw, "{"); for (j=0; j<darray_count(*item); j++) { //printf("tt:%s\n", item[1]->da_values+j*item[1]->da_size); if (strcmp(item[1]->da_values+j*item[1]->da_size, "0") == 0) fprintf(fdw, "\"%s\":\"%s\"", item[0]->da_values+j*item[n]->da_size, item[n]->da_values+j*item[n]->da_size); else if (strcmp(item[1]->da_values+j*item[1]->da_size, "2") == 0) { fprintf(fdw, "\"%s\":\"%s\"", item[0]->da_values+j*item[n]->da_size, item[n]->da_values+j*item[n]->da_size); /* char tt[1024]; memset(tt, 0, 1024); printf("len:%d\n", strlen(*(item[n]->da_values+j*item[n]->da_size))); memcpy(tt, item[n]->da_values+j*item[n]->da_size+1, strlen(item[n]->da_values+j*item[n]->da_size)-2); fprintf(fdw, "\"%s\":%s", item[0]->da_values+j*item[n]->da_size, item[n]->da_values+j*item[n]->da_size); */ } else if (strcmp(item[1]->da_values+j*item[1]->da_size, "1") == 0) fprintf(fdw, "\"%s\":%d", item[0]->da_values+j*item[n]->da_size, atoi(item[n]->da_values+j*item[n]->da_size)); else printf("data tyep err."); if (j<darray_count(*item)-1) fprintf(fdw, ", "); } if (n < darray_count(darr)-1) fprintf(fdw, "}, \n"); else fprintf(fdw, "}"); } if (darray_count(darr) > 3) fprintf(fdw, "]\n"); free(orgbuf); darray_free(darr); fclose(fd); fclose(fdw); return 0; }