Sivelkiria Operating System repository

Commit c4a4262d authored by Alexey's avatar Alexey
Browse files

init

parent 6b29ef37
#include "interface_parser.hpp"
interface_parser::interface_parser( const std::string filename ) {
doc.LoadFile( filename.c_str() );
tinyxml2::XMLElement *ptr;
ptr= doc.FirstChildElement( "Interface" )->FirstChildElement();
has_template= strcmp( ptr->Name(), "Template" ) == 0;
if ( has_template ) parse_template( ptr );
ptr= doc.FirstChildElement( "Interface" );
if ( ptr != nullptr ) parse_interface( ptr );
}
void interface_parser::parse_template( tinyxml2::XMLElement *ptr ) {
tinyxml2::XMLNode *tmpnode;
for ( tinyxml2::XMLNode *node= ptr->FirstChild(); node != nullptr;
node= node->NextSibling() ) {
tinyxml2::XMLElement *elem= node->ToElement();
std::pair<std::string, std::string> tmp=
std::pair<std::string, std::string>(
std::string( elem->Attribute( "name" ) ),
std::string( elem->Attribute( "defval" ) ) );
templates.push_back( tmp );
tmpnode= node->FirstChild();
description_template_names.push_back(
std::string( tmpnode->ToElement()->GetText() ) ); // description
// tmpnode->ToElement()->Attribute(language)
}
}
void interface_parser::parse_interface( tinyxml2::XMLElement *ptr ) {
// ptr->ToElement()->Attribute("xsi:schemaLocation");
const char ch[]= "support-type";
supp_type= std::string( ptr->Attribute( ch ) );
tinyxml2::XMLNode *ptr2= ptr->FirstChild();
if ( strcmp( ptr2->ToElement()->Name(), "Template" ) == 0 )
ptr2= ptr2->NextSibling();
// name
name= std::string( ptr2->ToElement()->GetText() );
ptr2= ptr2->NextSibling(); // major
major= std::string( ptr2->ToElement()->GetText() );
ptr2= ptr2->NextSibling(); // minor-max
minor= std::string( ptr2->ToElement()->GetText() );
ptr2= ptr2->NextSibling(); // parse_permision()
parse_permission( ptr2, &perm );
ptr2= ptr2->NextSibling();
while ( strcmp( ptr2->ToElement()->Name(), "method" ) == 0 ) {
parse_method( ptr2 );
ptr2= ptr2->NextSibling();
}
description= std::string( ptr2->ToElement()->GetText() ); // get description
}
void interface_parser::parse_permission(
tinyxml2::XMLNode *ptr, permission *_perm ) {
//надо прописать как права выглядят//
tinyxml2::XMLElement *perm= ptr->ToElement();
const char *default_= perm->Attribute( "default" );
if ( default_ != nullptr )
_perm->default_perm= std::string( default_ ); // may be parse other fields
const char *caller_int= perm->Attribute( "caller-interface" );
if ( caller_int != nullptr ) _perm->caller_int= std::string( caller_int );
const char *caller_method= perm->Attribute( "caller-method" );
if ( caller_method != nullptr )
_perm->caller_method= std::string( caller_method );
const char *caller_obj= perm->Attribute( "caller-object" );
if ( caller_obj != nullptr ) _perm->caller_obj= std::string( caller_obj );
tinyxml2::XMLNode *tmp= ptr->FirstChild();
if ( tmp != nullptr ) { //если есть вложенные права
if ( _perm->subperm == nullptr ) //если нет вектора подправ
_perm->subperm= new std::vector<permission>; //то создаем его
while ( tmp != nullptr ) { //пока подправа еще не перебраны
_perm->subperm->push_back( *( new permission ) ); //добавляем право
parse_permission( tmp, &*_perm->subperm->rbegin() ); //применяем право
tmp= tmp->NextSibling(); //переключаемся на следующее подправо
}
} else
_perm->subperm= nullptr; //инициализируем на всякий
}
void interface_parser::parse_method( tinyxml2::XMLNode *ptr ) {
tinyxml2::XMLElement *method= ptr->ToElement();
methods.resize( methods.size() + 1 );
methods.rbegin()->name= std::string( method->Attribute( "name" ) );
methods.rbegin()->id= std::string( method->Attribute( "id" ) );
methods.rbegin()->minor_since=
std::string( method->Attribute( "minor-since" ) );
tinyxml2::XMLNode *tmp= ptr->FirstChild();
if ( strcmp( tmp->ToElement()->Name(), "cdata-default" ) == 0 ) {
// insert to generator//how?
tmp= tmp->NextSibling();
}
parse_permission( tmp, &( methods.rbegin()->perm ) );
description_methods.push_back( descr_method() );
tmp= tmp->NextSibling();
description_methods.rbegin()->descr_main=
std::string( tmp->ToElement()->GetText() ); // description
tmp= tmp->NextSibling();
std::string ret_type= std::string( tmp->ToElement()->Attribute( "type" ) );
std::pair<std::string, std::string> *p= nullptr;
for ( std::vector<std::pair<std::string, std::string>>::iterator it=
templates.begin();
it != templates.end(); ++it ) // find in templates
if ( it->first == ret_type ) {
p= &*it;
break;
}
methods.rbegin()->ret.first= ret_type;
methods.rbegin()->ret.second= p;
if ( tmp->FirstChildElement() != nullptr )
description_methods.rbegin()->descr_ret= std::string(
tmp->FirstChildElement()->GetText() ); // add description of return
else
description_methods.rbegin()->descr_ret= "";
tmp= tmp->NextSibling(); // arg
while ( tmp != nullptr ) {
parse_arg( tmp );
tmp= tmp->NextSibling(); // arg
}
}
void interface_parser::parse_arg( tinyxml2::XMLNode *ptr ) {
tinyxml2::XMLElement *arg= ptr->ToElement();
methods.rbegin()->args.resize( methods.rbegin()->args.size() + 1 );
std::vector<std::pair<_arg, std::pair<std::string, std::string> *>>::
reverse_iterator gt= methods.rbegin()->args.rbegin();
gt->first.mode= std::string( arg->Attribute( "mode" ) ); // get mode
gt->first.type= std::string( arg->Attribute( "type" ) ); // get type
gt->second= nullptr;
for ( std::vector<std::pair<std::string, std::string>>::iterator it=
templates.begin();
it != templates.end(); ++it ) // find in templates
if ( it->first == gt->first.type ) {
gt->second= &*it;
break;
}
gt->first.name= std::string( arg->Attribute( "name" ) ); // get name arg
gt->first.id= std::string( arg->Attribute( "id" ) ); // get arg id
std::string tmp= std::string( ptr->FirstChild()->ToElement()->GetText() );
description_methods.rbegin()->descr_arg.resize(
description_methods.rbegin()->descr_arg.size() + 1 );
*description_methods.rbegin()->descr_arg.rbegin()= tmp; // get description
}
#pragma once
#include "./tinyxml2.h"
#include <exception>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <iterator>
struct _arg {
std::string mode, type, name, id;
};
struct permission {
std::string default_perm, caller_int, caller_method, caller_obj;
std::vector<permission> *subperm= nullptr;
};
struct method {
//т к массив шиблонов уже не будет меняться, можно смело взять указатель
std::vector<std::pair<_arg, std::pair<std::string, std::string> *>>
args; // 2-е - указатель на шаблон
// if == nullptr => is not template
std::pair<std::string, std::pair<std::string, std::string> *> ret; // return
// if == nullptr => is not template
std::string name;
permission perm;
std::string id; //его можно в int не превращать
std::string minor_since;
};
struct descr_method {
std::string descr_main, descr_ret;
std::vector<std::string> descr_arg;
};
class interface_parser {
public:
std::string description;
bool has_template;
tinyxml2::XMLDocument doc;
std::vector<std::pair<std::string, std::string>> templates; // name and defval
std::vector<std::string> description_template_names;
std::vector<method> methods;
std::vector<descr_method> description_methods;
std::string supp_type, name, major, minor;
permission perm;
interface_parser( const std::string filename );
private:
void parse_template( tinyxml2::XMLElement *ptr );
void parse_interface( tinyxml2::XMLElement *ptr );
void parse_permission( tinyxml2::XMLNode *ptr, permission *_perm );
void parse_method( tinyxml2::XMLNode *ptr );
void parse_arg( tinyxml2::XMLNode *ptr );
};
#include "struct_parser.hpp"
struct_parser::struct_parser( const std::string filename ) {
doc.LoadFile( filename.c_str() );
tinyxml2::XMLElement *ptr;
ptr= doc.FirstChildElement( "Structure" )->FirstChildElement();
has_template= strcmp( ptr->Name(), "Template" ) == 0;
if ( has_template ) parse_template( ptr );
ptr= doc.FirstChildElement( "Structure" );
if ( ptr != nullptr ) parse_struct( ptr );
}
void struct_parser::parse_template( tinyxml2::XMLElement *ptr ) {
tinyxml2::XMLNode *tmpnode;
for ( tinyxml2::XMLNode *node= ptr->FirstChild(); node != nullptr;
node= node->NextSibling() ) {
tinyxml2::XMLElement *elem= node->ToElement();
std::pair<std::string, std::string> tmp=
std::pair<std::string, std::string>(
std::string( elem->Attribute( "name" ) ),
std::string( elem->Attribute( "defval" ) ) );
templates.push_back( tmp );
tmpnode= node->FirstChild();
description_template_names.push_back(
std::string( tmpnode->ToElement()->GetText() ) ); // description
// tmpnode->ToElement()->Attribute(language)
}
}
void struct_parser::parse_struct( tinyxml2::XMLElement *ptr ) {
// ptr->ToElement()->Attribute("xsi:schemaLocation");
const char ch[]= "support-type";
supp_type= std::string( ptr->Attribute( ch ) );
tinyxml2::XMLNode *ptr2= ptr->FirstChild();
if ( strcmp( ptr2->ToElement()->Name(), "Template" ) == 0 )
ptr2= ptr2->NextSibling();
// name
name= std::string( ptr2->ToElement()->GetText() );
ptr2= ptr2->NextSibling(); // major
major= std::string( ptr2->ToElement()->GetText() );
ptr2= ptr2->NextSibling(); // minor-max
minor= std::string( ptr2->ToElement()->GetText() );
ptr2= ptr2->NextSibling(); // first field
while ( strcmp( ptr2->ToElement()->Name(), "field" ) == 0 ) {
parse_field( ptr2 );
ptr2= ptr2->NextSibling();
}
description= std::string( ptr2->ToElement()->GetText() ); // get description
}
void struct_parser::parse_field( tinyxml2::XMLNode *ptr ) {
tinyxml2::XMLElement *arg= ptr->ToElement();
// methods.rbegin()->args.resize( methods.rbegin()->args.size() + 1 );
fields.resize( fields.size() + 1 );
std::vector<std::pair<_field, std::pair<std::string, std::string> *>>::
reverse_iterator gt= fields.rbegin();
gt->first.type= std::string( arg->Attribute( "type" ) ); // get type
gt->second= nullptr;
for ( std::vector<std::pair<std::string, std::string>>::iterator it=
templates.begin();
it != templates.end(); ++it ) // find in templates
if ( it->first == gt->first.type ) {
gt->second= &*it;
break;
}
gt->first.name= std::string( arg->Attribute( "name" ) ); // get name arg
gt->first.id= std::string( arg->Attribute( "id" ) ); // get arg id
std::string tmp= std::string( ptr->FirstChild()->ToElement()->GetText() );
fields_descriptions.resize( fields_descriptions.size() + 1 );
*fields_descriptions.rbegin()= tmp; // get description
}
#pragma once
#include "./tinyxml2.h"
#include <exception>
#include <vector>
#include <string>
#include <map>
#include <algorithm>
#include <iterator>
struct _field {
std::string type, name, id;
};
class struct_parser {
public:
std::string description;
bool has_template;
tinyxml2::XMLDocument doc;
std::vector<std::pair<std::string, std::string>> templates; // name and defval
std::vector<std::string> description_template_names;
std::vector<std::pair<_field, std::pair<std::string, std::string> *>> fields;
std::vector<std::string> fields_descriptions;
std::string supp_type, name, major, minor;
struct_parser( const std::string filename );
private:
void parse_template( tinyxml2::XMLElement *ptr );
void parse_struct( tinyxml2::XMLElement *ptr );
void parse_field( tinyxml2::XMLNode *ptr );
};
/*
Original code by Lee Thomason (www.grinninglizard.com)
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any
damages arising from the use of this software.
Permission is granted to anyone to use this software for any
purpose, including commercial applications, and to alter it and
redistribute it freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must
not claim that you wrote the original software. If you use this
software in a product, an acknowledgment in the product documentation
would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and
must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*/
#include "tinyxml2.h"
#include <new> // yes, this one new style header, is in the Android SDK.
#if defined( ANDROID_NDK ) || defined( __BORLANDC__ ) || defined( __QNXNTO__ )
#include <stddef.h>
#include <stdarg.h>
#else
#include <cstddef>
#include <cstdarg>
#endif
#if defined( _MSC_VER ) && ( _MSC_VER >= 1400 ) && ( !defined WINCE )
// Microsoft Visual Studio, version 2005 and higher. Not WinCE.
/*int _snprintf_s(
char *buffer,
size_t sizeOfBuffer,
size_t count,
const char *format [,
argument] ...
);*/
static inline int
TIXML_SNPRINTF( char *buffer, size_t size, const char *format, ... ) {
va_list va;
va_start( va, format );
const int result= vsnprintf_s( buffer, size, _TRUNCATE, format, va );
va_end( va );
return result;
}
static inline int
TIXML_VSNPRINTF( char *buffer, size_t size, const char *format, va_list va ) {
const int result= vsnprintf_s( buffer, size, _TRUNCATE, format, va );
return result;
}
#define TIXML_VSCPRINTF _vscprintf
#define TIXML_SSCANF sscanf_s
#elif defined _MSC_VER
// Microsoft Visual Studio 2003 and earlier or WinCE
#define TIXML_SNPRINTF _snprintf
#define TIXML_VSNPRINTF _vsnprintf
#define TIXML_SSCANF sscanf
#if ( _MSC_VER < 1400 ) && ( !defined WINCE )
// Microsoft Visual Studio 2003 and not WinCE.
#define TIXML_VSCPRINTF \
_vscprintf // VS2003's C runtime has this, but VC6 C runtime or WinCE SDK
// doesn't have.
#else
// Microsoft Visual Studio 2003 and earlier or WinCE.
static inline int TIXML_VSCPRINTF( const char *format, va_list va ) {
int len= 512;
for ( ;; ) {
len= len * 2;
char *str= new char[ len ]();
const int required= _vsnprintf( str, len, format, va );
delete[] str;
if ( required != -1 ) {
TIXMLASSERT( required >= 0 );
len= required;
break;
}
}
TIXMLASSERT( len >= 0 );
return len;
}
#endif
#else
// GCC version 3 and higher
//#warning( "Using sn* functions." )
#define TIXML_SNPRINTF snprintf
#define TIXML_VSNPRINTF vsnprintf
static inline int TIXML_VSCPRINTF( const char *format, va_list va ) {
int len= vsnprintf( 0, 0, format, va );
TIXMLASSERT( len >= 0 );
return len;
}
#define TIXML_SSCANF sscanf
#endif
#if defined( _WIN64 )
#define TIXML_FSEEK _fseeki64
#define TIXML_FTELL _ftelli64
#elif defined( __APPLE__ )
#define TIXML_FSEEK fseeko
#define TIXML_FTELL ftello
#elif defined( __x86_64__ )
#define TIXML_FSEEK fseeko64
#define TIXML_FTELL ftello64
#else
#define TIXML_FSEEK fseek
#define TIXML_FTELL ftell
#endif
static const char LINE_FEED=
static_cast<char>( 0x0a ); // all line endings are normalized to LF
static const char LF= LINE_FEED;
static const char CARRIAGE_RETURN=
static_cast<char>( 0x0d ); // CR gets filtered out
static const char CR= CARRIAGE_RETURN;
static const char SINGLE_QUOTE= '\'';
static const char DOUBLE_QUOTE= '\"';
// Bunch of unicode info at:
// http://www.unicode.org/faq/utf_bom.html
// ef bb bf (Microsoft "lead bytes") - designates UTF-8
static const unsigned char TIXML_UTF_LEAD_0= 0xefU;
static const unsigned char TIXML_UTF_LEAD_1= 0xbbU;
static const unsigned char TIXML_UTF_LEAD_2= 0xbfU;
namespace tinyxml2 {
struct Entity {
const char *pattern;
int length;
char value;
};
static const int NUM_ENTITIES= 5;
static const Entity entities[ NUM_ENTITIES ]= {
{ "quot", 4, DOUBLE_QUOTE },
{ "amp", 3, '&' },
{ "apos", 4, SINGLE_QUOTE },
{ "lt", 2, '<' },
{ "gt", 2, '>' } };
StrPair::~StrPair() { Reset(); }
void StrPair::TransferTo( StrPair *other ) {
if ( this == other ) { return; }
// This in effect implements the assignment operator by "moving"
// ownership (as in auto_ptr).
TIXMLASSERT( other != 0 );
TIXMLASSERT( other->_flags == 0 );
TIXMLASSERT( other->_start == 0 );
TIXMLASSERT( other->_end == 0 );
other->Reset();
other->_flags= _flags;
other->_start= _start;
other->_end= _end;
_flags= 0;
_start= 0;
_end= 0;
}
void StrPair::Reset() {
if ( _flags & NEEDS_DELETE ) { delete[] _start; }
_flags= 0;
_start= 0;
_end= 0;
}
void StrPair::SetStr( const char *str, int flags ) {
TIXMLASSERT( str );
Reset();
size_t len= strlen( str );
TIXMLASSERT( _start == 0 );
_start= new char[ len + 1 ];
memcpy( _start, str, len + 1 );
_end= _start + len;
_flags= flags | NEEDS_DELETE;
}
char *StrPair::ParseText(
char *p, const char *endTag, int strFlags, int *curLineNumPtr ) {
TIXMLASSERT( p );
TIXMLASSERT( endTag && *endTag );
TIXMLASSERT( curLineNumPtr );
char *start= p;
const char endChar= *endTag;
size_t length= strlen( endTag );
// Inner loop of text parsing.
while ( *p ) {
if ( *p == endChar && strncmp( p, endTag, length ) == 0 ) {
Set( start, p, strFlags );
return p + length;
} else if ( *p == '\n' ) {
++( *curLineNumPtr );
}
++p;
TIXMLASSERT( p );
}
return 0;
}
char *StrPair::ParseName( char *p ) {
if ( !p || !( *p ) ) { return 0; }
if ( !XMLUtil::IsNameStartChar( *p ) ) { return 0; }
char *const start= p;
++p;
while ( *p && XMLUtil::IsNameChar( *p ) ) { ++p; }
Set( start, p, 0 );
return p;
}
void StrPair::CollapseWhitespace() {
// Adjusting _start would cause undefined behavior on delete[]
TIXMLASSERT( ( _flags & NEEDS_DELETE ) == 0 );
// Trim leading space.
_start= XMLUtil::SkipWhiteSpace( _start, 0 );
if ( *_start ) {
const char *p= _start; // the read pointer
char *q= _start; // the write pointer
while ( *p ) {
if ( XMLUtil::IsWhiteSpace( *p ) ) {
p= XMLUtil::SkipWhiteSpace( p, 0 );
if ( *p == 0 ) {
break; // don't write to q; this trims the trailing space.
}
*q= ' ';
++q;
}
*q= *p;
++q;
++p;
}
*q= 0;
}
}
const char *StrPair::GetStr() {
TIXMLASSERT( _start );
TIXMLASSERT( _end );
if ( _flags & NEEDS_FLUSH ) {
*_end= 0;
_flags^= NEEDS_FLUSH;
if ( _flags ) {
const char *p= _start; // the read pointer
char *q= _start; // the write pointer
while ( p < _end ) {
if ( ( _flags & NEEDS_NEWLINE_NORMALIZATION ) && *p == CR ) {
// CR-LF pair becomes LF
// CR alone becomes LF
// LF-CR becomes LF
if ( *( p + 1 ) == LF ) {
p+= 2;
} else {
++p;
}
*q= LF;
++q;
} else if ( ( _flags & NEEDS_NEWLINE_NORMALIZATION ) && *p == LF ) {
if ( *( p + 1 ) == CR ) {
p+= 2;
} else {
++p;
}
*q= LF;
++q;
} else if ( ( _flags & NEEDS_ENTITY_PROCESSING ) && *p == '&' ) {
// Entities handled by tinyXML2:
// - special entities in the entity table [in/out]
// - numeric character reference [in]
// &#20013; or &#x4e2d;
if ( *( p + 1 ) == '#' ) {
const int buflen= 10;
char buf[ buflen ]= { 0 };
int len= 0;
const char *adjusted=
const_cast<char *>( XMLUtil::GetCharacterRef( p, buf, &len ) );
if ( adjusted == 0 ) {