1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789
|
/*! \file util.c
\brief Various useful functions.
*/
/*! \var static char mtab1[12][4];
The list of the months.
*/
/*! \fn void getword_start(struct getwordstruct *gwarea, char *line)
Initialize the getword buffer with the given text line.
\param gwarea The getword buffer to initialize.
\param line The text line to use in the getword buffer.
*/
/*! \fn void getword_restart(struct getwordstruct *gwarea)
Restart the getword buffer from the beginning.
\param gwarea The getword buffer to reset.
*/
/*! \fn int getword(char *word, int limit, struct getwordstruct *gwarea, char stop)
Extract one "word" from the text line and remove it from the text line. The word's boundary is defined
by the \a stop character. If multiple stop characters are found after the word, only the first one is
removed. Therfore, passing the line buffer again to the function will remove the next word in a column
like manner.
\param word The buffer to store the extracted word.
\param limit The size of the buffer. If the stop character isn't found before that limit is reached,
the function displays an error message and returns an error code.
\param gwarea The getword buffer initialized by getword_start().
\param stop The character indicating the end of the word.
\retval 0 The word is extracted.
\retval -1 The stop character was not found before the limit is reached.
*/
/*! \fn int getword_limit(char *word, int limit, struct getwordstruct *gwarea, char stop)
Extract one word with a maximum size and skip any supernumerary bytes until the stop bytes is
found.
\param word The buffer to store the extracted word.
\param limit The size of the buffer.
\param gwarea The getword buffer initialized by getword_start().
\param stop The character indicating the end of the word.
\retval 0 The word is extracted.
*/
/*! \fn int getword_multisep(char *word, int limit, struct getwordstruct *gwarea, char stop)
Extract one "word" from the text line and remove it from the text line. The word's boundary is defined
by the \a stop character. All the stop characters following the word are removed too. Therefore, passing
the line buffer again to the function will remove words even if they are separated by multiple stop
characters.
\param word The buffer to store the extracted word.
\param limit The size of the buffer. If the stop character isn't found before that limit is reached,
the function displays an error message and returns an error code.
\param gwarea The getword buffer initialized by getword_start().
\param stop The character indicating the end of the word.
\retval 0 The word is extracted.
\retval -1 The stop character was not found before the limit is reached.
*/
/*! \fn int getword_skip(int limit, struct getwordstruct *gwarea, char stop)
Skip one "word" from the text line and remove it from the text line. The word's boundary is defined
by the \a stop character.
\param limit The maximum number of characters to skip. If the stop character isn't found before that limit is reached,
the function displays an error message and returns an error code.
\param gwarea The getword buffer initialized by getword_start().
\param stop The character indicating the end of the word.
\retval 0 The word is skipped.
\retval -1 The stop character was not found before the limit is reached.
*/
/*! \fn int getword_atoll(long long int *number, struct getwordstruct *gwarea, char stop)
Extract one number from the text line.
\param number Where the store the extracted number.
\param gwarea The getword buffer initialized by getword_start().
\param stop The character indicating the end of the word.
\retval 0 The number is extracted.
\retval -1 The stop character was not found after the number.
*/
/*! \fn int getword_ptr(char *orig_line,char **word, struct getwordstruct *gwarea, char stop)
Return a pointer to a null terminated string starting at the current position and ending
and the stop character.
\param orig_line The line that is being parsed.
\param word A pointer to set to the beginning of the string.
\param gwarea The getword buffer initialized by getword_start().
\param stop The character indicating the end of the word.
\retval 0 The word is skipped.
\retval -1 Invalid \a orig_line passed to the function.
*/
/*! \fn long long int my_atoll (const char *nptr)
Convert a string into a long long.
\param nptr The string containing the number to convert.
\return The number found in the string or zero if no number was found.
*/
/*! \fn static int is_absolute(const char *path)
Tell if the path is absolute. On Unix, a path is absolute if it starts with a /.
On Windows, we also check if the path starts with "x:" where x can be any letter.
\param path The path to check.
\retval 1 The path is absolute.
\retval 0 The path is relative.
*/
/*! \fn void my_mkdir(const char *name)
Create the directory and all the non existing parent directories.
\param name The absolute directory to create.
*/
/*! \fn void my_lltoa(unsigned long long int n, char *s, int ssize, int len)
Format a long long into a string.
\param n The number to format.
\param s The buffer to write the number.
\param ssize The size of the output buffer.
\param len The minimum number of digits to format in the output. If the formatted
number is less than this length, it is padded with zeros.
*/
/*! \fn int builddia(int day, int month, int year)
Return a numerical value made of the date.
\param day The day of the date.
\param month The number of the month starting from 1.
\param year The year.
\return The date in an integer format computed as year*10000+month*100+day.
*/
/*! \fn void buildymd(const char *dia, const char *mes, const char *ano, char *wdata)
Convert the date into a machine format YYYYMMDD.
\param dia The day.
\param mes The name of the month as spelled in ::mtab1. If the month is invalid, the output date
is set to month 13.
\param ano The year.
\param wdata The buffer to format the date.
*/
/*! \fn int conv_month(int char *month)
Convert the month's name into its two digits numerical equivalent.
\param month The name of the month as spelled in ::mtab1.
\return The month number on starting from one. If the month name is not in ::mtab1,
13 is returned.
*/
/*! \fn const char *conv_month_name(int month)
Convert a month number into a name.
\param month The number of the month in the range 1 to 12.
\return The name of the month from ::mtab1 unless the month number is not between 1 and 12
in which case, the number is returned encoded on 3 characters. If the number is
invalid, the returned string is static and will be reused by any subsequent call to this
function with an invalid month number.
*/
/*! \fn void name_month(char *month,int month_len)
Get the name of the month according to the language file selected by the user.
\param month The number of the month. It is replaced by the month's name if the number is between
1 and 12 or by the name of December if the number is invalid.
\param month_len The size of the \a month buffer.
*/
/*! \fn char *fixnum(long long int value, int n)
Rewrite a number to make it more readable. The number may be written
with the suffix K, M, G or T depending on its magnitude or the digits
are grouped by three and separated by a dot or a comma.
\param value The number to format.
\param n If the number is abreviated and this parameter is true then append
the suffix K, M, G or T if necessary. If it is zero, the number is shortened
but no suffix is written.
\return A static buffer containing the formatted number. It is overwritten on the next
call of this function.
*/
/*! \def MAXLEN_FIXNUM
The size of the buffer to format a number in fixnum().
*/
/*! \fn char *fixnum2(long long int value, int n)
Format a number by grouping the digits by three and separating the groups by
a dot or a comma.
*/
/*! \def MAXLEN_FIXNUM2
The size of the buffer to format a number in fixnum2().
*/
/*! \fn void buildhref(char * href)
Replace the path given as argument by the first part of a HTML tag to link to the given
directory (the A tag). More precisely, the argument is replaced by <a href=" followed by the given \a href.
\param href The directory to replace by a HTML A tag with the open HREF to it.
*/
/*! \fn char *buildtime(long long int elap)
Write the elapsed time given in milliseconds as a string in the format HH:MM:SS.
\param elap The elapsed time in milliseconds.
\return A static buffer with the formatted time. It is valid until the function is called again.
*/
/*! \fn void formatdate(char *date,int date_size,int year,int month,int day,int hour,int minute,int second,int dst)
Format a date to display it in the report.
\param date The buffer to write the formatted date into.
\param date_size The size of the buffer.
\param year The absolute year to format. It must be greater than 1900.
\param month The month to format. It must be between 1 and 12.
\param day The day to format starting from 1.
\param hour The hour to format.
\param minute The minute to format.
\param second The second to format.
\param dst A positive number if the daylight saving is active, zero if it is not active and a negative number if it is unknown.
*/
/*! \fn void computedate(int year,int month,int day,struct tm *t);
Fill a tm structure with the data of the date.
\param year The full year with century.
\param month The number of the month starting from one.
\param day The day of the date.
\param t The buffer to fill with the date.
*/
/*! \fn int obtuser(const char *dirname, const char *name)
Get the number of entries stored in a report data directory. The number is read from
the <tt>sarg-users</tt> file of the report data's directory.
\param dirname The directory containing the reports.
\param name The name of the report directory whose <tt>sarg-users</tt> file must be read.
\return The number of entries in the report or zero if the file doesn't exists.
*/
/*! \fn void obttotal(const char *dirname, const char *name, int nuser, long long int *tbytes, long long int *media)
Count the total size transfered in a report directory and compute the average number of bytes
per entry.
\param dirname The directory containing the reports.
\param name The name of the report directory whose <tt>sarg-general</tt> file must be read.
\param nuser The number of entries in the report directory.
\param tbytes A variable to store the total number of bytes from this report.
\param media A variable to store the average number of bytes per entry.
*/
/*! \fn int getperiod_fromsarglog(const char *arqtt,struct periodstruct *period)
Initialize the period with the content of the first line of a sarg log.
\param arqtt The text at the first line of a sarg log file.
\param period The period to initialize.
*/
/*! \fn int getperiod_buildtext(struct periodstruct *period)
Build the text to display as the date range of the report.
\param period The object whose text must be contructed.
\retval 0 No error.
\retval -1 Resulting text too long for buffer.
*/
/*! \fn static void copy_images(void)
Copy the images (in fact all the files) from the directory ::IMAGEDIR into the output directory
whose name is in ::outdir.
*/
/*! \fn void strip_latin(char *line)
Remove any HTML entity from the line. A HTML entity starts with an ampersand and end at the next
semicolon.
\param line The text whose html entities are to be removed.
*/
/*! \fn void zdate(char *ftime,int ftimesize, const char *DateFormat)
Format the current date and time according to the date format.
\param ftime The buffer to format the date.
\param ftimesize The size of the buffer to store the date
\param DateFormat The format of the date. It can be:
\arg u to format as mmm/dd/YYYY HH:MM
\arg e to format as dd/mmm/YYYY HH:MM
\arg w to format as WW-HH-MM where WW is the week number in the range 00 to 53.
*/
/*! \fn char *fixtime(long int elap)
Format a "time" into a size or a time formatted as HH:MM:SS.
\param elap The "time" to format in milliseconds if it is a time and into bytes if it is a size.
\return The formatted time.
\bug If the elapsed time is less than 1000ms, the time is formatted with the milliseconds as the seconds.
\todo Review this function and documentation based on the calls made to it and the arguments passed by the callers.
*/
/*! \fn void date_from(char *date, int *dfrom, int *duntil)
Split a date range into a date from and a date until. If the date range
is not a range but just a single date, it is duplicated to make a range out
of it.
\param date The date range to split in the form <tt>from-until</tt>. If it is a single date,
it is transformed into a range like <tt>date-date</tt>. Each date is in the form DD/MM/YYYY.
\param dfrom A variable to write the start date in the form YYYY*10000+MM*100+DD.
\param duntil A variable to write the end date in the form YYYY*10000+MM*100+DD.
*/
/*! \fn char *strlow(char *string)
Convert a string to all lowercases.
\param string The string to convert.
\return A pointer to the string passed as argument.
*/
/*! \fn char *strup(char *string)
Convert a string to all uppercases.
\param string The string to convert.
\return A pointer to the string passed as argument.
*/
/*! \fn void removetmp(const char *outdir)
Purge the file <tt>sarg-general</tt> from all the lines but the total.
\param outdir The output directory to purge.
*/
/*! \fn void load_excludecodes(const char *ExcludeCodes)
Load the list of the HTTP codes to exclude from the report. There must be one code per line.
Any trailing space is removed and there is no provision for comments.
\param ExcludeCodes The name of the file to load.
This function allocate the memory to store the codes and it must be freed by a call to
free_excludecodes().
*/
/*! \fn void free_excludecodes(void)
Free the memory allocated by load_excludecodes().
*/
/*! \fn int vercode(const char *code)
Check if the code is contained in the exclusion list loaded by load_excludecodes().
\param code The HTTP code to test.
\retval 1 The code is excluded.
\retval 0 The code is not excluded.
*/
/*! \fn void fixnone(char *str)
Find if the string is the word none and clear the string if it matches. The function
tolerates the trailing spaces and tabulations.
\param str The text to test for the word "none".
*/
/*! \fn void fixendofline(char *str)
Remove the control codes and spaces at the end of the line. That is, it remove any ASCII
code less than or equal to 0x20.
\param str The string to truncate.
*/
/*! \fn int testvaliduserchar(const char *user)
Tell if the user string contains any invalid character in a user name. The list
of the invalid characters is defined by ::UserInvalidChar.
\param user The user name to test.
\retval 1 The string contains at least one invalid character.
\retval 0 The string is valid.
*/
/*! \fn int compar( const void *a, const void *b )
Compare two integers for bsearch.
\param a A pointer to the first integer.
\param b A pointer to the second integer.
\retval 1 If a > b.
\retval 0 If a == b.
\retval -1 If a < b.
*/
/*! \fn void show_info(FILE *fp_ou)
Write the HTML formatted message to indicate the version of sarg that produced
the report and when the report was generated.
\param fp_ou The HTML file to which the identification block must be appended.
*/
/*! \fn void show_sarg(FILE *fp_ou, int depth)
Write the header of the report to tell that it was generated by sarg.
\param fp_ou The handle of the HTML file.
\param depth How deep is the page in the directory tree. It is used to prepend the images directory name
with as many .. as necessary. If the page is at the same level as the image directory, the depth is zero.
*/
/*! \fn char *get_size(const char *path, const char *file)
Get the size, in human readable form and kibibytes, of the content of a directory.
\param path The path containing the directory to scan.
\param file The last part of the path to the directory to scan.
\return The size of the path.
*/
/*! \fn void write_html_head(FILE *fp_ou,int depth, const char *page_title,int javascript)
Write the header of the HTML document. The DTD corresponds to a
transitional HTML version 4.01. The title of the document is taken from
the global variable ::Title.
\param fp_ou The file to which the HTML header is written.
\param depth How deep is the page in the directory tree. The path of the relative javascripts is adjusted accordingly.
\param title The title of the page.
\param javascript Which javascript to include in the page. Is a combination of the following bits:
\arg HTML_JS_SORTTABLE
*/
/*! \fn void write_html_header(FILE *fp_ou, int depth, const char *title)
Write the HTML header of a HTML report file including the sarg logo and
the beginning of the header of the report.
The header of the report must be closed by a call to close_html_header().
\param fp_ou The file to which the HTML header is written.
\param depth How deep is the page in the directory tree. The depth is passed to show_sarg().
\param title The title of the page.
\param javascript Which javascript to include in the page. Is a combination of bits.
See \see write_html_header() for the possible values.
*/
/*! \fn void close_html_header(FILE *fp_ou)
Close the header opened by write_html_header().
\param fp_ou The file to which the HTML header is written.
*/
/*! \fn void url_module(const char *url, char *w2)
Copy at most 254 bytes from the end of the URL or stops at the first /.
\param url The URL to parse.
\param w2 A buffer to store the copied portion of the URL. The buffer must
be at least 255 characters long.
*/
/*! \fn int write_html_trailer(FILE *fp_ou)
End the HTML file by closing the centered table that was opened by write_html_header(), writting
the informations of show_info() and closing the body and html tag. After this function returns, the
HTML file is complete and nothing should be written to it.
\param fp_ou The HTML file to close. The file handle is not closed but you should not write anything
to the file after this function returns.
\retval 0 No error.
\retval -1 Write error.
*/
/*! \fn void version(void)
Display the current version of sarg and terminate the program.
*/
/*! \fn char *get_param_value(const char *param,char *line)
Get the value of a parameter formatted in the string as "param value"
without the quotes.
If the parameter name matches \a param, then the value following the parameter
is returned. If it doesn't match, the function return NULL.
The function is suitable to parse configuration files because it will ignore
comments (anything but spaces and tabulations put before the parameter will make
it unrecognized by this function)
\param param The parameter name that must be found at the beginning of the line
with possible spaces or tabulations before.
\param line The text line to search for the parameter and it's value.
\return The beginning of the value after the equal sign and with the possible
spaces or tabulations removed. If the line doesn't start with the parameter name,
the function returns NULL.
*/
/*! \fn void write_logo_image(FILE *fp_ou)
Write a link of the logo of the organisation that generate the report in the HTML file. The logo
is written in a centered table.
\param fp_ou The handle of the HTML file being written.
*/
/*! \fn void output_html_string(FILE *fp_ou,const char *str,int maxlen)
Write a string in a file and replace the problematic ASCII characters by their equivalent HTML entities.
\param fp_ou The handle of the output file.
\param str The string to output.
\param maxlen The maximum number of bytes to write from the string. Set to zero to have no limit.
If the string is longer than the requested length, only the requested number of bytes are output and
the string is truncated and ended by ….
*/
/*! \fn void output_html_url(FILE *fp_ou,const char *url)
Write an URL into the file and replace any & by &.
\param fp_ou The handle of the output file.
\param url The URL to output.
*/
/*! \fn void unlinkdir(const char *dir,int contentonly)
Delete a directory and its content.
\param dir The name of the directory to delete.
\param contentonly \c True to delete only the content of the directory and leave the directory
itself in place. If set to \c zero, the directory is removed too.
*/
|