1 /////////////////////////////////////////////////////////////////////////////
2 // Name:        jsonreader.h
3 // Purpose:     the parser of JSON text
4 // Author:      Luciano Cattani
5 // Created:     2007/09/15
6 // RCS-ID:      $Id: jsonreader.h,v 1.3 2008/03/03 19:05:45 luccat Exp $
7 // Copyright:   (c) 2007 Luciano Cattani
8 // Licence:     wxWidgets licence
9 /////////////////////////////////////////////////////////////////////////////
10 
11 #if !defined( _WX_JSONREADER_H )
12 #define _WX_JSONREADER_H
13 
14 #ifdef __GNUG__
15     #pragma interface "jsonreader.h"
16 #endif
17 
18 // For compilers that support precompilation, includes "wx/wx.h".
19 #include "wx/wxprec.h"
20 
21 #ifdef __BORLANDC__
22     #pragma hdrstop
23 #endif
24 
25 // for all others, include the necessary headers (this file is usually all you
26 // need because it includes almost all "standard" wxWidgets headers)
27 #ifndef WX_PRECOMP
28     #include <wx/stream.h>
29     #include <wx/string.h>
30     #include <wx/arrstr.h>
31 #endif
32 
33 
34 #include "json_defs.h"
35 #include "jsonval.h"
36 
37 // The flags of the parser
38 enum {
39     wxJSONREADER_STRICT          = 0,
40     wxJSONREADER_ALLOW_COMMENTS  = 1,
41     wxJSONREADER_STORE_COMMENTS  = 2,
42     wxJSONREADER_CASE            = 4,
43     wxJSONREADER_MISSING         = 8,
44     wxJSONREADER_MULTISTRING     = 16,
45     wxJSONREADER_COMMENTS_AFTER  = 32,
46     wxJSONREADER_NOUTF8_STREAM   = 64,
47     wxJSONREADER_MEMORYBUFF      = 128,
48 
49     wxJSONREADER_TOLERANT        = wxJSONREADER_ALLOW_COMMENTS | wxJSONREADER_CASE |
50                                  wxJSONREADER_MISSING | wxJSONREADER_MULTISTRING,
51     wxJSONREADER_COMMENTS_BEFORE = wxJSONREADER_ALLOW_COMMENTS | wxJSONREADER_STORE_COMMENTS
52 };
53 
54 
55 class WXDLLIMPEXP_JSON  wxJSONReader
56 {
57 public:
58     wxJSONReader( int flags = wxJSONREADER_TOLERANT, int maxErrors = 30 );
59     virtual ~wxJSONReader();
60 
61     int Parse( const wxString& doc, wxJSONValue* val );
62     int Parse( wxInputStream& doc, wxJSONValue* val );
63 
64     int   GetDepth() const;
65     int   GetErrorCount() const;
66     int   GetWarningCount() const;
67     const wxArrayString& GetErrors() const;
68     const wxArrayString& GetWarnings() const;
69 
70     static int  UTF8NumBytes( char ch );
71 
72 #if defined( wxJSON_64BIT_INT )
73     static bool Strtoll( const wxString& str, wxInt64* i64 );
74     static bool Strtoull( const wxString& str, wxUint64* ui64 );
75     static bool DoStrto_ll( const wxString& str, wxUint64* ui64, wxChar* sign );
76 #endif
77 
78 protected:
79 
80     int  DoRead( wxInputStream& doc, wxJSONValue& val );
81     void AddError( const wxString& descr );
82     void AddError( const wxString& fmt, const wxString& str );
83     void AddError( const wxString& fmt, wxChar ch );
84     void AddWarning( int type, const wxString& descr );
85     int  GetStart( wxInputStream& is );
86     int  ReadChar( wxInputStream& is );
87     int  PeekChar( wxInputStream& is );
88     void StoreValue( int ch, const wxString& key, wxJSONValue& value, wxJSONValue& parent );
89     int  SkipWhiteSpace( wxInputStream& is );
90     int  SkipComment( wxInputStream& is );
91     void StoreComment( const wxJSONValue* parent );
92     int  ReadString(  wxInputStream& is, wxJSONValue& val );
93     int  ReadToken(  wxInputStream& is, int ch, wxString& s );
94     int  ReadValue(  wxInputStream& is, int ch, wxJSONValue& val );
95     int  ReadUES(  wxInputStream& is, char* uesBuffer );
96     int  AppendUES( wxMemoryBuffer& utf8Buff, const char* uesBuffer );
97     int  NumBytes( char ch );
98     int  ConvertCharByChar( wxString& s, const wxMemoryBuffer& utf8Buffer );
99     int  ReadMemoryBuff( wxInputStream& is, wxJSONValue& val );
100 
101     //! Flag that control the parser behaviour,
102     int  m_flags;
103 
104     //! Maximum number of errors stored in the error's array
105     int  m_maxErrors;
106 
107     //! The current line number (start at 1).
108     int  m_lineNo;
109 
110     //! The current column number (start at 1).
111     int  m_colNo;
112 
113     //! The current level of object/array annidation (start at ZERO).
114     int  m_level;
115 
116     //! The depth level of the read JSON text
117     int  m_depth;
118 
119     //! The pointer to the value object that is being read.
120     wxJSONValue* m_current;
121 
122     //! The pointer to the value object that was last stored.
123     wxJSONValue* m_lastStored;
124 
125     //! The pointer to the value object that will be read.
126     wxJSONValue* m_next;
127 
128     //! The comment string read by SkipComment().
129     wxString     m_comment;
130 
131     //! The starting line of the comment string.
132     int          m_commentLine;
133 
134     //! The array of error messages.
135     wxArrayString m_errors;
136 
137     //! The array of warning messages.
138     wxArrayString m_warnings;
139 
140     //! The character read by the PeekChar() function (-1 none)
141     int           m_peekChar;
142 
143     //! ANSI: do not convert UTF-8 strings
144     bool        m_noUtf8;
145 };
146 
147 
148 #endif            // not defined _WX_JSONREADER_H
149 
150 
151