/* linphone Copyright (C) 2013 Belledonne Communications SARL Simon Morlat (simon.morlat@linphone.org) This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ #include #include "generator.hh" #ifdef WIN32 #include #define strncasecmp _strnicmp #endif string to_lower(const string &str){ string res=str; for(string::iterator it=res.begin();it!=res.end();++it){ *it=tolower(*it); } return res; } CplusplusGenerator::CplusplusGenerator(){ } void CplusplusGenerator::generate(Project *proj){ list classes=proj->getClasses(); mCurProj=proj; #ifndef WIN32 mkdir(proj->getName().c_str(),S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IROTH); #else _mkdir(proj->getName().c_str()); #endif for_each(classes.begin(),classes.end(),bind1st(mem_fun(&CplusplusGenerator::writeClass),this)); } void CplusplusGenerator::writeEnumMember(ConstField *cf, bool isLast){ writeTabs(1); mOutfile<getName()<<"="<getValue(); if (!isLast) mOutfile<<","; if (!cf->getHelp().empty()) mOutfile<<"\t/**< "<getHelp()<<" */"; mOutfile<getName()<<"/"<getName()<<".hh"; mOutfile.open(filename.str().c_str()); if (!mOutfile.is_open()){ cerr<<"Could not write into "< methods=klass->getMethods(); list constFields=klass->getConstFields(); mCurClass=klass; mOutfile<<"/* Wrapper generated by lp-gen-wrappers, do not edit*/"<"<getName().empty()) mOutfile<<"namespace "<getName()<<"{"<getType()==Type::Enum){ mOutfile<<"enum "<getName()<<"{"<::iterator cfit,next; for (cfit=constFields.begin();cfit!=constFields.end();){ ConstField *cf=*cfit; writeEnumMember(cf,++cfit==constFields.end()); } }else{ mOutfile<<"class "<getName()<<"{"<getName().empty()) mOutfile<<"} //end of namespace "<getName()<getType(); if (type->getBasicType()==Type::Class){ if (arg->isConst()){ mOutfile<<"const "; } mOutfile<getName(); if (arg->isPointer()) mOutfile<<"*"; }else if (type->getBasicType()==Type::Integer){ mOutfile<<"int"; }else if (type->getBasicType()==Type::Enum){ mOutfile<getName(); }else if (type->getBasicType()==Type::String){ if (!isReturn) mOutfile<<"const std::string &"; else mOutfile<<"std::string"; }else if (type->getBasicType()==Type::Void){ mOutfile<<"void"; }else if (type->getBasicType()==Type::Boolean){ mOutfile<<"bool"; } if (!isReturn && !arg->getName().empty()) mOutfile<<" "<getName(); } void CplusplusGenerator::writeTabs(int ntabs){ int i; for(i=0;i100 && comment[i]==' ')){ mOutfile<isCallback()) return; Argument *retarg=method->getReturnArg(); const list &args=method->getArgs(); list::const_iterator it; writeTabs(1); mOutfile<<"/**"<getHelp(),1); mOutfile<getName()<<"("; for(it=args.begin();it!=args.end();++it){ if (it!=args.begin()) mOutfile<<", "; writeArgument(*it); } mOutfile<<")"; if (method->isConst()) mOutfile<<"const"; mOutfile<<";"< classes=proj->getClasses(); mCurProj=proj; #ifndef WIN32 remove(to_lower(proj->getName()).c_str()); mkdir(to_lower(proj->getName()).c_str(),S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IROTH); #else _mkdir(to_lower(proj->getName()).c_str()); #endif ostringstream filename; /*write a file for the namespace*/ filename<getName())<<"/"<getName())<<".js"; mOutfile.open(filename.str().c_str()); if (!mOutfile.is_open()){ cerr<<"Could not write into "<getName()<getName()<<" = {};"<getName(); if (strncasecmp(enum_name.c_str(),mCurProj->getName().c_str(),mCurProj->getName().size())==0){ //since enum is part of the namespace, drop the namespace part of the enum if any. enum_name.erase(0,mCurProj->getName().size()); } return enum_name; } void JavascriptGenerator::writeEnum(Class *klass){ if (klass->getType()!=Type::Enum) return; ostringstream filename; list members=klass->getConstFields(); list::iterator it; string enum_name=getEnumName(klass); filename<getName())<<"/"<getName()<<" = "<getName()<<" || {};"<getHelp(),0); mOutfile<getName()<<"."<getHelp().empty()){ writeTabs(1); mOutfile<<"/**"<getHelp(),1); mOutfile<getName().substr(prefix_size,string::npos)<<" : "<getValue(); if (++it!=members.end()) mOutfile<<","; mOutfile<getName() << ".get" << enum_name << "Text = function(value) {" << endl; mOutfile << "\tswitch (value) {" << endl; for (it = members.begin(); it != members.end(); it++) { ConstField *cf = *it; mOutfile << "\tcase " << mCurProj->getName() << "." << enum_name << "." << cf->getName().substr(prefix_size, string::npos) << ":" << endl; mOutfile << "\t\treturn \"" << cf->getName().substr(prefix_size, string::npos) << "\";" << endl; } mOutfile << "\tdefault:" << endl; mOutfile << "\t\treturn \"?\";" << endl; mOutfile << "\t}" << endl; mOutfile << "};" << endl; mOutfile.close(); } void JavascriptGenerator::writeClass(Class *klass){ ostringstream filename; if (klass->getType()==Type::Enum) { return; } const list &methods=klass->getMethods(); if (methods.empty()) return;//skip empty classes filename<getName())<<"/"<getName())<<".js"; mOutfile.open(filename.str().c_str()); if (!mOutfile.is_open()){ cerr<<"Could not write into "<getName().empty()) // mOutfile<<"namespace "<getName()<<"{"<getHelp()<getName()< properties=klass->getProperties(); for_each(properties.begin(),properties.end(),bind1st(mem_fun(&JavascriptGenerator::writeProperty),this)); mOutfile<getName().empty()) // mOutfile<<"} //end of namespace "<getName()<getBasicType()){ case Type::Float: case Type::Integer: mOutfile<<"number"; break; case Type::String: mOutfile<<"string"; break; case Type::Boolean: mOutfile<<"boolean"; break; case Type::Class: mOutfile<<"external:"<getName(); break; case Type::Enum: mOutfile<getName()<<"."<getClass(type->getName())); break; case Type::Void: mOutfile<<"void"; break; case Type::Callback: break; case Type::Array: mOutfile<<"Array."; break; } } void JavascriptGenerator::writeArgument(Argument *arg, ArgKind kind){ switch(kind){ case Normal: mOutfile<<" * @param {"; writeType(arg->getType()); mOutfile<<"} "<getName()<<" - "<getHelp()<getType()); mOutfile<<"} "<getHelp()<getType()); mOutfile<<"} "<getName()<<" - "<getHelp()<100 && comment[i]==' ')){ mOutfile<getName()=="userData" || prop->getName()=="userPointer") return; mOutfile<<"/**"<getHelp(),0); mOutfile<getType()); mOutfile<<"} external:"<getName()<<"#"<getName()<getAttribute()==Property::ReadOnly) mOutfile<<" * @readonly"<getReturnArg(); const list &args=method->getArgs(); list::const_iterator it; if (method->isCallback()) return; if (method->getPropertyBehaviour()!=Method::None) return; if (method->getName()=="ref" || method->getName()=="unref") return; mOutfile<<"/**"<getHelp(),0); mOutfile<getName()<<"#"<getName()< &args=event->getArgs(); list::const_iterator it; if (!event->isCallback()) return; mOutfile<<"/**"<getHelp()),0); mOutfile<getName()<<"#"<getName()<