Regular Expression Code Conversion Program for program porting between different cocos2d-x versions.

Source: Internet
Author: User

If the development environment does not have the reflection function, it takes a lot of effort to rename the code. The following code replaces variable names in batches. Modify many situations that can be used for code conversion.

This article is used for program porting between different cocos2d-x versions.

Main functions of the Code: exclude comments and strings, and replace other content with the variable name in the dictionary. Click Open Link

#!/usr/bin/env pythonimport osimport reimport sysimport codecs#############################dic={}jiacc=["spriteWithFile","actionWithDuration","menuWithItem","itemWithLabel","transitionWithDuration","spriteWithFile","labelWithString","node"]#,"Object","Sprite","Point","Scene","Node","MoveTo","Director","Application","Size","Touch","Event"]dic["CCMutableArray"]="CCArray"dic["CGFloat"]="float"dic["ccTime"]="float"for one in jiacc:    dic[one]="create"##############################pattern = re.compile(r"""   (//[^\r\n]*) # match a single line comment                            | (/\*.*?\*/)      # match a multi line comment                            | ("[^"]*")        # match a string literal                            | ([a-zA-Z_][a-zA-Z_0-9]*) #identifier                        """                           , re.X | re.S)def translateValue(old):    new=dic.get(old)    if new!=None:        return new    return olddef func(match):    if match.group(1) or match.group(2) or match.group(3):        return match.group()    if match.group(4):        return  translateValue(match.group())#        #raw_input()    else:      return match.group()def translateStr(source):  return re.sub(pattern,func,source)def translateFile(inputFileName):  s=codecs.open(inputFileName,"r","utf-8").read()  return translateStr(s)def treatfile(inputFileName):  print inputFileName  #bak  cmd="cp %s %s" %(inputFileName,inputFileName+".bak")  os.system(cmd)  #translate  fc=translateFile(inputFileName)  #output  f=codecs.open(inputFileName,"w","utf-8")  f.write(fc)  f.close()#dir##################def mylistdir(p,f):    a=os.listdir(p)    fs=myfind(a,f)    return(fs)def myfind(l,p):    lr=[];    #print p    p1=p.replace(".",r"\.")    p2=p1.replace("*",".*")    p2=p2+"$"    for a in l:        #print a        if  re.search(p2,a,re.IGNORECASE)==None :           pass           #print "pass"        else:           lr.append(a)       #print "append"    return lrdef translateDir(path):#    files=mylistdir(path,"*.cpp")    files2=mylistdir(path,"*.h")    for f in files2:        files.append(f)    for f in  files:        fn=path+"/"+f        treatfile(fn)if __name__=="__main__":    translateDir(sys.argv[1])




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.