home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Magazyn Amiga Shareware Floppies
/
ma91.dms
/
ma91.adf
/
KonwersjaLib.lha
/
Przykîad
/
Konwerter.c
< prev
next >
Wrap
C/C++ Source or Header
|
1997-07-08
|
16KB
|
614 lines
#include <proto/exec.h>
#include <exec/memory.h>
#include <proto/dos.h>
#include <libraries/dos.h>
#include <proto/asl.h>
#include <libraries/asl.h>
#include <proto/intuition.h>
#include <intuition/intuition.h>
#include <intuition/gadgetclass.h>
#include <proto/gadtools.h>
#include <libraries/gadtools.h>
#include <proto/graphics.h>
#include <proto/konwersja.h>
#include <libraries/konwersja.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define GD_Standardy 0
#define GD_Wczytaj 1
#define GD_Zapisz 2
#define GD_CRLF 3
#define GD_DodajEOF 4
void StworzSrodowisko (void);
void ZamknijSrodowisko (void);
void OtworzBiblioteki (void);
int PrzygotujEkran (void);
int DodajStandardy (void);
int OtworzOkno (void);
void OdswierzOkno (void);
void Renderuj (void);
void ZwolnijEkran (void);
void ZamknijOkno (void);
void UsunStandardy (void);
void ZamknijBiblioteki (void);
void WczytajPlik (void);
void ZapiszPlik (void);
void Informacja (UBYTE *);
void PiszStandard (UBYTE *, UWORD);
void PiszPlik (UBYTE *);
BOOL __stdargs __saveds funkcja (ULONG calosc, ULONG gotowe, APTR);
struct Library *AslBase = NULL;
struct Library *GadToolsBase = NULL;
struct GfxBase *GfxBase = NULL;
struct IntuitionBase *IntuitionBase = NULL;
struct KonBase *KonBase = NULL;
struct FileRequester *requester = NULL;
struct TextFont *font = NULL;
APTR visualinfo = NULL;
struct Screen *ekran = NULL;
struct Window *okno = NULL;
struct Gadget *gadzety = NULL;
struct Gadget *gad_zapisz, *gad_eof;
struct MinList Standardy;
UWORD zoomdata[] =
{
0, 0, 200, 10
};
struct TextAttr topaz8 =
{
(STRPTR) "topaz.font", 8, 0, FPF_ROMFONT
};
struct IntuiText napisy[] =
{
1, 0, JAM1, 242, 14, &topaz8, (UBYTE *) "Plik:", &napisy[1],
1, 0, JAM1, 277, 30, &topaz8, (UBYTE *) "Standard úródîowy:", &napisy[2],
1, 0, JAM1, 277, 61, &topaz8, (UBYTE *) "Standard docelowy:", NULL
};
UWORD kod;
WORD zrodlowy, docelowy = -1;
UBYTE flagi = NULL;
ULONG rozmiar = (ULONG) NULL;
UBYTE *bufor = (UBYTE *) NULL;
void StworzSrodowisko (void)
{
OtworzBiblioteki ();
if (!(PrzygotujEkran ()))
ZamknijSrodowisko ();
if (!(DodajStandardy ()))
ZamknijSrodowisko ();
if (!(OtworzOkno ()))
ZamknijSrodowisko ();
}
void ZamknijSrodowisko (void)
{
ZamknijOkno ();
ZwolnijEkran ();
UsunStandardy ();
ZamknijBiblioteki ();
if (rozmiar && bufor)
FreeMem (bufor, rozmiar);
exit (0);
}
void OtworzBiblioteki (void)
{
if (!(AslBase = (struct Library *) OpenLibrary ("asl.library", 37)))
ZamknijSrodowisko ();
if (!(GadToolsBase = (struct Library *) OpenLibrary ("gadtools.library", 37)))
ZamknijSrodowisko ();
if (!(GfxBase = (struct GfxBase *) OpenLibrary ("graphics.library", 37)))
ZamknijSrodowisko ();
if (!(IntuitionBase = (struct IntuitionBase *) OpenLibrary ("intuition.library", 37)))
ZamknijSrodowisko ();
if (!(KonBase = (struct KonBase *) OpenLibrary (KONNAME, KONVERSION)))
ZamknijSrodowisko ();
if (!(requester = (struct FileRequester *) AllocAslRequestTags (ASL_FileRequest, TAG_DONE)))
ZamknijSrodowisko ();
}
int PrzygotujEkran (void)
{
if (!(font = OpenFont (&topaz8)))
return (0);
if (!(ekran = LockPubScreen ((UBYTE *) NULL)))
return (0);
if (!(visualinfo = GetVisualInfo (ekran, TAG_DONE)))
return (0);
return (1);
}
int DodajStandardy (void)
{
struct Standard *s;
struct Node *n;
NewList ((struct List *) &Standardy);
for (s = (struct Standard *) (KonBase->kon_Standardy.mlh_Head);
s->stan_Node.mln_Succ;
s = (struct Standard *) (s->stan_Node.mln_Succ)
)
{
if (!(n = AllocMem (sizeof (*n), MEMF_PUBLIC | MEMF_CLEAR)))
return (0);
n->ln_Name = (UBYTE *) s->stan_Nazwa;
AddTail ((struct List *) &Standardy, (struct Node *) n);
}
}
int OtworzOkno (void)
{
struct NewGadget ng;
struct Gadget *g;
UWORD offx, offy;
offx = ekran->WBorLeft;
offy = ekran->WBorTop + ekran->RastPort.TxHeight + 1;
if (!(g = CreateContext (&gadzety)))
return (0);
ng.ng_LeftEdge = offx + 71;
ng.ng_TopEdge = offy + 22;
ng.ng_Width = 92;
ng.ng_Height = 68;
ng.ng_GadgetText = (UBYTE *) "Konwertuj na standard:";
ng.ng_TextAttr = &topaz8;
ng.ng_GadgetID = GD_Standardy;
ng.ng_Flags = PLACETEXT_ABOVE;
ng.ng_VisualInfo = visualinfo;
g = CreateGadget (LISTVIEW_KIND, g, &ng, GTLV_Labels, &Standardy, TAG_DONE);
ng.ng_LeftEdge = offx + 35;
ng.ng_TopEdge = offy + 95;
ng.ng_Width = 70;
ng.ng_Height = 14;
ng.ng_GadgetText = (UBYTE *) "Wczytaj";
ng.ng_GadgetID = GD_Wczytaj;
ng.ng_Flags = PLACETEXT_IN;
g = CreateGadget (BUTTON_KIND, g, &ng, TAG_DONE);
ng.ng_LeftEdge = offx + 130;
ng.ng_GadgetText = (UBYTE *) "Zapisz";
ng.ng_GadgetID = GD_Zapisz;
gad_zapisz = g = CreateGadget (BUTTON_KIND, g, &ng, GA_Disabled, 1, TAG_DONE);
ng.ng_LeftEdge = offx + 248;
ng.ng_TopEdge = offy + 100;
ng.ng_GadgetText = (UBYTE *) "LF <-> CR+LF";
ng.ng_GadgetID = GD_CRLF;
ng.ng_Flags = PLACETEXT_RIGHT;
g = CreateGadget (CHECKBOX_KIND, g, &ng, TAG_DONE);
ng.ng_TopEdge = offy + 115;
ng.ng_GadgetText = (UBYTE *) "Dodaj EOF";
ng.ng_GadgetID = GD_DodajEOF;
gad_eof = g = CreateGadget (CHECKBOX_KIND, g, &ng, GA_Disabled, 1, TAG_DONE);
if (!g)
return (0);
if (!(okno = OpenWindowTags (NULL,
WA_Left, 10,
WA_Top, 15,
WA_Width, 484,
WA_Height, 168 + offy,
WA_IDCMP, LISTVIEWIDCMP | BUTTONIDCMP | CHECKBOXIDCMP | IDCMP_CLOSEWINDOW | IDCMP_VANILLAKEY | IDCMP_NEWSIZE,
WA_Flags, WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_CLOSEGADGET | WFLG_SMART_REFRESH | WFLG_SMART_REFRESH | WFLG_ACTIVATE | WFLG_RMBTRAP,
WA_Gadgets, NULL,
WA_Title, (UBYTE *) "Konwerter 1.0",
WA_ScreenTitle, NULL,
WA_PubScreen, ekran,
WA_Zoom, zoomdata,
TAG_DONE)))
return (0);
SetFont (okno->RPort, font);
OdswierzOkno ();
return (1);
}
void OdswierzOkno (void)
{
Renderuj ();
AddGList (okno, gadzety, -1, -1, NULL);
RefreshGList (gadzety, okno, NULL, -1);
GT_RefreshWindow (okno, NULL);
}
void Renderuj (void)
{
UWORD offx, offy;
UWORD pattern[] =
{
0xaaaa, 0x5555
};
offx = okno->BorderLeft;
offy = okno->BorderTop;
SetAPen (okno->RPort, 2);
okno->RPort->AreaPtrn = (UWORD *) & pattern;
okno->RPort->AreaPtSz = 1;
RectFill (okno->RPort, offx, offy, 484 - offx, 168 + offy - 3);
okno->RPort->AreaPtrn = (UWORD *) NULL;
okno->RPort->AreaPtSz = NULL;
SetAPen (okno->RPort, 0);
RectFill (okno->RPort, offx + 14, offy + 6, offx + (14 + 208) - 1, offy + (6 + 109) - 1);
RectFill (okno->RPort, offx + 235, offy + 6, offx + (235 + 227) - 1, offy + (6 + 85) - 1);
RectFill (okno->RPort, offx + 14, offy + 120, offx + (14 + 208) - 1, offy + (120 + 11) - 1);
RectFill (okno->RPort, offx + 235, offy + 96, offx + (235 + 227) - 1, offy + (96 + 35) - 1);
RectFill (okno->RPort, offx + 14, offy + 136, offx + (14 + 448) - 1, offy + (136 + 24) - 1);
DrawBevelBox (okno->RPort, offx + 14, offy + 6, 208, 109, GT_VisualInfo, visualinfo, GTBB_Recessed, TRUE, TAG_DONE);
DrawBevelBox (okno->RPort, offx + 235, offy + 6, 227, 85, GT_VisualInfo, visualinfo, GTBB_Recessed, TRUE, TAG_DONE);
DrawBevelBox (okno->RPort, offx + 289, offy + 11, 164, 14, GT_VisualInfo, visualinfo, GTBB_Recessed, TRUE, TAG_DONE);
DrawBevelBox (okno->RPort, offx + 301, offy + 41, 95, 14, GT_VisualInfo, visualinfo, GTBB_Recessed, TRUE, TAG_DONE);
DrawBevelBox (okno->RPort, offx + 301, offy + 72, 95, 14, GT_VisualInfo, visualinfo, GTBB_Recessed, TRUE, TAG_DONE);
DrawBevelBox (okno->RPort, offx + 14, offy + 120, 208, 11, GT_VisualInfo, visualinfo, GTBB_Recessed, TRUE, TAG_DONE);
DrawBevelBox (okno->RPort, offx + 235, offy + 96, 227, 35, GT_VisualInfo, visualinfo, GTBB_Recessed, TRUE, TAG_DONE);
DrawBevelBox (okno->RPort, offx + 14, offy + 136, 448, 24, GT_VisualInfo, visualinfo, GTBB_Recessed, TRUE, TAG_DONE);
DrawBevelBox (okno->RPort, offx + 27, offy + 141, 422, 14, GT_VisualInfo, visualinfo, GTBB_Recessed, TRUE, TAG_DONE);
PrintIText (okno->RPort, napisy, offx, offy);
}
void ZwolnijEkran (void)
{
if (visualinfo)
FreeVisualInfo (visualinfo);
if (ekran)
UnlockPubScreen (NULL, ekran);
if (font)
CloseFont (font);
}
void ZamknijOkno (void)
{
if (okno)
CloseWindow (okno);
if (gadzety)
FreeGadgets (gadzety);
}
void UsunStandardy (void)
{
struct Node *n;
while (n = (struct Node *) RemTail ((struct List *) &Standardy))
FreeMem (n, sizeof (*n));
}
void ZamknijBiblioteki (void)
{
if (requester)
FreeAslRequest ((struct FileRequester *) requester);
if (KonBase)
CloseLibrary ((struct Library *) KonBase);
if (IntuitionBase)
CloseLibrary ((struct Library *) IntuitionBase);
if (GfxBase)
CloseLibrary ((struct Library *) GfxBase);
if (GadToolsBase)
CloseLibrary ((struct Library *) GadToolsBase);
if (AslBase)
CloseLibrary ((struct Library *) AslBase);
}
void WczytajPlik (void)
{
struct Requester req;
UBYTE nazwapliku[255];
BPTR plik;
InitRequester (&req);
Request (&req, okno);
SetWindowPointer (okno,
WA_BusyPointer, 1,
WA_PointerDelay, 1,
TAG_DONE);
if (!(AslRequestTags ((struct FileRequester *) requester,
ASLFR_Window, okno,
ASLFR_TitleText, (STRPTR) "Wybierz plik do konwersji",
ASLFR_PositiveText, (STRPTR) "Wczytaj",
ASLFR_NegativeText, (STRPTR) "Zaniechaj",
TAG_DONE)))
goto przywroc_wskaznik;
strcpy (nazwapliku, requester->fr_Drawer);
AddPart (nazwapliku, requester->fr_File, 255);
if (rozmiar && bufor)
{
FreeMem (bufor, rozmiar);
rozmiar = (ULONG) NULL;
bufor = (UBYTE *) NULL;
}
Informacja ("Îadujë plik...");
if (!(plik = Open (nazwapliku, MODE_OLDFILE)))
{
Informacja ("Bîâd otwarcia pliku úródîowego!");
goto przywroc_wskaznik;
}
Seek (plik, NULL, OFFSET_END);
rozmiar = Seek (plik, NULL, OFFSET_BEGINNING);
if (!rozmiar)
{
Informacja ("Plik úródîowy jest pusty!");
goto zamknij;
}
if (!(bufor = AllocMem (rozmiar, MEMF_PUBLIC)))
{
Informacja ("Brak pamiëci!");
}
Read (plik, bufor, rozmiar);
PiszStandard (NULL, 61);
Informacja ("Rozpoznajë standard wczytanego pliku...");
if ((zrodlowy = RozpoznajStandard (bufor, rozmiar)) < 0)
{
Informacja ("Standard nie zostaî rozpoznany");
FreeMem (bufor, rozmiar);
rozmiar = (ULONG) NULL;
bufor = (UBYTE *) NULL;
}
else
{
PiszPlik (FilePart (nazwapliku));
PiszStandard (PobierzStandard (zrodlowy)->stan_Nazwa, 61);
Informacja ("Standard zostaî rozpoznany");
GT_SetGadgetAttrs (gad_zapisz, okno, NULL, GA_Disabled, 0, TAG_DONE);
}
zamknij:
Close (plik);
przywroc_wskaznik:
SetWindowPointer (okno, TAG_DONE);
EndRequest (&req, okno);
}
void ZapiszPlik (void)
{
struct Requester req;
UBYTE nazwapliku[256];
BPTR plik;
UBYTE *nb;
ULONG nr;
if (docelowy == zrodlowy)
{
Informacja ("Standardy: úródîowy i docelowy sâ takie same!");
return;
}
if (docelowy < 0)
{
Informacja ("Wybierz standard docelowy!!!");
return;
}
InitRequester (&req);
req.Flags = NOISYREQ;
Request (&req, okno);
SetWindowPointer (okno,
WA_BusyPointer, 1,
WA_PointerDelay, 1,
TAG_DONE);
if (!(AslRequestTags ((struct FileRequester *) requester,
ASLFR_Window, okno,
ASLFR_TitleText, (UBYTE *) "Zapisz plik jako",
ASLFR_PositiveText, "Zapisz",
ASLFR_NegativeText, "Zaniechaj",
TAG_DONE)))
goto przywroc_wskaznik;
strcpy (nazwapliku, requester->fr_Drawer);
AddPart (nazwapliku, requester->fr_File, 255);
if (!(plik = Open (nazwapliku, MODE_NEWFILE)))
{
Informacja ("Bîâd otwarcia pliku docelowego!");
goto przywroc_wskaznik;
}
switch (Konwertuj (bufor, rozmiar, &nb, &nr, MEMF_PUBLIC, zrodlowy, docelowy, flagi, funkcja, NULL))
{
case KONBLAD_OK:
Informacja ("Konwertujë - 100%");
Write (plik, nb, nr);
Close (plik);
FreeMem (nb, nr);
Informacja ("Zapisaîem plik");
break;
case KONBLAD_KONWERSJA_PRZERWANA:
Informacja ("Konwersja przerwana");
break;
case KONBLAD_BRAK_PAMIECI:
Informacja ("Brak pamiëci");
break;
}
FreeMem (bufor, rozmiar);
rozmiar = (ULONG) NULL;
bufor = (UBYTE *) NULL;
PiszPlik (NULL);
PiszStandard (NULL, 61);
GT_SetGadgetAttrs (gad_zapisz, okno, NULL, GA_Disabled, 1, TAG_DONE);
przywroc_wskaznik:
SetWindowPointer (okno, TAG_DONE);
EndRequest (&req, okno);
}
void Informacja (UBYTE * tekst)
{
SetAPen (okno->RPort, 0);
RectFill (okno->RPort, 40, 155, 440, 163);
SetAPen (okno->RPort, 1);
Move (okno->RPort, 40, 161);
Text (okno->RPort, tekst, strlen (tekst));
}
void PiszStandard (UBYTE * tekst, UWORD posy)
{
SetAPen (okno->RPort, 0);
RectFill (okno->RPort, 321, posy - 6, 385, posy + 2);
SetAPen (okno->RPort, 1);
Move (okno->RPort, 321, posy);
Text (okno->RPort, tekst, strlen (tekst));
}
void PiszPlik (UBYTE * tekst)
{
UWORD ile_liter;
SetAPen (okno->RPort, 0);
RectFill (okno->RPort, 303, 25, 447, 33);
SetAPen (okno->RPort, 1);
Move (okno->RPort, 303, 31);
ile_liter = strlen (tekst);
Text (okno->RPort, tekst, (ile_liter) < 18 ? ile_liter : 18);
}
BOOL __stdargs __saveds funkcja (ULONG calosc, ULONG gotowe, APTR atrapa)
{
struct IntuiMessage *msg;
UBYTE dane[20];
while (msg = GT_GetIMsg (okno->UserPort))
{
if (msg->Code == 27)
return (0);
}
sprintf ((UBYTE *) & dane, "Konwertujë - %d%%", ((gotowe * 100) / calosc));
Informacja ((UBYTE *) & dane);
return (1);
}
main ()
{
struct IntuiMessage *msg;
LONG klasa;
WORD kod;
struct Gadget *g;
ULONG stan = 1;
UWORD zoom = 0;
StworzSrodowisko ();
for (;;)
{
WaitPort (okno->UserPort);
while (msg = GT_GetIMsg (okno->UserPort))
{
klasa = msg->Class;
kod = msg->Code;
g = (struct Gadget *) msg->IAddress;
GT_ReplyIMsg (msg);
switch (klasa)
{
case IDCMP_CLOSEWINDOW:
ZamknijSrodowisko ();
case IDCMP_NEWSIZE:
if (zoom ^= 1)
RemoveGList (okno, gadzety, -1);
else
OdswierzOkno ();
break;
case IDCMP_GADGETUP:
switch (g->GadgetID)
{
case GD_Standardy:
docelowy = kod;
PiszStandard (PobierzStandard (docelowy)->stan_Nazwa, 92);
break;
case GD_Wczytaj:
WczytajPlik ();
break;
case GD_Zapisz:
ZapiszPlik ();
break;
case GD_CRLF:
flagi ^= KONWF_ZMIENNL;
stan ^= 1;
GT_SetGadgetAttrs (gad_eof, okno, NULL, GA_Disabled, stan, TAG_END);
break;
case GD_DodajEOF:
flagi ^= KONWF_DODAJEOF;
}
}
}
}
}