Exemple #1
0
//功能:得到一个函数中的从指定位置查找到的一个switch语句语句的所有相关命令到数组中
//参数:begin是从函数的第几行开始查找,switchLen是switch语句的长度
//返回值:返回这个switch结尾下一行减1
int getOneSwitch(string func[], int flen, int begin, string switchCmd[], int & switchLen)
{
	int i=0;
	int j=0;
	int k=0;
	switchLen = 0;
	string single[100] = {""};
	string temp = "";
	int slen = 0;
	int bracket = 0;  // 大括号匹配检测

	for(i=begin; i<flen; i++)
	{
		temp = func[i];
		slen = covertCmd(temp, single);

		if("switch" == single[0] && ";" != single[slen-1]) // 判断是否是switch语句的开始
		{
			// switch语句的开始,操作完成后就要break出语句
			bracket = 0;  //大括号匹配值初始为0
			j = 0;
			for(k=i; k<flen; k++)
			{
				switchCmd[j] = func[k];   // 先将该行赋给switchCmd[j]
				switchLen = j+1;
				j++;
				temp = func[k];
				slen = covertCmd(temp, single);

				if(k == i && "}" == single[slen-1])
				{
					return (k+1);
				}
				else
				{
					getBracket(single, slen, bracket); // 得到大括号匹配情况

					if(k!=i && 0 == bracket)   // 大括号匹配完成,说明switch语句结束
					{
						return (k+1);
					}
					else // 没有匹配结束,说明switch语句还没有结束
					{
						continue;
					}
				}
			}
		}
	}

	return -1;
}
        DirectionTypePtr Direction::createDirectionType( std::ostream& message, xml::XElementIterator& subIter, xml::XElementIterator& subIterEnd, bool& isSuccess )
        {
            auto directionType = makeDirectionType();
            
            if( subIter == subIterEnd )
            {
                message << "Direction: well thats weird - should not get here" << std::endl;
                isSuccess = false;
                return directionType;
            }
            
            if( subIter->getName() == "wedge" )
            {
                directionType->setChoice( DirectionType::Choice::wedge );
                isSuccess &= directionType->getWedge()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "dashes" )
            {
                directionType->setChoice( DirectionType::Choice::dashes );
                isSuccess &= directionType->getDashes()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "bracket" )
            {
                directionType->setChoice( DirectionType::Choice::bracket );
                isSuccess &= directionType->getBracket()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "pedal" )
            {
                directionType->setChoice( DirectionType::Choice::pedal );
                isSuccess &= directionType->getPedal()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "metronome" )
            {
                directionType->setChoice( DirectionType::Choice::metronome );
                isSuccess &= directionType->getMetronome()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "octave-shift" )
            {
                directionType->setChoice( DirectionType::Choice::octaveShift );
                isSuccess &= directionType->getOctaveShift()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "harp-pedals" )
            {
                directionType->setChoice( DirectionType::Choice::harpPedals );
                isSuccess &= directionType->getHarpPedals()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "damp" )
            {
                directionType->setChoice( DirectionType::Choice::damp );
                isSuccess &= directionType->getDamp()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "damp-all" )
            {
                directionType->setChoice( DirectionType::Choice::dampAll );
                isSuccess &= directionType->getDampAll()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "eyeglasses" )
            {
                directionType->setChoice( DirectionType::Choice::eyeglasses );
                isSuccess &= directionType->getEyeglasses()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "string-mute" )
            {
                directionType->setChoice( DirectionType::Choice::stringMute );
                isSuccess &= directionType->getStringMute()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "scordatura" )
            {
                directionType->setChoice( DirectionType::Choice::scordatura );
                isSuccess &= directionType->getScordatura()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "image" )
            {
                directionType->setChoice( DirectionType::Choice::image );
                isSuccess &= directionType->getImage()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "principal-voice" )
            {
                directionType->setChoice( DirectionType::Choice::principalVoice );
                isSuccess &= directionType->getPrincipalVoice()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "accordion-registration" )
            {
                directionType->setChoice( DirectionType::Choice::accordionRegistration );
                isSuccess &= directionType->getAccordionRegistration()->fromXElement( message, *subIter );
                return directionType;
            }
            
            if( subIter->getName() == "other-direction" )
            {
                directionType->setChoice( DirectionType::Choice::otherDirection );
                isSuccess &= directionType->getOtherDirection()->fromXElement( message, *subIter );
                return directionType;
            }
                
            std::string name = "rehearsal";
            if( subIter->getName() == name )
            {
                directionType->setChoice( DirectionType::Choice::rehearsal );
                bool isFirstSubItemAdded = false;
                
                while( subIter != subIterEnd )
                {
                    if( subIter->getName() != name )
                    {
                        message << "Direction: createDirectionType encountered an unexpected element '" << subIter->getName() << "' while parsing a collection of '" << name << "' elements" << std::endl;
                        isSuccess = false;
                        return directionType;
                    }
                    auto itemToAdd = makeRehearsal();
                    isSuccess &= itemToAdd->fromXElement( message, *subIter );
                    if( !isFirstSubItemAdded && directionType->getRehearsalSet().size() == 1 )
                    {
                        directionType->addRehearsal( itemToAdd );
                        directionType->removeRehearsal( directionType->getRehearsalSet().cbegin() );
                    }
                    else
                    {
                        directionType->addRehearsal( itemToAdd );
                    }
                    isFirstSubItemAdded = true;
                    ++subIter;
                } // end loop
                return directionType;
            } // end rehearsal
            
            
            name = "segno";
            if( subIter->getName() == name )
            {
                directionType->setChoice( DirectionType::Choice::segno );
                bool isFirstSubItemAdded = false;
                
                while( subIter != subIterEnd )
                {
                    if( subIter->getName() != name )
                    {
                        message << "Direction: createDirectionType encountered an unexpected element '" << subIter->getName() << "' while parsing a collection of '" << name << "' elements" << std::endl;
                        isSuccess = false;
                        return directionType;
                    }
                    auto itemToAdd = makeSegno();
                    isSuccess &= itemToAdd->fromXElement( message, *subIter );
                    if( !isFirstSubItemAdded && directionType->getSegnoSet().size() == 1 )
                    {
                        directionType->addSegno( itemToAdd );
                        directionType->removeSegno( directionType->getSegnoSet().cbegin() );
                    }
                    else
                    {
                        directionType->addSegno( itemToAdd );
                    }
                    isFirstSubItemAdded = true;
                    ++subIter;
                } // end loop
                return directionType;
            } // end segno
            
            
            name = "words";
            if( subIter->getName() == name )
            {
                directionType->setChoice( DirectionType::Choice::words );
                bool isFirstSubItemAdded = false;
                
                while( subIter != subIterEnd )
                {
                    if( subIter->getName() != name )
                    {
                        message << "Direction: createDirectionType encountered an unexpected element '" << subIter->getName() << "' while parsing a collection of '" << name << "' elements" << std::endl;
                        isSuccess = false;
                        return directionType;
                    }
                    auto itemToAdd = makeWords();
                    isSuccess &= itemToAdd->fromXElement( message, *subIter );
                    if( !isFirstSubItemAdded && directionType->getWordsSet().size() == 1 )
                    {
                        directionType->addWords( itemToAdd );
                        directionType->removeWords( directionType->getWordsSet().cbegin() );
                    }
                    else
                    {
                        directionType->addWords( itemToAdd );
                    }
                    isFirstSubItemAdded = true;
                    ++subIter;
                } // end loop
                return directionType;
            } // end words
            
            
            name = "coda";
            if( subIter->getName() == name )
            {
                directionType->setChoice( DirectionType::Choice::coda );
                bool isFirstSubItemAdded = false;
                
                while( subIter != subIterEnd )
                {
                    if( subIter->getName() != name )
                    {
                        message << "Direction: createDirectionType encountered an unexpected element '" << subIter->getName() << "' while parsing a collection of '" << name << "' elements" << std::endl;
                        isSuccess = false;
                        return directionType;
                    }
                    auto itemToAdd = makeCoda();
                    isSuccess &= itemToAdd->fromXElement( message, *subIter );
                    if( !isFirstSubItemAdded && directionType->getCodaSet().size() == 1 )
                    {
                        directionType->addCoda( itemToAdd );
                        directionType->removeCoda( directionType->getCodaSet().cbegin() );
                    }
                    else
                    {
                        directionType->addCoda( itemToAdd );
                    }
                    isFirstSubItemAdded = true;
                    ++subIter;
                } // end loop
                return directionType;
            } // end coda
            
            
            name = "dynamics";
            if( subIter->getName() == name )
            {
                directionType->setChoice( DirectionType::Choice::dynamics );
                bool isFirstSubItemAdded = false;
                
                while( subIter != subIterEnd )
                {
                    if( subIter->getName() != name )
                    {
                        message << "Direction: createDirectionType encountered an unexpected element '" << subIter->getName() << "' while parsing a collection of '" << name << "' elements" << std::endl;
                        isSuccess = false;
                        return directionType;
                    }
                    auto itemToAdd = makeDynamics();
                    isSuccess &= itemToAdd->fromXElement( message, *subIter );
                    if( !isFirstSubItemAdded && directionType->getDynamicsSet().size() == 1 )
                    {
                        directionType->addDynamics( itemToAdd );
                        directionType->removeDynamics( directionType->getDynamicsSet().cbegin() );
                    }
                    else
                    {
                        directionType->addDynamics( itemToAdd );
                    }
                    isFirstSubItemAdded = true;
                    ++subIter;
                } // end loop
                return directionType;
            } // end dynamics
            
            
            name = "percussion";
            if( subIter->getName() == name )
            {
                directionType->setChoice( DirectionType::Choice::percussion );
                bool isFirstSubItemAdded = false;
                
                while( subIter != subIterEnd )
                {
                    if( subIter->getName() != name )
                    {
                        message << "Direction: createDirectionType encountered an unexpected element '" << subIter->getName() << "' while parsing a collection of '" << name << "' elements" << std::endl;
                        isSuccess = false;
                        return directionType;
                    }
                    auto itemToAdd = makePercussion();
                    isSuccess &= itemToAdd->fromXElement( message, *subIter );
                    if( !isFirstSubItemAdded && directionType->getPercussionSet().size() == 1 )
                    {
                        directionType->addPercussion( itemToAdd );
                        directionType->removePercussion( directionType->getPercussionSet().cbegin() );
                    }
                    else
                    {
                        directionType->addPercussion( itemToAdd );
                    }
                    isFirstSubItemAdded = true;
                    ++subIter;
                } // end loop
                return directionType;
            } // end percussion

            return directionType;
        }