00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
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