Main Page Data Structures File List Data Fields Globals Related Pages

# misc.cpp

Go to the documentation of this file.

    00001 /*  misc.cpp
    00002     small helper functions that don't fit anywhere else
    00003
    00004     24.04.2000: tk, initial implementation.
    00005
    00006     Copyright (c)2000 by Thomas Kindler, thomas.kindler@gmx.de
    00007
    00008     This program is free software; you can redistribute it and/or
    00009     modify it under the terms of the GNU General Public License as
    00010     published by the Free Software Foundation; either version 2 of
    00011     the License, or (at your option) any later version. Read the
    00012     full License at http://www.gnu.org/copyleft for more details.
    00013 */
    00014
    00015 // include files -----
    00016 //
    00017 #include <stdlib.h>
    00018 #include <io.h>
    00019 #include <dos.h>
    00020 #include <math.h>
    00021 #include <stdio.h>
    00022 #include <assert.h>
    00023 #include <ctype.h>
    00024 #include "misc.h"
    00025
    00033 char *ProgressBar(float v, float min, float max)
    00034 {
    00035   static char *wheel = "|/-\\";
    00036   static int  spin;
    00037   static char buf[32];
    00038
    00039   float  progress = ((v-min)/(max-min));
    00040   if (progress<0) progress = 0;
    00041   if (progress>1) progress = 1;
    00042
    00043   assert(
    00044     sprintf( buf, "---------- %2.0f%% %c",
    00045              progress*100, wheel[spin++ % sizeof(wheel)] )
    00046     < sizeof(buf)
    00047   );
    00048
    00049   for (int i=0; i<floor(progress*10); i++)
    00050     buf[i] = '=';
    00051
    00052   return buf;
    00053 }
    00054
    00055
    00063 void HexDump(void *mem, unsigned length)
    00064 {
    00065   char  line[80];
    00066   char  huge *src = (char*)mem;
    00067
    00068   printf("\ndumping %u bytes from %p\n", length, src);
    00069   printf("       0  1  2  3  4  5  6  7  8  9  A  B  C  D  E  F"
    00070          "    0123456789ABCDEF\n");
    00071   for (unsigned i=0; i<length; i+=16, src+=16) {
    00072     char *t = line;
    00073
    00074     t += sprintf(t, "%04x:  ", i);
    00075     for (int j=0; j<16; j++) {
    00076       if (i+j < length)
    00077         t += sprintf(t, "%02X", src[j] & 0xff);
    00078       else
    00079         t += sprintf(t, "  ");
    00080       t += sprintf(t, j%2 ? " " : "-");
    00081     }
    00082
    00083     t += sprintf(t, "  ");
    00084     for (int j=0; j<16; j++) {
    00085       if (i+j < length) {
    00086         if (isprint(src[j]))
    00087           t += sprintf(t, "%c", src[j]);
    00088         else
    00089           t += sprintf(t, ".");
    00090       } else {
    00091           t += sprintf(t, " ");
    00092       }
    00093     }
    00094
    00095     t += sprintf(t, "\n");
    00096     printf("%s", line);
    00097   }
    00098 }
    00099
    00100
    00111 void *loadfile(char *fn, unsigned *len)
    00112 {
    00113   void *mem = NULL;
    00114
    00115   FILE *f = fopen(fn, "rb");
    00116   unsigned flen = 0;
    00117
    00118   if (f) {
    00119     flen = filelength(fileno(f));
    00120     mem  = malloc(flen);
    00121     if (mem)
    00122       fread(mem, flen, 1, f);
    00123     fclose(f);
    00124   }
    00125
    00126   if (len)
    00127     *len = mem ? flen : 0;
    00128
    00129   return mem;
    00130 }
    00131