Skip to content

Commit 192e229

Browse files
committed
Changed method
1 parent b2a99be commit 192e229

File tree

4 files changed

+347
-345
lines changed

4 files changed

+347
-345
lines changed

demo/test.c

Lines changed: 74 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -323,18 +323,18 @@ static int test_mp_fread_fwrite(void)
323323

324324
#if (!(defined LTM_NOTHING) && !(defined MP_NO_FILE) && (defined __GLIBC__))
325325
#include <printf.h>
326-
#define LTM_TEST_BUFSIZ 1024
326+
#define MP_TEST_BUFSIZ 1024
327327
static int test_mp_printf_extension(void)
328328
{
329329
FILE *test_file = NULL;
330330

331-
char line_buffer[LTM_TEST_BUFSIZ] = {0};
331+
char line_buffer[MP_TEST_BUFSIZ] = {0};
332332
bool write_only = false;
333333
size_t slen = 0;
334334
int characters_printed = 0;
335335
char *fgets_return;
336336
int idx = 0;
337-
337+
/* TODO: test printing of all three flavours of mp_digit and the array */
338338
const char *test_values[41] = {
339339
"4DDCFDE0D20EF8663B34D19F829FDD",
340340
"-51D9769BDAE5B38121F2A31D881E5F"
@@ -401,72 +401,72 @@ static int test_mp_printf_extension(void)
401401
};
402402

403403
const char *print_strings[58] = {
404-
"Right aligned AAA %50N BBB\n",
405-
"Left aligned AAA %-50N BBB\n",
406-
"Right aligned AAA %+50N BBB\n",
407-
"Left aligned AAA %+-50N BBB\n",
408-
"Right aligned AAA %' '50N BBB\n",
409-
"Left aligned AAA %' '-50N BBB\n",
410-
411-
"hex with right align AAA %50kN BBB\n",
412-
"hex with left align AAA %-50kN BBB\n",
413-
"hex with right align AAA %+50kN BBB\n",
414-
"hex with left align AAA %+-50kN BBB\n",
415-
"hex with right align AAA %' '50kN BBB\n",
416-
"hex with left align AAA %' '-50kN BBB\n",
417-
418-
"hex with right align AAA %#50kN BBB\n",
419-
"hex with left align AAA %#-50kN BBB\n",
420-
"hex with right align AAA %#+50kN BBB\n",
421-
"hex with left align AAA %#+-50kN BBB\n",
422-
"hex with right align AAA %#' '50kN BBB\n",
423-
"hex with left align AAA %#' '-50kN BBB\n",
404+
"Right aligned AAA %50Zd BBB\n",
405+
"Left aligned AAA %-50Zd BBB\n",
406+
"Right aligned AAA %+50Zd BBB\n",
407+
"Left aligned AAA %+-50Zd BBB\n",
408+
"Right aligned AAA %' '50Zd BBB\n",
409+
"Left aligned AAA %' '-50Zd BBB\n",
410+
411+
"hex with right align AAA %50Zx BBB\n",
412+
"hex with left align AAA %-50Zx BBB\n",
413+
"hex with right align AAA %+50Zx BBB\n",
414+
"hex with left align AAA %+-50Zx BBB\n",
415+
"hex with right align AAA %' '50Zx BBB\n",
416+
"hex with left align AAA %' '-50Zx BBB\n",
417+
418+
"hex with right align AAA %#50Zx BBB\n",
419+
"hex with left align AAA %#-50Zx BBB\n",
420+
"hex with right align AAA %#+50Zx BBB\n",
421+
"hex with left align AAA %#+-50Zx BBB\n",
422+
"hex with right align AAA %#' '50Zx BBB\n",
423+
"hex with left align AAA %#' '-50Zx BBB\n",
424424
/* at idx == 18 mp_exch(&p,&q); */
425-
"Right aligned AAA %50N BBB\n",
426-
"Left aligned AAA %-50N BBB\n",
427-
"Right aligned AAA %+50N BBB\n",
428-
"Left aligned AAA %+-50N BBB\n",
429-
"Right aligned AAA %' '50N BBB\n",
430-
"Left aligned AAA %' '-50N BBB\n",
431-
432-
"hex with right align AAA %50kN BBB\n",
433-
"hex with left align AAA %-50kN BBB\n",
434-
"hex with right align AAA %+50kN BBB\n",
435-
"hex with left align AAA %+-50kN BBB\n",
436-
"hex with right align AAA %' '50kN BBB\n",
437-
"hex with left align AAA %' '-50kN BBB\n",
438-
439-
"hex with right align AAA %#50kN BBB\n",
440-
"hex with left align AAA %#-50kN BBB\n",
441-
"hex with right align AAA %#+50kN BBB\n",
442-
"hex with left align AAA %#+-50kN BBB\n",
443-
"hex with right align AAA %#' '50kN BBB\n",
444-
"hex with left align AAA %#' '-50kN BBB\n",
425+
"Right aligned AAA %50Zd BBB\n",
426+
"Left aligned AAA %-50Zd BBB\n",
427+
"Right aligned AAA %+50Zd BBB\n",
428+
"Left aligned AAA %+-50Zd BBB\n",
429+
"Right aligned AAA %' '50Zd BBB\n",
430+
"Left aligned AAA %' '-50Zd BBB\n",
431+
432+
"hex with right align AAA %50Zx BBB\n",
433+
"hex with left align AAA %-50Zx BBB\n",
434+
"hex with right align AAA %+50Zx BBB\n",
435+
"hex with left align AAA %+-50Zx BBB\n",
436+
"hex with right align AAA %' '50Zx BBB\n",
437+
"hex with left align AAA %' '-50Zx BBB\n",
438+
439+
"hex with right align AAA %#50Zx BBB\n",
440+
"hex with left align AAA %#-50Zx BBB\n",
441+
"hex with right align AAA %#+50Zx BBB\n",
442+
"hex with left align AAA %#+-50Zx BBB\n",
443+
"hex with right align AAA %#' '50Zx BBB\n",
444+
"hex with left align AAA %#' '-50Zx BBB\n",
445445

446446
/* at idx == 36 mp_zero(&p); */
447-
"Right aligned AAA %50N BBB\n",
448-
"Left aligned AAA %-50N BBB\n",
449-
"hex with right align AAA %#50kN BBB\n",
450-
"hex with left align AAA %#-50kN BBB\n",
447+
"Right aligned AAA %50Zd BBB\n",
448+
"Left aligned AAA %-50Zd BBB\n",
449+
"hex with right align AAA %#50Zx BBB\n",
450+
"hex with left align AAA %#-50Zx BBB\n",
451451
/* at idx == 40 mp_exch(&p,&q); */
452-
"Right aligned AAA %50bN BBB\n",
453-
"Left aligned AAA %-50bN BBB\n",
454-
"Right aligned AAA %+50bN BBB\n",
455-
"Left aligned AAA %+-50bN BBB\n",
456-
"Right aligned AAA %' '50bN BBB\n",
457-
"Left aligned AAA %' '-50bN BBB\n",
458-
"Right aligned AAA %50@N BBB\n",
459-
"Left aligned AAA %-50@N BBB\n",
460-
"Right aligned AAA %+50@N BBB\n",
461-
"Left aligned AAA %+-50@N BBB\n",
462-
"Right aligned AAA %' '50@N BBB\n",
463-
"Left aligned AAA %' '-50@N BBB\n",
464-
"Right aligned AAA %#50@N BBB\n",
465-
"Left aligned AAA %#-50@N BBB\n",
466-
"Right aligned AAA %#+50@N BBB\n",
467-
"Left aligned AAA %#+-50@N BBB\n",
468-
"Right aligned AAA %#' '50@N BBB\n",
469-
"Left aligned AAA %#' '-50@N BBB\n"
452+
"Right aligned AAA %50Zb BBB\n",
453+
"Left aligned AAA %-50Zb BBB\n",
454+
"Right aligned AAA %+50Zb BBB\n",
455+
"Left aligned AAA %+-50Zb BBB\n",
456+
"Right aligned AAA %' '50Zb BBB\n",
457+
"Left aligned AAA %' '-50Zb BBB\n",
458+
"Right aligned AAA %50Z@ BBB\n",
459+
"Left aligned AAA %-50Z@ BBB\n",
460+
"Right aligned AAA %+50Z@ BBB\n",
461+
"Left aligned AAA %+-50Z@ BBB\n",
462+
"Right aligned AAA %' '50Z@ BBB\n",
463+
"Left aligned AAA %' '-50Z@ BBB\n",
464+
"Right aligned AAA %#50Z@ BBB\n",
465+
"Left aligned AAA %#-50Z@ BBB\n",
466+
"Right aligned AAA %#+50Z@ BBB\n",
467+
"Left aligned AAA %#+-50Z@ BBB\n",
468+
"Right aligned AAA %#' '50Z@ BBB\n",
469+
"Left aligned AAA %#' '-50Z@ BBB\n"
470470
};
471471

472472
mp_int p, q;
@@ -483,7 +483,7 @@ static int test_mp_printf_extension(void)
483483
DO(mp_read_radix(&p, test_values[0], 16));
484484
DO(mp_read_radix(&q, test_values[1], 16));
485485

486-
DO(mp_printf_extension());
486+
DO(mp_printf_extension_init());
487487

488488
for (idx = 0; idx < 52; idx++) {
489489
if (idx == 18) {
@@ -498,6 +498,7 @@ static int test_mp_printf_extension(void)
498498
characters_printed = fprintf(test_file, print_strings[idx], &p);
499499
slen = strlen(test_strings[idx]);
500500
if ((characters_printed - (int)slen) != 0) {
501+
mp_printf_extension_clear();
501502
fprintf(stderr, "%d test_mp_print_extension: failed to print o:%zu t:%d\n",
502503
idx, slen, characters_printed);
503504
fprintf(stderr,"\"%s\"\n",test_strings[idx]);
@@ -506,12 +507,14 @@ static int test_mp_printf_extension(void)
506507
}
507508
if (!write_only) {
508509
rewind(test_file);
509-
fgets_return = fgets(line_buffer, LTM_TEST_BUFSIZ, test_file);
510+
fgets_return = fgets(line_buffer, MP_TEST_BUFSIZ, test_file);
510511
if (fgets_return == NULL) {
512+
mp_printf_extension_clear();
511513
fprintf(stderr, "%d test_mp_fprintf: failed to read from file\n", idx);
512514
goto LBL_ERR;
513515
}
514516
if (strcmp(line_buffer, test_strings[idx]) != 0) {
517+
mp_printf_extension_clear();
515518
fprintf(stderr, "test_mp_fprintf: file content is not equal to test string #%d\n",idx);
516519
goto LBL_ERR;
517520
}
@@ -520,21 +523,23 @@ static int test_mp_printf_extension(void)
520523
test_file = freopen("ltm_testing_mp_fprintf_88a43603fcfc2f7e7c6646cd4b89180a","w+", test_file);
521524
if (test_file == NULL) {
522525
/* use logfile instead to have at least sth. in case of an error */
523-
test_file = stdout;
526+
test_file = stderr;
524527
write_only = true;
525528
}
526529
}
527530

528531
mp_clear_multi(&p, &q, NULL);
529532
fclose(test_file);
533+
mp_printf_extension_clear();
530534
if (remove("ltm_testing_mp_fprintf_88a43603fcfc2f7e7c6646cd4b89180a") != 0) {
531535
fprintf(stderr, "Could not delete file ltm_testing_mp_fprintf_88a43603fcfc2f7e7c6646cd4b89180a\n");
532536
}
533537
return EXIT_SUCCESS;
534538
LBL_ERR:
535539
mp_clear_multi(&p, &q, NULL);
536540
fclose(test_file);
537-
/* We don't delete the testfile in case of error, conrtent might be helpful. */
541+
mp_printf_extension_clear();
542+
/* We don't delete the testfile in case of error, content might be helpful. */
538543
return EXIT_FAILURE;
539544
}
540545
#endif

doc/bn.tex

Lines changed: 46 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2423,32 +2423,63 @@ \subsection{To ASCII}
24232423

24242424
Also available in that case is a small extension to \texttt{printf(3)} to print
24252425
a big integer in a formated way. Not every formating is supported (e.g.: no
2426-
thousands separator, no leading zeros) but normal alignment works well.
2426+
thousands separator) but normal alignment works well.
24272427

2428-
Modifiers (\texttt{k,b,r,@})\footnote{There are only so many letters in the alphabet
2429-
and almost all useful ones were already taken, so it is \texttt{k} for hexadecimal representation like in ``eks'' and
2430-
\texttt{r} like in Rear Admiral Grace Brewster Murray Hopper (07 r's) for octals.}
2431-
come before the specifier. See example below for the necessary details.
2428+
Modifiers are \texttt{Z} for a big integer \texttt{M} for a \texttt{mp\_digit} and
2429+
\texttt{N} to print the array \texttt{a->dp} of a \texttt{mp\_int a}.
24322430

2433-
This function is not threadsafe!
2434-
\index{mp\_printf\_extension}
2431+
Specifiers are \texttt{d, x, o, b, @} for decimal, hexadecimal, octal, binary, and
2432+
base-64 representations respectively. These specifiers are bound to the extensions
2433+
but can be unregistered individually or all together
2434+
2435+
See example below for the necessary details.
2436+
2437+
This functions are not threadsafe!
2438+
\index{mp\_printf\_extension\_init}
2439+
\index{mp\_printf\_extension\_clear}
24352440
\begin{alltt}
2436-
mp_err p_printf_extension(void);
2441+
mp_err mp_printf_extension_init(void);
2442+
void mp_printf_extension_clear(void);
24372443
\end{alltt}
2444+
2445+
24382446
Example:
24392447
\begin{alltt}
2448+
24402449
/* Switch on the extension */
2441-
if((err = mp_printf_extension(void)) != MP_OKAY) goto LTM_ERR;
2450+
if((err = mp_printf_extension_init()) != MP_OKAY) goto LTM_ERR;
24422451
...
2443-
/* Do some calculation */
2452+
/* Do some calculation with big integer a */
24442453
...
2445-
/* print the results */
24462454
2447-
printf("In hex: %kN and in decimal: %N\textbackslash{}n", &a, &a);
2448-
printf("In bin: %bN and in base 64: %@N\textbackslash{}n", &a, &a);
2449-
printf("And finally octal: %oN \textbackslash{}n", &a);
2455+
printf("Bigint decimal: %Zd \textbackslash{}n", &a);
2456+
printf("Bigint hexadecimal: %Zx \textbackslash{}n", &a);
2457+
printf("Bigint octal: %Zo \textbackslash{}n", &a);
2458+
printf("Bigint binary: %Zb \textbackslash{}n", &a);
2459+
printf("Bigint base-64: %Z@ \textbackslash{}n", &a);
2460+
2461+
printf("Limb decimal: %Md \textbackslash{}n", a.dp[0]);
2462+
printf("Limb hexdecimal: %Mx \textbackslash{}n", a.dp[0]);
2463+
/* and so on */
2464+
2465+
printf("Array decimal: %Nd \textbackslash{}n", &a);
2466+
printf("Array hexadecimal: %Nx \textbackslash{}n", &a);
2467+
2468+
#include <printf.h>
2469+
register_printf_specifier('d', NULL, NULL);
2470+
printf("Bigint number %d: %Zx \textbackslash{}n", 123, &a);
2471+
2472+
/* re-registering is only possible completely */
2473+
if((err = mp_printf_extension_init()) != MP_OKAY) goto LTM_ERR;
2474+
printf("Bigint number %i: %Zd \textbackslash{}n", 123, &a);
2475+
2476+
/* and so on */
2477+
2478+
/* Switch off all extension */
2479+
mp_printf_extension_clear()
2480+
24502481
\end{alltt}
2451-
Returns \texttt{MP\_VAL} if that function is not supported.
2482+
\textttt{mp\_printf\_extension\_init} returns \texttt{MP\_VAL} if those functions are not supported.
24522483

24532484
\subsection{From ASCII}
24542485
\index{mp\_read\_radix}

0 commit comments

Comments
 (0)