diff options
author | Pixel <Pixel> | 2001-12-08 13:57:52 +0000 |
---|---|---|
committer | Pixel <Pixel> | 2001-12-08 13:57:52 +0000 |
commit | a83a43e57be59ed407d98f465d02953af5ae0160 (patch) | |
tree | 91aa1ea4a24e5b85ef26686828054e858cec21aa | |
parent | c54dfe9b844f6c5d8969669f5144e721e2af82b0 (diff) |
*hum* some changes...
-rw-r--r-- | include/Action.h | 2 | ||||
-rw-r--r-- | include/Buffer.h | 5 | ||||
-rw-r--r-- | include/Confirm.h | 2 | ||||
-rw-r--r-- | include/CopyJob.h | 4 | ||||
-rw-r--r-- | include/Form.h | 2 | ||||
-rw-r--r-- | include/Handle.h | 2 | ||||
-rw-r--r-- | include/Image.h | 61 | ||||
-rw-r--r-- | include/Input.h | 2 | ||||
-rw-r--r-- | include/Makefile.am | 2 | ||||
-rw-r--r-- | include/Menu.h | 2 | ||||
-rw-r--r-- | include/Message.h | 2 | ||||
-rw-r--r-- | include/Table.h | 2 | ||||
-rw-r--r-- | lib/Buffer.cc | 22 | ||||
-rw-r--r-- | lib/Confirm.cc | 2 | ||||
-rw-r--r-- | lib/CopyJob.cc | 16 | ||||
-rw-r--r-- | lib/Form.cc | 2 | ||||
-rw-r--r-- | lib/Handle.cc | 2 | ||||
-rw-r--r-- | lib/HttpServ.cc | 15 | ||||
-rw-r--r-- | lib/Image.cc | 78 | ||||
-rw-r--r-- | lib/Input.cc | 6 | ||||
-rw-r--r-- | lib/Makefile.am | 2 | ||||
-rw-r--r-- | lib/Menu.cc | 2 | ||||
-rw-r--r-- | lib/Message.cc | 2 | ||||
-rw-r--r-- | lib/Output.cc | 4 | ||||
-rw-r--r-- | lib/Table.cc | 2 | ||||
-rw-r--r-- | lib/TaskMan.cc | 5 | ||||
-rw-r--r-- | src/Main.cc | 25 |
27 files changed, 231 insertions, 42 deletions
diff --git a/include/Action.h b/include/Action.h index 99ad7ea..d11162f 100644 --- a/include/Action.h +++ b/include/Action.h @@ -12,7 +12,7 @@ class Action : public Base { Action(const String & = ""); virtual ~Action(); Action * Look4URL(const String &); - virtual Task * Do(Variables *, Handle *) = 0; + virtual Task * Do(Variables *, Variables *, Handle *) = 0; virtual void SendHead(Handle *); virtual void SendFoot(Handle *); virtual void ShowButton(Handle *, const String & = " Ok ", const String & = "start"); diff --git a/include/Buffer.h b/include/Buffer.h index 5b35139..cf6481f 100644 --- a/include/Buffer.h +++ b/include/Buffer.h @@ -21,9 +21,12 @@ class Buffer : public Handle { virtual String GetName(); virtual Buffer operator=(const Buffer &); virtual bool CanWatch(); + virtual ssize_t GetSize(); + char operator[](size_t) const; + char & operator[](size_t); private: - char * buffer; + char * buffer, zero; int realsiz, bufsiz, ptr; }; diff --git a/include/Confirm.h b/include/Confirm.h index be0b659..da0365d 100644 --- a/include/Confirm.h +++ b/include/Confirm.h @@ -11,7 +11,7 @@ class Confirm : public Action { Confirm(const String & titre, const String & msg, const String & url, Action * yes, Action * no = 0); virtual ~Confirm() { } virtual String GetTitle(); - virtual Task * Do(Variables *, Handle *); + virtual Task * Do(Variables *, Variables *, Handle *); private: String tit, msg; diff --git a/include/CopyJob.h b/include/CopyJob.h index bd859bb..2a886c5 100644 --- a/include/CopyJob.h +++ b/include/CopyJob.h @@ -9,14 +9,14 @@ class CopyJob : public Task { public: - CopyJob(Handle *, Handle *, ssize_t = -1, bool = false); + CopyJob(Handle *, Handle *, ssize_t = -1, bool = false, bool = false); virtual ~CopyJob(); virtual int Do() throw (GeneralException); virtual String GetName(); private: Handle * s, * d; - bool ds; + bool ds, dd; ssize_t siz, cursiz; char buffer[COPY_BUFSIZ]; int r; diff --git a/include/Form.h b/include/Form.h index 77498a8..9e77967 100644 --- a/include/Form.h +++ b/include/Form.h @@ -13,7 +13,7 @@ class Form : public Action { int nb, Action * ok = 0); virtual ~Form() { } virtual String GetTitle(); - virtual Task * Do(Variables *, Handle *); + virtual Task * Do(Variables *, Variables *, Handle *); private: String tit, iv, * nms, * ivs, * defs, ** lsts, ** dscs; diff --git a/include/Handle.h b/include/Handle.h index 9fef098..3b9c9dc 100644 --- a/include/Handle.h +++ b/include/Handle.h @@ -23,7 +23,7 @@ class Handle : public Base { virtual bool CanRead(); virtual bool CanWrite(); virtual String GetName(); - virtual off_t GetSize(); + virtual ssize_t GetSize(); virtual time_t GetModif(); void close() throw (GeneralException); int GetHandle(); diff --git a/include/Image.h b/include/Image.h new file mode 100644 index 0000000..8fd719c --- /dev/null +++ b/include/Image.h @@ -0,0 +1,61 @@ +#ifndef __IMAGE_H__ +#define __IMAGE_H__ +#ifdef __cplusplus + +#include <Buffer.h> + +enum { + FORMAT_TGA_BASIC, +} format_t; + +struct Color { + Color(unsigned char aR, unsigned char aG, unsigned char aB, unsigned char aA = 255) : + R(aR), G(aG), B(aB), A(aA) { } + unsigned char R, G, B, A; +}; + +class Image : public Buffer { + public: + Image(unsigned int, unsigned int); + virtual ~Image(); + Color GetPixel(unsigned int, unsigned int); + void SetPixel(unsigned int, unsigned int, Color); + bool Prepare(unsigned int = FORMAT_TGA_BASIC); + void Fill(Color = Color(0, 0, 0)); + virtual String GetName(); + virtual bool CanWrite(); + + private: + typedef unsigned char Byte; + typedef unsigned short int Word; + typedef unsigned long int DWord; + struct TGAHeader { + Byte IDLength; + Byte ColorMapType; + Byte ImageType; + Word CM_FirstEntry; + Word CM_Length; + Byte CM_EntrySize; + Word IS_XOrigin; + Word IS_YOrigin; + Word IS_Width; + Word IS_Height; + Byte IS_Depth; + Byte IS_Descriptor; + } __attribute__((packed)); + + struct TGAFooter { + DWord ExtOffset; + DWord DevOffset; + char Sig[18]; + } __attribute__((packed)); + + unsigned int x, y; + bool r; + Color * img; +}; + +#else +#error This only works with a C++ compiler +#endif +#endif diff --git a/include/Input.h b/include/Input.h index 7aa0ff2..87190f1 100644 --- a/include/Input.h +++ b/include/Input.h @@ -14,7 +14,7 @@ class Input : public Handle { virtual bool CanWrite(); virtual bool CanRead(); virtual String GetName(); - virtual off_t GetSize(); + virtual ssize_t GetSize(); virtual time_t GetModif(); protected: diff --git a/include/Makefile.am b/include/Makefile.am index c748342..de443e0 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -2,4 +2,4 @@ pkginclude_HEADERS = \ Exceptions.h Handle.h String.h Output.h Socket.h HttpServ.h Variables.h Menu.h \ Action.h Message.h Form.h Confirm.h Table.h IRC.h Task.h Buffer.h General.h \ -CopyJob.h ReadJob.h Regex.h TaskMan.h InPipe.h OutPipe.h Input.h +CopyJob.h ReadJob.h Regex.h TaskMan.h InPipe.h OutPipe.h Input.h Image.h diff --git a/include/Menu.h b/include/Menu.h index 6e77c70..2558920 100644 --- a/include/Menu.h +++ b/include/Menu.h @@ -11,7 +11,7 @@ class Menu : public Action { Menu(const String & titre, const String & url, String * labels, Action ** listac, int nb); virtual ~Menu() {} virtual String GetTitle(); - virtual Task * Do(Variables *, Handle *); + virtual Task * Do(Variables *, Variables *, Handle *); private: String tit; diff --git a/include/Message.h b/include/Message.h index 043f4d6..35437bd 100644 --- a/include/Message.h +++ b/include/Message.h @@ -11,7 +11,7 @@ class Message : public Action { Message(const String & titre, const String & msg, const String & url, Action * ok = 0); virtual ~Message() { } virtual String GetTitle(); - virtual Task * Do(Variables *, Handle *); + virtual Task * Do(Variables *, Variables *, Handle *); private: String tit, msg; diff --git a/include/Table.h b/include/Table.h index 5b01aab..c616e7e 100644 --- a/include/Table.h +++ b/include/Table.h @@ -11,7 +11,7 @@ class Table : public Action { Table(const String & titre, const String & url, String * heads, String * cells, int nbcol, int nblgn, Action * ok = 0); virtual ~Table() { } virtual String GetTitle(); - virtual Task * Do(Variables *, Handle *); + virtual Task * Do(Variables *, Variables *, Handle *); private: String tit, * hds, * cls; diff --git a/lib/Buffer.cc b/lib/Buffer.cc index 2c2c955..e8335d9 100644 --- a/lib/Buffer.cc +++ b/lib/Buffer.cc @@ -3,7 +3,7 @@ #include "General.h" #include "config.h" -Buffer::Buffer() : Handle(-1), buffer(0), realsiz(0), bufsiz(0), ptr(0) { } +Buffer::Buffer() : Handle(-1), buffer(0), zero(0), realsiz(0), bufsiz(0), ptr(0) { } Buffer::~Buffer() { free(buffer); @@ -75,3 +75,23 @@ Buffer Buffer::operator=(const Buffer & b) { bool Buffer::CanWatch() { return false; } + +ssize_t Buffer::GetSize() { + return realsiz; +} + +char Buffer::operator[](size_t p) const { + if (p >= realsiz) { + return 0; + } else { + return buffer[ptr + p]; + } +} + +char & Buffer::operator[](size_t p) { + if (p >= realsiz) { + return zero; + } else { + return buffer[ptr + p]; + } +} diff --git a/lib/Confirm.cc b/lib/Confirm.cc index 7d72ee0..1176db1 100644 --- a/lib/Confirm.cc +++ b/lib/Confirm.cc @@ -7,7 +7,7 @@ Confirm::Confirm(const String & t, const String & m, const String & U, Action * y, Action * n) : Action(U), tit(t), msg(m), NYes(y), NNo(n) { } -Task * Confirm::Do(Variables * v, Handle * h) { +Task * Confirm::Do(Variables * v, Variables *, Handle * h) { Handle * b = new Buffer; Task * t = new CopyJob(b, h, -1, true); SendHead(b); diff --git a/lib/CopyJob.cc b/lib/CopyJob.cc index 864706b..1e151c3 100644 --- a/lib/CopyJob.cc +++ b/lib/CopyJob.cc @@ -2,13 +2,21 @@ #include "General.h" #include "config.h" -CopyJob::CopyJob(Handle * as, Handle * ad, ssize_t asiz, bool ads) : s(as), d(ad), ds(ads), siz(asiz), cursiz(0), r(0) { +CopyJob::CopyJob(Handle * as, Handle * ad, ssize_t asiz, bool ads, bool add) : s(as), d(ad), ds(ads), dd(add), siz(asiz), cursiz(0), r(0) { s->SetNonBlock(); d->SetNonBlock(); WaitFor(s, W4_READING); } -CopyJob::~CopyJob() { } +CopyJob::~CopyJob() { + if (ds) { + delete s; + } + + if (dd) { + delete d; + } +} int CopyJob::Do() throw (GeneralException) { int tr; @@ -44,10 +52,6 @@ int CopyJob::Do() throw (GeneralException) { Suspend(TASK_ON_HOLD); } - if (ds) { - delete s; - } - return TASK_DONE; } diff --git a/lib/Form.cc b/lib/Form.cc index e26cf72..464dec3 100644 --- a/lib/Form.cc +++ b/lib/Form.cc @@ -12,7 +12,7 @@ String Form::GetTitle(void) { return tit; } -Task * Form::Do(Variables * v, Handle * h) { +Task * Form::Do(Variables * v, Variables *, Handle * h) { Handle * b = new Buffer(); Task * t = new CopyJob(b, h, -1, true); SendHead(b); diff --git a/lib/Handle.cc b/lib/Handle.cc index 0221e71..4585b61 100644 --- a/lib/Handle.cc +++ b/lib/Handle.cc @@ -174,7 +174,7 @@ String Handle::GetName(void) { return _("Bare Handle - should not happend"); } -off_t Handle::GetSize(void) { +ssize_t Handle::GetSize(void) { return -1; } diff --git a/lib/HttpServ.cc b/lib/HttpServ.cc index c20862a..d2c3c02 100644 --- a/lib/HttpServ.cc +++ b/lib/HttpServ.cc @@ -33,7 +33,7 @@ class ProcessRequest : public Task { Socket s; String name, host; - Variables * Vars; + Variables * Vars, * Heads; bool bad, hasvars, post; }; @@ -63,9 +63,12 @@ int ProcessRequest::Do() { // cerr << "---- Got a request from handle " << s.GetHandle() << " \n"; post = ParseUri(file, domain, &b); + + Heads = new Variables(); len = -1; do { + int p; b >> t; // cerr << "Read Request (n): " << t << endl; if ((t.strstr("Content-Length: ") == 0) || (t.strstr("Content-length: ") == 0)) { @@ -75,6 +78,13 @@ int ProcessRequest::Do() { if (t.strstr("Host: ") == 0) { host = t.extract(6); } + + if ((p = t.strchr(':')) >= 0) { + String s = t.extract(0, p - 1); + s += '='; + s += t.extract(p + 2); + Heads->Add(s); + } } while (t.strlen()); // cerr << "---- Processing it.\n"; @@ -144,7 +154,7 @@ int ProcessRequest::Do() { // Le domaine 'bin' est réservé aux actions. On cherche donc l'action à effectuer. if ((f = p->Look4URL(file))) { SendHeads(&b, "text/html"); - a = f->Do(Vars, &s); + a = f->Do(Vars, Heads, &s); } else { ShowError(&b); } @@ -169,6 +179,7 @@ int ProcessRequest::Do() { if (a) a->Stop(); delete Vars; + delete Heads; // cerr << "---- Sending header buffer.\n"; c = new CopyJob(&b, &s, -1, false); WaitFor(c); diff --git a/lib/Image.cc b/lib/Image.cc new file mode 100644 index 0000000..9b41b4e --- /dev/null +++ b/lib/Image.cc @@ -0,0 +1,78 @@ +#include "Image.h" +#include "config.h" + +Image::Image(unsigned int ax, unsigned int ay) : x(ax), y(ay), img((Color *) malloc(x * y * sizeof(Color))) { + Fill(); +} + +Image::~Image() { + free((void *)img); +} + +bool Image::CanWrite() { + return false; +} + +String Image::GetName() { + return String(_("Image ")) + x + "x" + y; +} + +void Image::Fill(Color c) { + for (unsigned int i = 0; i < x * y; i++) { + img[i] = c; + } +} + +Color Image::GetPixel(unsigned int px, unsigned int py) { + if ((px >= x) || (py >= y)) { + return Color(0, 0, 0, 0); + } + + return img[x * py + px]; +} + +void Image::SetPixel(unsigned int px, unsigned int py, Color c) { + if ((px >= x) || (py >= y)) { + return; + } + + img[x * py + px] = c; +} + +bool Image::Prepare(unsigned int f) { + if (GetSize()) return false; + + switch (f) { + case FORMAT_TGA_BASIC: + TGAHeader Header; + TGAFooter Footer; + + Header.IDLength = 0; + Header.ColorMapType = 0; + Header.ImageType = 2; + Header.CM_FirstEntry = 0; + Header.CM_Length = 0; + Header.CM_EntrySize = 0; + Header.IS_XOrigin = 0; + Header.IS_YOrigin = 0; + Header.IS_Width = x; + Header.IS_Height = y; + Header.IS_Depth = 32; + Header.IS_Descriptor = 0x20; + + Footer.ExtOffset = 0; + Footer.DevOffset = 0; + strcpy(Footer.Sig, "TRUEVISION-XFILE."); + + write(&Header, sizeof(Header)); + write(img, x * y * sizeof(Color)); + write(&Footer, sizeof(Footer)); + + return true; + + break; + default: + return false; + } +} + diff --git a/lib/Input.cc b/lib/Input.cc index be6981f..318821d 100644 --- a/lib/Input.cc +++ b/lib/Input.cc @@ -10,10 +10,10 @@ #include "config.h" Input::Input(String no) throw (GeneralException) : - Handle(no.strlen() ? open(no.to_charp(), O_RDONLY) : 0), + Handle(no.strlen() ? open(no.to_charp(), O_RDONLY) : dup(0)), n(no) { if (GetHandle() < 0) { - throw IOGeneral(String(_("Error opening file")) + no + _(" for reading: ") + strerror(errno)); + throw IOGeneral(String(_("Error opening file ")) + no + _(" for reading: ") + strerror(errno)); } size = lseek(GetHandle(), 0, SEEK_END); @@ -38,7 +38,7 @@ String Input::GetName() { return n; } -off_t Input::GetSize() { +ssize_t Input::GetSize() { return size; } diff --git a/lib/Makefile.am b/lib/Makefile.am index a88f7be..a2f0930 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -8,6 +8,6 @@ lib_LTLIBRARIES = libBaltisot.la libBaltisot_la_SOURCES = Exceptions.cc Handle.cc Output.cc String.cc\ Socket.cc Input.cc HttpServ.cc Variables.cc Action.cc Menu.cc Message.cc\ Form.cc Confirm.cc Table.cc checkargs.c datecalc.c IRC.cc Task.cc Buffer.cc\ - CopyJob.cc ReadJob.cc Regex.cc TaskMan.cc InPipe.cc OutPipe.cc + CopyJob.cc ReadJob.cc Regex.cc TaskMan.cc InPipe.cc OutPipe.cc Image.cc libBaltisot_la_LDFLAGS = -release $(Baltisot_VERSION_INFO) diff --git a/lib/Menu.cc b/lib/Menu.cc index f804cef..f730e5b 100644 --- a/lib/Menu.cc +++ b/lib/Menu.cc @@ -7,7 +7,7 @@ Menu::Menu(const String & t, const String & U, String * ts, Action ** as, int nb) : Action(U), tit(t), lt(ts), la(as), nba(nb) { } -Task * Menu::Do(Variables * v, Handle * h) { +Task * Menu::Do(Variables * v, Variables *, Handle * h) { int i, f = 0; Handle * b = new Buffer(); Task * t = new CopyJob(b, h, -1, true); diff --git a/lib/Message.cc b/lib/Message.cc index acd7933..572b0a0 100644 --- a/lib/Message.cc +++ b/lib/Message.cc @@ -7,7 +7,7 @@ Message::Message(const String & t, const String & m, const String & U, Action * n) : Action(U), tit(t), msg(m), Next(n) { } -Task * Message::Do(Variables * v, Handle * h) { +Task * Message::Do(Variables * v, Variables *, Handle * h) { Handle * b = new Buffer(); Task * t = new CopyJob(b, h, -1, true); diff --git a/lib/Output.cc b/lib/Output.cc index c85d372..27f28d2 100644 --- a/lib/Output.cc +++ b/lib/Output.cc @@ -10,10 +10,10 @@ #include "config.h" Output::Output(String no, int trunc = 1) throw (GeneralException) : - Handle(no.strlen() ? open(no.to_charp(), O_WRONLY | O_CREAT | (trunc ? O_TRUNC : O_APPEND)) : 1), + Handle(no.strlen() ? open(no.to_charp(), O_WRONLY | O_CREAT | (trunc ? O_TRUNC : O_APPEND), 00666) : dup(1)), n(no) { if (GetHandle() < 0) { - throw IOGeneral(String(_("Error opening file")) + no + _(" for writing: ") + strerror(errno)); + throw IOGeneral(String(_("Error opening file ")) + no + _(" for writing: ") + strerror(errno)); } } diff --git a/lib/Table.cc b/lib/Table.cc index 30f3127..f2b7bad 100644 --- a/lib/Table.cc +++ b/lib/Table.cc @@ -11,7 +11,7 @@ String Table::GetTitle(void) { return tit; } -Task * Table::Do(Variables * v, Handle * h) { +Task * Table::Do(Variables * v, Variables *, Handle * h) { Handle * b = new Buffer(); Task * t = new CopyJob(b, h, -1, true); diff --git a/lib/TaskMan.cc b/lib/TaskMan.cc index 9053d0b..94e89b1 100644 --- a/lib/TaskMan.cc +++ b/lib/TaskMan.cc @@ -212,7 +212,7 @@ void TaskMan::MainLoop() throw (GeneralException) { } } - r = poll(ufsd, nfds, (no_burst) && !(Zombies.size()) ? -1: 1); + r = poll(ufsd, nfds, (no_burst) && !(Zombies.size()) ? -1: 0); #else FD_ZERO(readfds); FD_ZERO(writefds); @@ -232,7 +232,7 @@ void TaskMan::MainLoop() throw (GeneralException) { r = select(highest + 1, &readfds, &writefds, &exceptfds, NULL); #endif - if (r == -1) { + if (r < 0) { if (errno == EINTR) { // child } else { @@ -256,7 +256,6 @@ void TaskMan::MainLoop() throw (GeneralException) { if (q->revents & POLLHUP) { cerr << _("Handle ") << q->fd << _(" hung up.\n"); - // What should I do now? } fd = q->fd; diff --git a/src/Main.cc b/src/Main.cc index e624478..2bd0b0a 100644 --- a/src/Main.cc +++ b/src/Main.cc @@ -15,6 +15,8 @@ #include "Confirm.h" #include "Table.h" #include "InPipe.h" +#include "Image.h" +#include "CopyJob.h" InPipe * in; @@ -23,8 +25,8 @@ class ad_t : public Action { ad_t() : Action("menu6") { } virtual ~ad_t() { } virtual String GetTitle() { return "Action dynamique"; } - virtual Task * Do(Variables * v, Handle * h) { - String ut, un; + virtual Task * Do(Variables * v, Variables * hds, Handle * h) { + String ut, un, shds; if (!fork()) { execlp("uptime", "uptime", NULL); @@ -38,11 +40,22 @@ class ad_t : public Action { (*in) >> un; wait(NULL); + for (int i = 0; i < hds->GetNb(); i++) { + shds += (*hds)[i] + "<BR>\n"; + } + Action * m = new Message("Action dynamique", String("Vous avez choisi l'action dynamique. L'uptime de la machine est '") + - ut + "' et sa définition complète est '" + un + "'", ""); + ut + "' et sa définition complète est '" + un + "'<BR><BR><BR>Voici la liste des entêtes:<BR><BR>" + shds, ""); + + Task * t = m->Do(v, hds, h); + + Image * testimg = new Image(100, 100); + testimg->Prepare(); + + Output * testoutput = new Output("TestImg.tga"); - Task * t = m->Do(v, h); + new CopyJob(testimg, testoutput, -1, true, true); delete m; return t; @@ -61,10 +74,10 @@ String Titres[] = {"Titre 1", "Titre 2", "Titre 3"}; String Cells[] = {"L1C1", "L1C2", "L1C3", "L2C1", "L2C2", "L2C3", "L3C1", "L3C2", "L3C3", "L4C1", "L4C2", "L4C3"}; Action * a1 = new Message("Action 1", "Vous avez cliqué sur l'option 1 du menu", "menu1"); Action * a2 = new Table("Petite table", "menu2", Titres, Cells, 3, 4); -Action * a3 = new Message("Schéma EA", +Action * a3 = new Message("Test d'image", "Voici un test d'affichage d'image:" "<center><table border=0><td><tr><img src=\"/image/nobis-logo-small.jpg\"></tr></td></table></center>", -"schemaea"); +"menu3"); Action * a4 = new Form("Test de formulaire...", "menu4", "Rentrez des trucs...", Noms, Invites, Defaults, Lists, Descs, 3); Action * a5 = new Confirm("Confirmation", "Oui ou non?", "menu5", 0, 0); Action * java = new Message("Applet JAVA", |