source: projects/punch-card/punch-card-editor/src/text/codec.h @ 52

Last change on this file since 52 was 52, checked in by sven, 14 years ago

Punch Card Editor: Code reordering and class renaming, rewriting.
Now using the namespace QPunchCard everywhere.

Parted the whole application into 5 parts/directories:

  • app: The application core with MainWindow and main() function
  • qpunchcard: Everything directly binary card related (no interpretation): modeling, input/output, view. Most important classes are Card, Deck, FileFormat, Widget
  • text: Everything related to text interpretation of Cards/Card Decks. Having the abstract Codec classes and the Text::Editor, Text::EditorDock
  • deckviewer: Application components like binary card editing central widget (CardEditor) and Navigator (Model View Controller classes)
  • driver: Basis for the driver framework, must be written soon.

Deck now hides the complete Storage to implement frontend methods that
implement versioning (Undo framework). All code was cleaned up, but doesn't
compile right now (still quite some non heavy errors).

-- Sven @ workstation

File size: 3.0 KB
Line 
1#ifndef CODEC_H
2#define CODEC_H
3
4#include <QString>
5#include <QList>
6
7namespace QPunchCard {
8        class Codec;
9}
10
11// der define ist fuer cardcodes.h
12#define ERROR     -5
13
14#include "qpunchcard/card.h"
15#include "qpunchcard/deck.h"
16
17namespace QPunchCard {
18
19/**
20 * Abstracte Codec-Klasse. Implementierungen muessen toAscii/fromAscii
21 * implementieren und sollten canEncode() implementieren. Fuer Namenszuordnung
22 * und fortgeschrittene Erstellung ist CodecFactory zustaendig.
23 **/
24class Codec {
25public:
26        // soll const sein, weil Codec unveraenderbar *immer* das gleiche
27        // Ergebnis liefern soll, nach Erzeugung. Das ist auch noetig fuer
28        // CharArrayCodec.
29        const char illegal;
30        /// @param illegal_character Zeichen fuer nicht existente Zahlen
31        Codec(char illegal_character = '~') : illegal(illegal_character) { }
32        virtual ~Codec() {}
33        virtual char toAscii(const Column* col) const = 0;
34        virtual QString toAscii(const Card* target_card) const;
35        virtual Column fromAscii(char ch) const = 0;
36        void fromAscii(const QString& string, Card* target_card) const;
37
38        virtual bool canEncode(const Column* col) const { return toAscii(col) != illegal; }
39        virtual bool canEncode(char /*char*/ ) const = 0;
40        bool canEncode(const QString& string) const;
41
42        int countIllegalColumns(const Card* card) const;
43        int countIllegalColumns(const Deck* deck) const;
44};
45
46/**
47 * Codec-Implementierung, der mithilfe von hardgecodeten Codetabellen
48 * (von Douglas Jones uebernommen) eine Umwandlung char->Column und andersrum
49 * durchfuehren kann. Letzteres geht genauso perfomant dank 4kb grosser
50 * inverser Tabelle, die bei Objektkonstruktion angelegt wird.
51 **/
52class CharArrayCodec : public Codec {
53        const int* table;
54        char inverse_table[4096];
55
56public:
57        CharArrayCodec(const int* table, char illegal = '~');// : Codec(illegal), table(table) {}
58        char toAscii(const Column* col) const { return inverse_table[*col]; }
59        Column fromAscii(char ch) const { return Column(canEncode(ch) ? table[ch]: 0); }
60
61        bool canEncode(const Column* col) const { return inverse_table[*col] != illegal; }
62        bool canEncode(char ch) const {
63                bool r = (table[ch] != ERROR);
64                if(ch < ' ' || ch > 'z') r = false;
65                //qDebug("CharArrayCodec: %c is a %s character", ch, r ? "valid" : "invalid");
66                return r; }
67};
68
69/**
70 * Factory-Klasse, die alle bekannten Codecs zurueckgeben kann. Wesentliches
71 * Merkmal ist die Faehigkeit, mit Namen (QStrings) von Codecs umgehen zu
72 * koennen und Codecs aus Namen erstellen zu koennen. Ausserdem gibt es
73 * Algorithmen zum Finden des bestmoeglichen Codecs zu einer Card/einem Deck,
74 * die mit Codec:countIllegalColumns() arbeitet.
75 **/
76class CodecFactory {
77public:
78        // TODO: Codec-Caching (Codec-Constructor privatisieren, dafuer hier caching
79        // betreiben) weil einige codecs recht teuer zu erstellen sind (CharArrayCodec)
80        static QList<QString> availableCodecs();
81        static const Codec* createCodec(const QString& name, char illegal_character = '~');
82        static const QString autoDetectCodec(const Card* card);
83        static const QString autoDetectCodec(const Deck* deck);
84};
85
86
87}; // Namespace
88#endif // CODEC_H
Note: See TracBrowser for help on using the repository browser.
© 2008 - 2013 technikum29 • Sven Köppel • Some rights reserved
Powered by Trac
Expect where otherwise noted, content on this site is licensed under a Creative Commons 3.0 License