# HG changeset patch # User Emilio Largo # Date 1239878952 -7200 # Node ID 7197576fedcf878b6a6fe38788ccdb9c44ba4fbe # Parent bb6586b1c5472ea5c0b21d819fc98d9495cb9c98 pmc: namespace algebra for vector -> the namespace prevents clashes with std::vector -> std::vector and algebra::Vector with capital V. md5: a md5 replacement ncurses: keypad function added committer: Markus Bröker diff --git a/Makefile b/Makefile --- a/Makefile +++ b/Makefile @@ -29,6 +29,7 @@ sudoku \ cunit \ md5 \ + md5rec \ varargs \ concatenation \ alpha_beta \ @@ -175,6 +176,10 @@ md5: md5.o @echo Linking $< ... + @$(CC) -o $@ $< -lssl + +md5rec: md5rec.o + @echo Linking $< ... @$(CC) -o $@ $< -llsf varargs: varargs.o diff --git a/md5.c b/md5.c new file mode 100644 --- /dev/null +++ b/md5.c @@ -0,0 +1,47 @@ +/** + * test/demos/md5.c + * Copyright (C) 2008 Markus Broeker + */ + +#include +#include +#include +#include + +typedef unsigned char u_char; + +int main (int argc, char **argv) +{ + char buffer[80]; + char text[80]; + + unsigned char *md5_hash; + char result[33]; + char byte[3]; + + int i, j; + + if (argc != 2) { + printf ("Usage: %s \n", argv[0]); + return EXIT_FAILURE; + } + + /* + * MD5 alters the input buffer + */ + strcpy (buffer, argv[1]); + strcpy (text, argv[1]); + + md5_hash = MD5 ((u_char *) buffer, strlen (buffer), NULL); + + for (i = 0, j = 0; i < 16; i++) { + sprintf (byte, "%02x", (md5_hash[i] & 0xFF)); + result[j++] = byte[0]; + result[j++] = byte[1]; + } + + result[j] = 0; + printf ("%s: %s\n", text, result); + + return EXIT_SUCCESS; +} diff --git a/md5rec.c b/md5rec.c --- a/md5rec.c +++ b/md5rec.c @@ -1,5 +1,5 @@ /** - * test/demos/md5.c + * test/demos/md5rec.c * Copyright (C) 2008 Markus Broeker */ diff --git a/ncurses.c b/ncurses.c --- a/ncurses.c +++ b/ncurses.c @@ -34,14 +34,13 @@ return EXIT_SUCCESS; } -wchar_t fucnt (WINDOW * win) +wchar_t func (WINDOW * win) { wchar_t guess; char dest[4]; int maxx; - int maxy; while (guess != ESCAPE) { @@ -82,10 +81,12 @@ check_locale (); win = initscr (); + keypad (win, 1); noecho (); - x = fucnt (win); + + x = func (win); + endwin (); - printf ("\n"); return EXIT_SUCCESS; diff --git a/pmc/cube.cc b/pmc/cube.cc --- a/pmc/cube.cc +++ b/pmc/cube.cc @@ -6,8 +6,14 @@ #include #include +using namespace algebra; + Cube::Cube (Surface * s) { + surface = s; + name = "Cube"; + height = 25; + P[0] = new Vector (0, 0); P[1] = new Vector (100, 0); P[2] = new Vector (100, 100); @@ -17,16 +23,15 @@ P[5] = new Vector (125, 25); P[6] = new Vector (125, 125); P[7] = new Vector (25, 125); - - surface = s; - height = 25; } -Cube::Cube (Surface * s, Vector& p1, Vector& p2, Vector& p3, Vector& p4, int h) +Cube::Cube (Surface * s, Vector & p1, Vector & p2, Vector & p3, Vector & p4, int h) { + surface = s; + name = "Cube"; height = h; - surface = s; - Vector location(h/2, h/2); + + Vector location (h / 4, h / 4); P[0] = new Vector (p1); P[1] = new Vector (p2); @@ -39,6 +44,21 @@ } } +Cube::Cube (Surface * s, Vector p[4], int h) +{ + surface = s; + name = "Cube"; + height = h; + + Vector location (h / 4, h / 4); + + for (int i = 0; i < 4; i++) { + P[i] = new Vector (p[i]); + P[4 + i] = new Vector (*P[i]); + *P[4 + i] = *P[i] + location; + } +} + Cube::~Cube () { for (int i = 0; i < 8; i++) { @@ -58,7 +78,9 @@ void Cube::show () { - /* Implement some Sanity Checks */ + /* + * Implement some Sanity Checks + */ int height = surface->getHeight (); surface->drawLine (P[0]->X (), P[0]->Y (), P[1]->X (), P[1]->Y ()); diff --git a/pmc/include/cube.h b/pmc/include/cube.h --- a/pmc/include/cube.h +++ b/pmc/include/cube.h @@ -12,15 +12,16 @@ class Cube:public Drawable { private: - Vector * P[8]; + algebra::Vector * P[8]; int height; public: - Cube (Surface *, Vector&, Vector&, Vector&, Vector&, int); + Cube (Surface *, algebra::Vector &, algebra::Vector &, algebra::Vector &, algebra::Vector &, int); + Cube (Surface *, algebra::Vector[4], int); Cube (Surface *); virtual ~ Cube (); void show (); - void move (Vector); + void move (algebra::Vector); }; #endif diff --git a/pmc/include/drawable.h b/pmc/include/drawable.h --- a/pmc/include/drawable.h +++ b/pmc/include/drawable.h @@ -11,12 +11,12 @@ class Drawable:public Object { protected: - Vector anker; + algebra::Vector anker; Surface *surface; public: - virtual ~ Drawable () {}; - virtual void move (Vector) = 0; + virtual ~ Drawable () { }; + virtual void move (algebra::Vector) = 0; virtual void show () = 0; }; #endif diff --git a/pmc/include/rectangle.h b/pmc/include/rectangle.h --- a/pmc/include/rectangle.h +++ b/pmc/include/rectangle.h @@ -12,13 +12,14 @@ class Rectangle:public Drawable { private: - Vector * P[4]; + algebra::Vector * P[4]; public: - Rectangle (Surface *, Vector& p1, Vector& p2, Vector& p3, Vector& p4); + Rectangle (Surface *, algebra::Vector & p1, algebra::Vector & p2, algebra::Vector & p3, algebra::Vector & p4); + Rectangle (Surface *, algebra::Vector[4]); Rectangle (Surface *); virtual ~ Rectangle (); - void move (Vector); + void move (algebra::Vector); void show (); }; #endif diff --git a/pmc/include/surface.h b/pmc/include/surface.h --- a/pmc/include/surface.h +++ b/pmc/include/surface.h @@ -20,12 +20,13 @@ public: enum foregroundColor { BLACK, RED }; - Surface (int w, int h, int d); - ~Surface (); + Surface (int w, int h, int d); + ~Surface (); int getWidth () { return width; }; + int getHeight () { return height; }; diff --git a/pmc/include/vector.h b/pmc/include/vector.h --- a/pmc/include/vector.h +++ b/pmc/include/vector.h @@ -8,27 +8,29 @@ #include -class Vector:public Object { - public: - enum Mode { DEG, RAD, GRAD }; +namespace algebra { + class Vector:public Object { + public: + enum Mode { DEG, RAD, GRAD }; - Vector (int xx = 0, int yy = 0); - virtual ~ Vector (); + Vector (int xx = 0, int yy = 0); + virtual ~ Vector (); - int X (); - int Y (); + int X (); + int Y (); - Vector operator+ (Vector&); - Vector operator- (Vector&); - double abs (); + Vector operator+ (Vector &); + Vector operator- (Vector &); + double abs (); + + void vector (); + double angle (Vector &); + void setMode (Mode m = RAD); - void vector (); - double angle (Vector&); - void setMode (Mode m = RAD); - - private: - Mode mode; - int x; - int y; -}; + private: + Mode mode; + int x; + int y; + }; +} #endif diff --git a/pmc/main.cc b/pmc/main.cc --- a/pmc/main.cc +++ b/pmc/main.cc @@ -16,6 +16,8 @@ #define MAX 3 +using namespace algebra; + int main (int argc, char **argv) { Surface *surface; @@ -45,7 +47,7 @@ d[2] = new Cube (surface, p1, p2, p3, p4, height); bool running = true; - int x, y; + int i, x, y; x = y = 0; @@ -60,7 +62,7 @@ if (event.type == SDL_KEYDOWN) { surface->setColor (Surface::BLACK); - for (int i = 0; i < MAX; i++) + for (i = 0; i < MAX; i++) d[i]->show (); surface->setColor (Surface::RED); @@ -70,29 +72,29 @@ break; case SDLK_UP: - for (int i = 0; i < MAX; i++) + for (i = 0; i < MAX; i++) d[i]->move ((Vector (x, y - STEP))); break; case SDLK_DOWN: - for (int i = 0; i < MAX; i++) + for (i = 0; i < MAX; i++) d[i]->move ((Vector (x, y + STEP))); break; case SDLK_LEFT: - for (int i = 0; i < MAX; i++) + for (i = 0; i < MAX; i++) d[i]->move ((Vector (x - STEP, y))); break; case SDLK_RIGHT: - for (int i = 0; i < MAX; i++) + for (i = 0; i < MAX; i++) d[i]->move ((Vector (x + STEP, y))); break; default: break; } - for (int i = 0; i < MAX; i++) { + for (i = 0; i < MAX; i++) { d[i]->show (); } @@ -100,7 +102,7 @@ } } - for (int i = 0; i < MAX; i++) + for (i = 0; i < MAX; i++) delete d[i]; delete surface; diff --git a/pmc/rectangle.cc b/pmc/rectangle.cc --- a/pmc/rectangle.cc +++ b/pmc/rectangle.cc @@ -6,28 +6,44 @@ #include #include +using namespace algebra; + Rectangle::Rectangle (Surface * s) { + surface = s; + name = "Rectangle"; + + anker = Vector (0, 0); + P[0] = new Vector (0, 0); P[1] = new Vector (0, 100); P[2] = new Vector (100, 100); P[3] = new Vector (0, 100); +} + +Rectangle::Rectangle (Surface * s, Vector & p1, Vector & p2, Vector & p3, Vector & p4) +{ + surface = s; + name = "Rectangle"; anker = Vector (0, 0); - surface = s; -} - -Rectangle::Rectangle (Surface * s, Vector& p1, Vector& p2, Vector& p3, Vector& p4) -{ P[0] = new Vector (p1); P[1] = new Vector (p2); P[2] = new Vector (p3); P[3] = new Vector (p4); +} + +Rectangle::Rectangle (Surface * s, Vector p[4]) +{ + surface = s; + name = "Rectangle"; anker = Vector (0, 0); - surface = s; + for (int i = 0; i < 4; i++) { + P[i] = new Vector (p[i]); + } } Rectangle::~Rectangle () diff --git a/pmc/vector.cc b/pmc/vector.cc --- a/pmc/vector.cc +++ b/pmc/vector.cc @@ -8,6 +8,8 @@ #include +using namespace algebra; + Vector::Vector (int xx, int yy) { name = "Vector"; @@ -37,12 +39,12 @@ return y; } -Vector Vector::operator+ (Vector& a) +Vector Vector::operator+ (Vector & a) { return Vector (x + a.X (), y + a.Y ()); } -Vector Vector::operator- (Vector& a) +Vector Vector::operator- (Vector & a) { return Vector (x - a.X (), y - a.Y ()); } @@ -57,7 +59,7 @@ fprintf (stderr, "(%.3d, %.3d)", x, y); } -double Vector::angle (Vector& v) +double Vector::angle (Vector & v) { if (mode == DEG) return ((180.0 / M_PI) * std::acos ((v.X () * X () + v.Y () * Y ()) / (abs () * v.abs ()))); diff --git a/vector.cc b/vector.cc --- a/vector.cc +++ b/vector.cc @@ -20,11 +20,11 @@ Vector (int xx = 0, int yy = 0); ~Vector (); - int X () { return x; } - int Y () { return y; } + int X () { return x; }; + int Y () { return y; }; - Vector operator+ (Vector&); - Vector operator- (Vector&); + Vector operator+ (Vector &); + Vector operator- (Vector &); void vector (); double abs (); @@ -39,11 +39,11 @@ fprintf (stderr, "Removing Vector (%.3d, %.3d)\n", x, y); } - Vector Vector::operator+ (Vector& v) { + Vector Vector::operator+ (Vector & v) { return Vector (x + v.X (), y + v.Y ()); } - Vector Vector::operator- (Vector& v) { + Vector Vector::operator- (Vector & v) { return Vector (x - v.X (), y - v.Y ()); }