|
3 | 3 | * |
4 | 4 | * Copyright (c) 2000-2005, PostgreSQL Global Development Group |
5 | 5 | * |
6 | | - * $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.75 2005/09/26 18:09:57 momjian Exp $ |
| 6 | + * $PostgreSQL: pgsql/src/bin/psql/print.c,v 1.76 2005/09/27 16:30:25 momjian Exp $ |
7 | 7 | */ |
8 | 8 | #include"postgres_fe.h" |
9 | 9 | #include"common.h" |
@@ -86,22 +86,26 @@ strlen_with_numeric_locale(const char *my_str) |
86 | 86 | returnstrlen(my_str)+additional_numeric_locale_len(my_str); |
87 | 87 | } |
88 | 88 |
|
89 | | -staticvoid |
90 | | -format_numeric_locale(char*my_str) |
| 89 | +staticchar* |
| 90 | +format_numeric_locale(constchar*my_str) |
91 | 91 | { |
92 | 92 | inti,j,int_len=integer_digits(my_str),leading_digits; |
93 | 93 | intgroupdigits=atoi(grouping); |
94 | | -char*new_str; |
95 | | - |
96 | | -if (my_str[0]=='-') |
97 | | -my_str++; |
98 | | - |
99 | | -new_str=pg_local_malloc(strlen_with_numeric_locale(my_str)+1); |
| 94 | +intnew_str_start=0; |
| 95 | +char*new_str=new_str=pg_local_malloc( |
| 96 | +strlen_with_numeric_locale(my_str)+1); |
100 | 97 |
|
101 | 98 | leading_digits= (int_len %groupdigits!=0) ? |
102 | 99 | int_len %groupdigits :groupdigits; |
103 | 100 |
|
104 | | -for (i=0,j=0; ;i++,j++) |
| 101 | +if (my_str[0]=='-')/* skip over sign, affects grouping calculations */ |
| 102 | +{ |
| 103 | +new_str[0]=my_str[0]; |
| 104 | +my_str++; |
| 105 | +new_str_start=1; |
| 106 | +} |
| 107 | + |
| 108 | +for (i=0,j=new_str_start; ;i++,j++) |
105 | 109 | { |
106 | 110 | /* Hit decimal point? */ |
107 | 111 | if (my_str[i]=='.') |
@@ -130,8 +134,7 @@ format_numeric_locale(char *my_str) |
130 | 134 | new_str[j]=my_str[i]; |
131 | 135 | } |
132 | 136 |
|
133 | | -strcpy(my_str,new_str); |
134 | | -free(new_str); |
| 137 | +returnnew_str; |
135 | 138 | } |
136 | 139 |
|
137 | 140 | /*************************/ |
@@ -185,10 +188,8 @@ print_unaligned_text(const char *title, const char *const *headers, |
185 | 188 | } |
186 | 189 | if (opt_align[i %col_count]=='r'&&opt_numeric_locale) |
187 | 190 | { |
188 | | -char*my_cell=pg_local_malloc(strlen_with_numeric_locale(*ptr)+1); |
189 | | - |
190 | | -strcpy(my_cell,*ptr); |
191 | | -format_numeric_locale(my_cell); |
| 191 | +char*my_cell=format_numeric_locale(*ptr); |
| 192 | + |
192 | 193 | fputs(my_cell,fout); |
193 | 194 | free(my_cell); |
194 | 195 | } |
@@ -261,10 +262,8 @@ print_unaligned_vertical(const char *title, const char *const *headers, |
261 | 262 | fputs(opt_fieldsep,fout); |
262 | 263 | if (opt_align[i %col_count]=='r'&&opt_numeric_locale) |
263 | 264 | { |
264 | | -char*my_cell=pg_local_malloc(strlen_with_numeric_locale(*ptr)+1); |
265 | | - |
266 | | -strcpy(my_cell,*ptr); |
267 | | -format_numeric_locale(my_cell); |
| 265 | +char*my_cell=format_numeric_locale(*ptr); |
| 266 | + |
268 | 267 | fputs(my_cell,fout); |
269 | 268 | free(my_cell); |
270 | 269 | } |
@@ -488,13 +487,11 @@ print_aligned_text(const char *title, const char *const *headers, |
488 | 487 | { |
489 | 488 | if (opt_numeric_locale) |
490 | 489 | { |
491 | | -char*my_cell=pg_local_malloc(cell_w[i]+1); |
| 490 | +char*my_cell=format_numeric_locale(*ptr); |
492 | 491 |
|
493 | | -strcpy(my_cell,*ptr); |
494 | | -format_numeric_locale(my_cell); |
495 | 492 | fprintf(fout,"%*s%s",widths[i %col_count]-cell_w[i],"",my_cell); |
496 | 493 | free(my_cell); |
497 | | -} |
| 494 | +} |
498 | 495 | else |
499 | 496 | fprintf(fout,"%*s%s",widths[i %col_count]-cell_w[i],"",*ptr); |
500 | 497 | } |
@@ -697,18 +694,23 @@ print_aligned_vertical(const char *title, const char *const *headers, |
697 | 694 | else |
698 | 695 | fputs(" ",fout); |
699 | 696 |
|
| 697 | +if (opt_align[i %col_count]=='r'&&opt_numeric_locale) |
700 | 698 | { |
701 | | -char*my_cell=pg_local_malloc(cell_w[i]+1); |
702 | | - |
703 | | -strcpy(my_cell,*ptr); |
704 | | -if (opt_align[i %col_count]=='r'&&opt_numeric_locale) |
705 | | -format_numeric_locale(my_cell); |
| 699 | +char*my_cell=format_numeric_locale(*ptr); |
| 700 | + |
706 | 701 | if (opt_border<2) |
707 | 702 | fprintf(fout,"%s\n",my_cell); |
708 | 703 | else |
709 | 704 | fprintf(fout,"%-s%*s |\n",my_cell,dwidth-cell_w[i],""); |
710 | 705 | free(my_cell); |
711 | 706 | } |
| 707 | +else |
| 708 | +{ |
| 709 | +if (opt_border<2) |
| 710 | +fprintf(fout,"%s\n",*ptr); |
| 711 | +else |
| 712 | +fprintf(fout,"%-s%*s |\n",*ptr,dwidth-cell_w[i],""); |
| 713 | +} |
712 | 714 | } |
713 | 715 |
|
714 | 716 | if (opt_border==2) |
@@ -837,10 +839,8 @@ print_html_text(const char *title, const char *const *headers, |
837 | 839 | fputs(" ",fout); |
838 | 840 | elseif (opt_align[i %col_count]=='r'&&opt_numeric_locale) |
839 | 841 | { |
840 | | -char*my_cell=pg_local_malloc(strlen_with_numeric_locale(*ptr)+1); |
| 842 | +char*my_cell=format_numeric_locale(*ptr); |
841 | 843 |
|
842 | | -strcpy(my_cell,*ptr); |
843 | | -format_numeric_locale(my_cell); |
844 | 844 | html_escaped_print(my_cell,fout); |
845 | 845 | free(my_cell); |
846 | 846 | } |
@@ -922,10 +922,8 @@ print_html_vertical(const char *title, const char *const *headers, |
922 | 922 | fputs(" ",fout); |
923 | 923 | elseif (opt_align[i %col_count]=='r'&&opt_numeric_locale) |
924 | 924 | { |
925 | | -char*my_cell=pg_local_malloc(strlen_with_numeric_locale(*ptr)+1); |
926 | | - |
927 | | -strcpy(my_cell,*ptr); |
928 | | -format_numeric_locale(my_cell); |
| 925 | +char*my_cell=format_numeric_locale(*ptr); |
| 926 | + |
929 | 927 | html_escaped_print(my_cell,fout); |
930 | 928 | free(my_cell); |
931 | 929 | } |
@@ -1064,10 +1062,8 @@ print_latex_text(const char *title, const char *const *headers, |
1064 | 1062 | { |
1065 | 1063 | if (opt_numeric_locale) |
1066 | 1064 | { |
1067 | | -char*my_cell=pg_local_malloc(strlen_with_numeric_locale(*ptr)+1); |
1068 | | - |
1069 | | -strcpy(my_cell,*ptr); |
1070 | | -format_numeric_locale(my_cell); |
| 1065 | +char*my_cell=format_numeric_locale(*ptr); |
| 1066 | + |
1071 | 1067 | latex_escaped_print(my_cell,fout); |
1072 | 1068 | free(my_cell); |
1073 | 1069 | } |
@@ -1177,10 +1173,8 @@ print_latex_vertical(const char *title, const char *const *headers, |
1177 | 1173 | { |
1178 | 1174 | if (opt_numeric_locale) |
1179 | 1175 | { |
1180 | | -char*my_cell=pg_local_malloc(strlen_with_numeric_locale(*ptr)+1); |
| 1176 | +char*my_cell=format_numeric_locale(*ptr); |
1181 | 1177 |
|
1182 | | -strcpy(my_cell,*ptr); |
1183 | | -format_numeric_locale(my_cell); |
1184 | 1178 | latex_escaped_print(my_cell,fout); |
1185 | 1179 | free(my_cell); |
1186 | 1180 | } |
@@ -1277,10 +1271,8 @@ print_troff_ms_text(const char *title, const char *const *headers, |
1277 | 1271 | { |
1278 | 1272 | if (opt_numeric_locale) |
1279 | 1273 | { |
1280 | | -char*my_cell=pg_local_malloc(strlen_with_numeric_locale(*ptr)+1); |
1281 | | - |
1282 | | -strcpy(my_cell,*ptr); |
1283 | | -format_numeric_locale(my_cell); |
| 1274 | +char*my_cell=format_numeric_locale(*ptr); |
| 1275 | + |
1284 | 1276 | troff_ms_escaped_print(my_cell,fout); |
1285 | 1277 | free(my_cell); |
1286 | 1278 | } |
@@ -1389,10 +1381,8 @@ print_troff_ms_vertical(const char *title, const char *const *headers, |
1389 | 1381 | fputc('\t',fout); |
1390 | 1382 | if (opt_numeric_locale) |
1391 | 1383 | { |
1392 | | -char*my_cell=pg_local_malloc(strlen_with_numeric_locale(*ptr)+1); |
1393 | | - |
1394 | | -strcpy(my_cell,*ptr); |
1395 | | -format_numeric_locale(my_cell); |
| 1384 | +char*my_cell=format_numeric_locale(*ptr); |
| 1385 | + |
1396 | 1386 | troff_ms_escaped_print(my_cell,fout); |
1397 | 1387 | free(my_cell); |
1398 | 1388 | } |
|