Skip to content

Commit 89ba70f

Browse files
committed
Changed method
1 parent 0f05820 commit 89ba70f

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
@@ -317,18 +317,18 @@ static int test_mp_fread_fwrite(void)
317317

318318
#if (!(defined LTM_NOTHING) && !(defined MP_NO_FILE) && (defined __GLIBC__))
319319
#include <printf.h>
320-
#define LTM_TEST_BUFSIZ 1024
320+
#define MP_TEST_BUFSIZ 1024
321321
static int test_mp_printf_extension(void)
322322
{
323323
FILE *test_file = NULL;
324324

325-
char line_buffer[LTM_TEST_BUFSIZ] = {0};
325+
char line_buffer[MP_TEST_BUFSIZ] = {0};
326326
bool write_only = false;
327327
size_t slen = 0;
328328
int characters_printed = 0;
329329
char *fgets_return;
330330
int idx = 0;
331-
331+
/* TODO: test printing of all three flavours of mp_digit and the array */
332332
const char *test_values[41] = {
333333
"4DDCFDE0D20EF8663B34D19F829FDD",
334334
"-51D9769BDAE5B38121F2A31D881E5F"
@@ -395,72 +395,72 @@ static int test_mp_printf_extension(void)
395395
};
396396

397397
const char *print_strings[58] = {
398-
"Right aligned AAA %50N BBB\n",
399-
"Left aligned AAA %-50N BBB\n",
400-
"Right aligned AAA %+50N BBB\n",
401-
"Left aligned AAA %+-50N BBB\n",
402-
"Right aligned AAA %' '50N BBB\n",
403-
"Left aligned AAA %' '-50N BBB\n",
404-
405-
"hex with right align AAA %50kN BBB\n",
406-
"hex with left align AAA %-50kN BBB\n",
407-
"hex with right align AAA %+50kN BBB\n",
408-
"hex with left align AAA %+-50kN BBB\n",
409-
"hex with right align AAA %' '50kN BBB\n",
410-
"hex with left align AAA %' '-50kN BBB\n",
411-
412-
"hex with right align AAA %#50kN BBB\n",
413-
"hex with left align AAA %#-50kN BBB\n",
414-
"hex with right align AAA %#+50kN BBB\n",
415-
"hex with left align AAA %#+-50kN BBB\n",
416-
"hex with right align AAA %#' '50kN BBB\n",
417-
"hex with left align AAA %#' '-50kN BBB\n",
398+
"Right aligned AAA %50Zd BBB\n",
399+
"Left aligned AAA %-50Zd BBB\n",
400+
"Right aligned AAA %+50Zd BBB\n",
401+
"Left aligned AAA %+-50Zd BBB\n",
402+
"Right aligned AAA %' '50Zd BBB\n",
403+
"Left aligned AAA %' '-50Zd BBB\n",
404+
405+
"hex with right align AAA %50Zx BBB\n",
406+
"hex with left align AAA %-50Zx BBB\n",
407+
"hex with right align AAA %+50Zx BBB\n",
408+
"hex with left align AAA %+-50Zx BBB\n",
409+
"hex with right align AAA %' '50Zx BBB\n",
410+
"hex with left align AAA %' '-50Zx BBB\n",
411+
412+
"hex with right align AAA %#50Zx BBB\n",
413+
"hex with left align AAA %#-50Zx BBB\n",
414+
"hex with right align AAA %#+50Zx BBB\n",
415+
"hex with left align AAA %#+-50Zx BBB\n",
416+
"hex with right align AAA %#' '50Zx BBB\n",
417+
"hex with left align AAA %#' '-50Zx BBB\n",
418418
/* at idx == 18 mp_exch(&p,&q); */
419-
"Right aligned AAA %50N BBB\n",
420-
"Left aligned AAA %-50N BBB\n",
421-
"Right aligned AAA %+50N BBB\n",
422-
"Left aligned AAA %+-50N BBB\n",
423-
"Right aligned AAA %' '50N BBB\n",
424-
"Left aligned AAA %' '-50N BBB\n",
425-
426-
"hex with right align AAA %50kN BBB\n",
427-
"hex with left align AAA %-50kN BBB\n",
428-
"hex with right align AAA %+50kN BBB\n",
429-
"hex with left align AAA %+-50kN BBB\n",
430-
"hex with right align AAA %' '50kN BBB\n",
431-
"hex with left align AAA %' '-50kN BBB\n",
432-
433-
"hex with right align AAA %#50kN BBB\n",
434-
"hex with left align AAA %#-50kN BBB\n",
435-
"hex with right align AAA %#+50kN BBB\n",
436-
"hex with left align AAA %#+-50kN BBB\n",
437-
"hex with right align AAA %#' '50kN BBB\n",
438-
"hex with left align AAA %#' '-50kN BBB\n",
419+
"Right aligned AAA %50Zd BBB\n",
420+
"Left aligned AAA %-50Zd BBB\n",
421+
"Right aligned AAA %+50Zd BBB\n",
422+
"Left aligned AAA %+-50Zd BBB\n",
423+
"Right aligned AAA %' '50Zd BBB\n",
424+
"Left aligned AAA %' '-50Zd BBB\n",
425+
426+
"hex with right align AAA %50Zx BBB\n",
427+
"hex with left align AAA %-50Zx BBB\n",
428+
"hex with right align AAA %+50Zx BBB\n",
429+
"hex with left align AAA %+-50Zx BBB\n",
430+
"hex with right align AAA %' '50Zx BBB\n",
431+
"hex with left align AAA %' '-50Zx BBB\n",
432+
433+
"hex with right align AAA %#50Zx BBB\n",
434+
"hex with left align AAA %#-50Zx BBB\n",
435+
"hex with right align AAA %#+50Zx BBB\n",
436+
"hex with left align AAA %#+-50Zx BBB\n",
437+
"hex with right align AAA %#' '50Zx BBB\n",
438+
"hex with left align AAA %#' '-50Zx BBB\n",
439439

440440
/* at idx == 36 mp_zero(&p); */
441-
"Right aligned AAA %50N BBB\n",
442-
"Left aligned AAA %-50N BBB\n",
443-
"hex with right align AAA %#50kN BBB\n",
444-
"hex with left align AAA %#-50kN BBB\n",
441+
"Right aligned AAA %50Zd BBB\n",
442+
"Left aligned AAA %-50Zd BBB\n",
443+
"hex with right align AAA %#50Zx BBB\n",
444+
"hex with left align AAA %#-50Zx BBB\n",
445445
/* at idx == 40 mp_exch(&p,&q); */
446-
"Right aligned AAA %50bN BBB\n",
447-
"Left aligned AAA %-50bN BBB\n",
448-
"Right aligned AAA %+50bN BBB\n",
449-
"Left aligned AAA %+-50bN BBB\n",
450-
"Right aligned AAA %' '50bN BBB\n",
451-
"Left aligned AAA %' '-50bN BBB\n",
452-
"Right aligned AAA %50@N BBB\n",
453-
"Left aligned AAA %-50@N BBB\n",
454-
"Right aligned AAA %+50@N BBB\n",
455-
"Left aligned AAA %+-50@N BBB\n",
456-
"Right aligned AAA %' '50@N BBB\n",
457-
"Left aligned AAA %' '-50@N 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"
446+
"Right aligned AAA %50Zb BBB\n",
447+
"Left aligned AAA %-50Zb BBB\n",
448+
"Right aligned AAA %+50Zb BBB\n",
449+
"Left aligned AAA %+-50Zb BBB\n",
450+
"Right aligned AAA %' '50Zb BBB\n",
451+
"Left aligned AAA %' '-50Zb BBB\n",
452+
"Right aligned AAA %50Z@ BBB\n",
453+
"Left aligned AAA %-50Z@ BBB\n",
454+
"Right aligned AAA %+50Z@ BBB\n",
455+
"Left aligned AAA %+-50Z@ BBB\n",
456+
"Right aligned AAA %' '50Z@ BBB\n",
457+
"Left aligned AAA %' '-50Z@ 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"
464464
};
465465

466466
mp_int p, q;
@@ -477,7 +477,7 @@ static int test_mp_printf_extension(void)
477477
DO(mp_read_radix(&p, test_values[0], 16));
478478
DO(mp_read_radix(&q, test_values[1], 16));
479479

480-
DO(mp_printf_extension());
480+
DO(mp_printf_extension_init());
481481

482482
for (idx = 0; idx < 52; idx++) {
483483
if (idx == 18) {
@@ -492,6 +492,7 @@ static int test_mp_printf_extension(void)
492492
characters_printed = fprintf(test_file, print_strings[idx], &p);
493493
slen = strlen(test_strings[idx]);
494494
if ((characters_printed - (int)slen) != 0) {
495+
mp_printf_extension_clear();
495496
fprintf(stderr, "%d test_mp_print_extension: failed to print o:%zu t:%d\n",
496497
idx, slen, characters_printed);
497498
fprintf(stderr,"\"%s\"\n",test_strings[idx]);
@@ -500,12 +501,14 @@ static int test_mp_printf_extension(void)
500501
}
501502
if (!write_only) {
502503
rewind(test_file);
503-
fgets_return = fgets(line_buffer, LTM_TEST_BUFSIZ, test_file);
504+
fgets_return = fgets(line_buffer, MP_TEST_BUFSIZ, test_file);
504505
if (fgets_return == NULL) {
506+
mp_printf_extension_clear();
505507
fprintf(stderr, "%d test_mp_fprintf: failed to read from file\n", idx);
506508
goto LBL_ERR;
507509
}
508510
if (strcmp(line_buffer, test_strings[idx]) != 0) {
511+
mp_printf_extension_clear();
509512
fprintf(stderr, "test_mp_fprintf: file content is not equal to test string #%d\n",idx);
510513
goto LBL_ERR;
511514
}
@@ -514,21 +517,23 @@ static int test_mp_printf_extension(void)
514517
test_file = freopen("ltm_testing_mp_fprintf_88a43603fcfc2f7e7c6646cd4b89180a","w+", test_file);
515518
if (test_file == NULL) {
516519
/* use logfile instead to have at least sth. in case of an error */
517-
test_file = stdout;
520+
test_file = stderr;
518521
write_only = true;
519522
}
520523
}
521524

522525
mp_clear_multi(&p, &q, NULL);
523526
fclose(test_file);
527+
mp_printf_extension_clear();
524528
if (remove("ltm_testing_mp_fprintf_88a43603fcfc2f7e7c6646cd4b89180a") != 0) {
525529
fprintf(stderr, "Could not delete file ltm_testing_mp_fprintf_88a43603fcfc2f7e7c6646cd4b89180a\n");
526530
}
527531
return EXIT_SUCCESS;
528532
LBL_ERR:
529533
mp_clear_multi(&p, &q, NULL);
530534
fclose(test_file);
531-
/* We don't delete the testfile in case of error, conrtent might be helpful. */
535+
mp_printf_extension_clear();
536+
/* We don't delete the testfile in case of error, content might be helpful. */
532537
return EXIT_FAILURE;
533538
}
534539
#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)