outres.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "orasel.h"
  2. //-----------------------------------------------------------------------------
  3. static void out_delim(const unsigned* pl, unsigned nc)
  4. {
  5. unsigned i;
  6. for(i = 0; i < nc; i++) {
  7. unsigned j = pl[i] + 2;
  8. putchar('+');
  9. do putchar('-'); while(--j);
  10. }
  11. printf("+\n");
  12. }
  13. //-----------------------------------------------------------------------------
  14. void out_res(const res_t* _r)
  15. {
  16. unsigned* pl = NULL;
  17. unsigned nc = _r->col_n, nr = _r->row_n, i, j;
  18. Str** ps = _r->names;
  19. if(!outmode.raw) {
  20. pl = safe_malloc(nc * sizeof(unsigned));
  21. for(i = 0; i < nc; i++)
  22. pl[i] = ps[i]->len;
  23. for(j = 0; j < nr; j++) {
  24. ps = _r->rows[j];
  25. for(i = 0; i < nc; i++)
  26. if(pl[i] < ps[i]->len) pl[i] = ps[i]->len;
  27. }
  28. out_delim(pl, nc);
  29. }
  30. if(!outmode.hdr) {
  31. ps = _r->names;
  32. for(i = 0; i < nc; i++) {
  33. if(!outmode.raw) {
  34. printf("| %-*.*s ", pl[i], ps[i]->len, ps[i]->s);
  35. } else {
  36. if(i) putchar('\t');
  37. printf("%.*s", ps[i]->len, ps[i]->s);
  38. }
  39. }
  40. if(outmode.raw) putchar('\n');
  41. else {
  42. printf("|\n");
  43. out_delim(pl, nc);
  44. }
  45. }
  46. for(j = 0; j < nr; j++) {
  47. ps = _r->rows[j];
  48. if(!outmode.raw) {
  49. for(i = 0; i < nc; i++)
  50. printf(_r->types[i] ? "| %-*.*s " : "| %*.*s ",
  51. pl[i], ps[i]->len, ps[i]->s);
  52. printf("|\n");
  53. } else {
  54. for(i = 0; i < nc; i++) {
  55. if(i) putchar('\t');
  56. printf("%.*s", ps[i]->len, ps[i]->s);
  57. }
  58. putchar('\n');
  59. }
  60. }
  61. if(!outmode.raw) out_delim(pl, nc);
  62. }
  63. //-----------------------------------------------------------------------------