Archive

Posts Tagged ‘size’

stermp.h, trying to port conio.h to Linux

October 22, 2013 No comments

This time I want to rescue an old project. I started it long ago. These days I’ve been reading some source codes in facebook using conio.h so I hope this could be interesting for anyone.

Of course there are some libraries that allow us to to write strings in colors and get/set position on screen and keys without echoing and pressing Enter, or we can do it without them, using ANSI codes directly but we would have to do a lot of changes in the source code.

I tried to keep the name of the functions the same, we use:

  • clrscr() : To clean screen
  • textbackground(color) : To change background color
  • textcolor(color) : To change text color
  • gotoxy(x,y) : Go to specific position
  • wherex() : To get X position
  • wherey() : To get Y position
  • getch() : To get a key press without ENTER
  • getche() : Like getch but echoing character on screen
  • kbhit() : To know if a key has been pressed without stopping execution. Returns true or false

We also have some additional stuff like:

  • wherexy() : Returns X,Y position in a struct
  • kbhit2() : Gets a key code if pressed without stopping execution
  • kbhit_pre() : Prepares to do lots of kbhits() to increase performance
  • restore_terminal_color() : Restores terminal color
  • screenheight() : Gets screen height.
  • screenwidth() : Gets screen width.

I tried also to keep color names the same. Let’s see an example:

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
#include <stdio.h>
#include <time.h>
#include "stermp.h"

void update_time()
{
  struct tm *tm;
  time_t _time;
  char text[50];
  textcolor(YELLOW);
  _time=time(NULL);
  tm = localtime(&_time);
  strftime(text,50,"%d/%m/%Y %H:%M:%S", tm);
  gotoxy(1,1);
  printf("%s    ", text);
}

int main()
{
  int x,y;
  int width, height;
  int key;
  term_init();

  width = screenwidth();
  height = screenheight();
  /* Rellenamos de verde la pantalla */
  textbackground(GREEN);
  clrscr();

  textbackground(BLUE);
  /* Rellenamos de azul la primera fila */
  for (x=0; x<width; x++)
    printf(" ");

  gotoxy(1,height);
  /* Rellenamos de azul la última fila */
  for (x=0; x<width; x++)
    printf(" ");

  gotoxy(2,2);
  while ((key=kbhit2())==0)
      update_time();

  printf("You have pressed: %d\n", key);

  term_defaults();
 
}

We can see I’m calling term_init() and term_defaults() but they are just to restore terminal after the execution ends.
You can download the source code on github. Just include stermp.h in your code and include stermp.h and stermp.c in your project.

File sizes for humans (PHP, C, C++)

October 11, 2013 No comments

When we get a file size, we usually get it in bytes. It’s ok to have this value, but, when giving the user this information, we must convert it to another unit to make it a little more accessible. When talking about megabytes or gigabytes, everyone will make a mess. So here are some snippets to help us do that:

First in PHP:

1
2
3
4
5
6
7
8
9
function human_size ($tam) {
        $i = 0;
        $unids=array("bytes","Kb","Mb","Gb","Tb","Pb","Eb","Zb","Yb","Bb");
        while ($tam>1024) {
                $tam = $tam /1024;
                $i++;
        }
        return number_format($tam,2,",",".").$unids[$i];
}

So we can call:

1
echo human_size(12939128332323);

It will return

11.77Tb

So we can have (as users) a better value.

In C++ we can do it that way:

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
#include <iostream>
#include <sstream>

using namespace std;

string human_size(long double size)
{
  static string units[10]={"bytes","Kb","Mb","Gb","Tb","Pb","Eb","Zb","Yb","Bb"};
  ostringstream out;
  int i= 0;

  while (size>1024) {
    size = size /1024;
    i++;
  }

  out.precision(3);
  out<<size<<units[i];
  return out.str();
}

int main()
{
  cout <<human_size(1232312333)<<endl;
}

This time we use ostringstream to build the output string, but the basis is the same as php, it’s only to have a copy-paste snippet :)

And finally, C language, this time, we don’t have a string type, so we have to use char arrays to make it possible, this way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>

char *human_size(char *store, long double size)
{
  static char units[10][6]={"bytes","Kb","Mb","Gb","Tb","Pb","Eb","Zb","Yb","Bb"};  
  int i= 0;

  while (size>1024) {
    size = size /1024;
    i++;
  }

  sprintf(store,"%.2Lf%s",size, units[i]);

  return store;
}

int main()
{
  char cadena[30];
  printf("Size: %s\n", human_size(cadena, 1293323872));
}

I know I’m using a lot of units: “bytes”, “Kilobytes”, “Megabytes”, “GigaBytes”, “Terabytes”, “Petabytes”, “Exabytes”, “Zetabytes”, “Yottabytes” and “Brontobytes”, and numeric variables won’t store such huge numbers. :)

This post is available in Spanish here: http://totaki.com/poesiabinaria/2010/03/tamano-de-archivo-para-seres-humanos-phpc-y-c/
Photo: Kate Haskell (Flickr) CC-by

Top