Main Page | Class List | Directories | File List | Class Members | File Members

iban.h

Go to the documentation of this file.
00001 
00030 #ifndef IBAN_H
00031 #define IBAN_H
00032 
00039 #ifdef __cplusplus
00040 
00041 #include <iostream>
00042 #include <fstream>
00043 #include <sstream>
00044 #include <vector>
00045 #include <map>
00046 #include <ctype.h>
00047 
00054 class Iban
00055 {
00056 public:
00057 
00059   Iban();
00060 
00062   Iban(const Iban& iban);
00063 
00074   Iban(const std::string& iban, bool normalize = true);
00075 
00077   ~Iban();
00078 
00080   const std::string& transmissionForm() const {
00081     return m_transmission;
00082   }
00083 
00085   const std::string& printableForm() {
00086     if (m_printable.empty()) 
00087       m_printable = createPrintable();
00088     return m_printable;
00089   }
00090 
00091 private:
00092   std::string m_transmission; 
00093   std::string m_printable;    
00094 
00096   static std::string createTransmission(const std::string& iban_str);
00098   std::string createPrintable() const;
00099 };
00100 
00112 class IbanCheck {
00113 public:
00114 
00116   enum Result {
00117     // do not change anything here without changing
00118     // the initialisation of m_ResultText!
00119     OK = 0,            
00120     TOO_SHORT,         
00121     PREFIX_NOT_FOUND,  
00122     WRONG_LENGTH,      
00123     COUNTRY_NOT_FOUND, 
00124     WRONG_COUNTRY,     
00125     BAD_CHECKSUM,      
00126   };
00127 
00128 
00144   IbanCheck(const std::string& filename = "");
00145 
00147   ~IbanCheck();
00148 
00157   Result check(const Iban& iban, const std::string& country = "") const {
00158     return check(iban.transmissionForm(), country); }
00159 
00164   Result check(const std::string& iban, const std::string& country = "") const;
00165 
00173   Result bic_position(const std::string& iban, int& start, int& end) const;
00174 
00182   static const char *resultText(Result res);
00183 
00187   bool error() const { return m_IbanSpec.size() == 0; }
00188 
00192   bool selftest();
00193 
00194 private:
00195 
00196   static const char *m_ResultText[];
00197 
00198   typedef std::vector<std::string> svector;
00199 
00200   struct Spec {
00201     std::string prefix;
00202     unsigned int length;
00203     unsigned int bic_start, bic_end;
00204     std::string example;
00205   };
00206 
00207   typedef std::map<std::string,Spec*> specmap;
00208 
00209   struct Country {
00210     std::string country;
00211     svector prefixes;
00212   };
00213 
00214   typedef std::map<std::string, Country*> countrymap;
00215 
00216   friend std::istream& operator>>(std::istream &is, Spec &spec);
00217   friend std::istream& operator>>(std::istream &is, Country &c);
00218 
00219   bool readSpecTable(std::istream &fin, const std::string& stopcomment);
00220   bool readCountryTable(std::istream &fin);
00221   static int to_number(char c) { return c - 'A' + 10; }
00222   static std::string iban2number(const std::string& iban);
00223   static int modulo97(const std::string& number);
00224 
00225   specmap m_IbanSpec;
00226   countrymap m_CountryMap;
00227 };
00228 
00229 typedef IbanCheck::Result IbanCheck_Result;
00230 extern "C" {
00231 #else /* __cplusplus */
00232   typedef struct IbanCheck IbanCheck;
00233   typedef struct Iban Iban;
00234   typedef int IbanCheck_Result;
00235 #endif /* __cplusplus */
00236 
00237   /* @{ */
00238 
00254   IbanCheck *IbanCheck_new(const char *filename);
00256   void IbanCheck_free(IbanCheck *p);
00262   IbanCheck_Result IbanCheck_check_str(const IbanCheck *p,
00263                                               const char *iban,
00264                                               const char *country);
00274   IbanCheck_Result IbanCheck_check_iban(const IbanCheck *p,
00275                                                const Iban *iban,
00276                                                const char *country);
00285   IbanCheck_Result IbanCheck_bic_position(const IbanCheck *p, 
00286                                                  const char *iban,
00287                                                  int *start, int *end);
00295   const char *IbanCheck_resultText(IbanCheck_Result res);
00299   int IbanCheck_error(const IbanCheck *p);
00303   int IbanCheck_selftest(IbanCheck *p);
00304   /* @} */
00305 
00307   /* @{ */
00309   Iban *Iban_new(const char* iban, int normalize);
00311   void Iban_free(Iban *p);
00313   const char *Iban_transmissionForm(const Iban *iban);
00315   const char *Iban_printableForm(Iban *iban);
00316   /* @} */
00317 #ifdef __cplusplus
00318 }
00319 #endif /* __cplusplus */
00320 
00321 #endif /* IBAN_H */

Generated on Thu Nov 23 13:33:27 2006 for ktoblzcheck by  doxygen 1.4.1