• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            冰果

            技術群:26678700     
            交流QQ: 704839634
            合作: 1) 可兼職遠程辦公開發; 2) 有一套Go+Python開發的行業短信云平臺可合作;3)目前正在開發物聯網、大數據平臺。

            腳本:根據模板生成簡單代碼

                     在UNIX系統上進行C++開發,公司規定用vi編輯,所以寫個簡單腳本,可以根據簡單模板簡單生成C++代碼文件,  可以偷一下懶,文件頭也比較規范。里面大量使用awk和sed,這是寫sh腳本常用的東東。
                      第一部分是代碼,第二部分是我自己常用的簡單模板。
             
            =====================================================================
                                                            腳本
            =====================================================================
            #!/bin/sh
            #
            ################################################################################
            #
            # Auto-create the code file from the template file.
            #
            # Author:  liangbing
            # version:  1.0
            # date:  2007-05-19
            #
            ################################################################################


            ################################################################################
            #
            # function: print the help
            #
            ################################################################################
            usage( )
            {
             echo "act.sh [-v] [-h] "
             echo "  Print the help"
             echo "act.sh [-t templFileName] [-f codeFileName] [-c classNameList]"
             echo "     Create the code file by the template file(通過模板生成代碼, 完成后自動進入vi編輯)"
             echo "     classNameList is separated by \":\", 即多個類名之間用冒號分隔"
             echo "     default template path(如果缺省路徑不對,請自己修改腳本act.sh中變量templDefaultPath): "
             echo "  $templDefaultPath"
             echo " .e.g:"
             echo "   1. default template path: 使用缺省的模板文件的路徑"
              echo "     AIMC code:"
             echo "  act.sh -t apisvr -f test.cpp"
             echo "  act.sh -t h2sg -f test.cpp"
             echo "  act.sh -t webmail -f test.cpp"
             echo "  act.sh -t wmail -f test.cpp"
             echo ""
              echo "     common code:"
             echo "  act.sh -t main -f test.cpp"
             echo "  act.sh -t head -f test.hpp"
             echo "  act.sh -t src -f test.cpp"
             echo "  act.sh -c clsString -f test"
             echo "  act.sh -c clsString:clsVector:clsList -f test"
             echo ""
             echo "   2. specified template path: 自己指定模板文件的路徑和文件名"
             echo "  act.sh -t ./yourTempl.cpp -f test.cpp"
             echo "  act.sh -t ./yourClassTempl -c clsString -f test"
             echo "  act.sh -t ./yourClassTempl -c clsString:clsVector:clsList -f test"
             echo ""
             echo "   3. append a class into your existed hpp and cpp file: 追加一個新類到已經存在的頭文件和源文件"
             echo "  act.sh -c clsString -f test"
             echo "  act.sh -c clsString:clsVector:clsList -f test"
            }


            ################################################################################
            #
            # function: create the simple file
            #
            ################################################################################
            creat_simple_file( )
            {
            # the definition of the variabl
             templFileName=$1
             codeFileName=$2
             templDefaultPath=$3

            # the definiton of the key value
             keyFileName=
             moduleName=
             version=1.0
             date=`date "+%Y-%m-%d"`
             dateTime=`date "+%Y\/%m\/%d %H:%M:%S"`

            # test if the template file is existed
             if [ ! -f "$templFileName" ]
             then
              templFileName=`echo ${templDefaultPath}/${templFileName}.act`
              if [ ! -f "$templFileName" ]
              then
               echo "$templFileName is not existed!"
               exit
              fi
             fi

            # test if the code file is existed
             if [ -f "$codeFileName" ]
             then
              vi $codeFileName
              exit
             fi

            # get the path name, the key file name and the module name
             keyFileName=`echo $codeFileName | awk -F/ '{print $NF}'`
             moduleName=`echo $keyFileName | awk -F. '{print $1}'`

            # get the file identifier
             dateStr=`date "+%Y%m%d%H%M%S"`
             fileIdentifier=`echo $moduleName | tr "[a-z]" "[A-Z]"`
             fileIdentifier=`echo ${fileIdentifier}_${dateStr}`

            # create the code file
             sed "s/<%file_name%>/$keyFileName/g" $templFileName | \
              sed "s/<%version%>/$version/g" | \
              sed "s/<%login_name%>/$LOGNAME/g" | \
              sed "s/<%date%>/$date/g" | \
              sed "s/<%module_name%>/$moduleName/g" | \
              sed "s/<%file_identifier%>/$fileIdentifier/g" | \
              sed "s/<%date_time%>/$dateTime/g" > $codeFileName

             vi $codeFileName
            }


            ################################################################################
            #
            # function: append the class code to the existed hpp and cpp file
            #
            ################################################################################
            append_class_code( )
            {
            # the definition of the variabl
             templHeadFileName=`echo ${templDefaultPath}/class.head`
             templSrcFileName=`echo ${templDefaultPath}/class.src`

            # test if the template file is existed
             if [ ! -f "$templSrcFileName" ]
             then
              echo "$templSrcFileName is not existed!"
              exit
             elif [ ! -f "$templHeadFileName" ]
             then
              echo "$templHeadFileName is not existed!"
              exit
             fi

            # append the class code
             tempHeadFile=`echo ".temp.head.act"`
             tempSrcFile=`echo ".temp.src.act"`

             lineNumList=`sed -n "/#endif/=" $codeHeadFileName`
             lineNum=`echo $lineNumList | awk '{print $NF}'`

             sed "${lineNum}d" $codeHeadFileName > $tempHeadFile
             awk '{print $0}' $codeSourceFileName > $tempSrcFile

             for className in $classNameList
             do
              sed "s/<%class_name%>/$className/g" $templHeadFileName >> $tempHeadFile
              sed "s/<%class_name%>/$className/g" $templSrcFileName >> $tempSrcFile
             done

             sed -n "$lineNum"p $codeHeadFileName >> $tempHeadFile
             awk '{print $0}' $tempHeadFile > $codeHeadFileName
             awk '{print $0}' $tempSrcFile > $codeSourceFileName

            # delete the temp file
             if [ -f "$tempHeadFile" ]
             then
              rm $tempHeadFile
             fi

             if [ -f "$tempSrcFile" ]
             then
              rm $tempSrcFile
             fi
            }


            ################################################################################
            #
            # function: create the class file
            #
            ################################################################################
            creat_class_file( )
            {
            # the definition of the variabl
             templFileName=$1
             classNameList=`echo $2 | sed "s/:/ /g"`
             codeFileName=$3
             templDefaultPath=$4

            # the definiton of the key value
             version=1.0
             date=`date "+%Y-%m-%d"`
             dateTime=`date "+%Y\/%m\/%d %H:%M:%S"`

            # get template info: the path name, the file name
             count=`echo $templFileName | awk -F/ '{print NF}'`
             tmpFileName=`echo $templFileName | awk -F/ '{print $NF}'`
             templPathName=.
             if [ $count -ge 2 ]
             then
              count=`expr $count - 1`
              templPathName=`echo $templFileName | cut -d/ -f1-$count`
             fi
             templName=`echo $tmpFileName | awk -F. '{print $1}'`
             templHeadFileName=`echo ${templPathName}/${templName}.hpp`
             templSourceFileName=`echo ${templPathName}/${templName}.cpp`

            # test if the template file is existed
             if [ ! -f "$templHeadFileName" ]
             then
              templHeadFileName=`echo ${templDefaultPath}/${templName}.hpp`
              if [ ! -f "$templHeadFileName" ]
              then
               echo "$templHeadFileName is not existed!"
               exit
              fi
             fi

             if [ ! -f "$templSourceFileName" ]
             then
              templSourceFileName=`echo ${templDefaultPath}/${templName}.cpp`
              if [ ! -f "$templSourceFileName" ]
              then
               echo "$templSourceFileName is not existed!"
               exit
              fi
             fi

            # get code file info: the path name, the file name
             count=`echo $codeFileName | awk -F/ '{print NF}'`
             tmpFileName=`echo $codeFileName | awk -F/ '{print $NF}'`
             codePathName=.
             if [ $count -ge 2 ]
             then
              count=`expr $count - 1`
              codePathName=`echo $codeFileName | cut -d/ -f1-$count`
             fi
             moduleName=`echo $tmpFileName | awk -F. '{print $1}'`
             codeHeadFileName=`echo ${codePathName}/${moduleName}.hpp`
             codeSourceFileName=`echo ${codePathName}/${moduleName}.cpp`
             keyHeadFileName=`echo ${moduleName}.hpp`
             keySourceFileName=`echo ${moduleName}.cpp`

            # get the file identifier
             dateStr=`date "+%Y%m%d%H%M%S"`
             fileIdentifier=`echo $moduleName | tr "[a-z]" "[A-Z]"`
             fileIdentifier=`echo ${fileIdentifier}_${dateStr}`

            # test if the code file is existed
             if [ -f "$codeHeadFileName" -a -f "$codeSourceFileName" ]
             then
              append_class_code
             fi

             if [ -f "$codeHeadFileName" ]
             then
              vi $codeHeadFileName
              exit
             elif [ -f "$codeSourceFileName" ]
             then
              vi $codeSourceFileName
              exit
             fi

            # create the class header file
             beginLineNum=`sed -n "/<%class_begin%>/=" $templHeadFileName`
             endLineNum=`sed -n "/<%class_end%>/=" $templHeadFileName`

             headLineNum=`expr $beginLineNum - 1`
             sed -n "1,$headLineNum"p $templHeadFileName | \
              sed "s/<%file_name%>/$keyHeadFileName/g" | \
              sed "s/<%version%>/$version/g" | \
              sed "s/<%login_name%>/$LOGNAME/g" | \
              sed "s/<%date%>/$date/g" | \
              sed "s/<%module_name%>/$moduleName/g" | \
              sed "s/<%file_identifier%>/$fileIdentifier/g" | \
              sed "s/<%date_time%>/$dateTime/g" > $codeHeadFileName

             for className in $classNameList
             do
              classBeginLineNum=`expr $beginLineNum + 1`
              classEndLineNum=`expr $endLineNum - 1`
              sed -n "$classBeginLineNum,$classEndLineNum"p $templHeadFileName | \
               sed "s/<%class_name%>/$className/g" >> $codeHeadFileName
             done

             rearLineNum=`expr $endLineNum + 1`
             sed -n "$rearLineNum,$"p $templHeadFileName | \
              sed "s/<%file_identifier%>/$fileIdentifier/g" >> $codeHeadFileName

            # create the class source file
             beginLineNum=`sed -n "/<%class_begin%>/=" $templSourceFileName`
             endLineNum=`sed -n "/<%class_end%>/=" $templSourceFileName`

             headLineNum=`expr $beginLineNum - 1`
             sed -n "1,$headLineNum"p $templSourceFileName | \
              sed "s/<%file_name%>/$keySourceFileName/g" | \
              sed "s/<%version%>/$version/g" | \
              sed "s/<%login_name%>/$LOGNAME/g" | \
              sed "s/<%date%>/$date/g" | \
              sed "s/<%module_name%>/$moduleName/g" | \
              sed "s/<%header_file_name%>/$keyHeadFileName/g" | \
              sed "s/<%date_time%>/$dateTime/g" > $codeSourceFileName

             for className in $classNameList
             do
              classBeginLineNum=`expr $beginLineNum + 1`
              classEndLineNum=`expr $endLineNum - 1`
              sed -n "$classBeginLineNum,$classEndLineNum"p $templSourceFileName | \
               sed "s/<%class_name%>/$className/g" >> $codeSourceFileName
             done

             vi $codeHeadFileName
            }


            ################################################################################
            #
            # The main module
            #
            ################################################################################
            # the definition of the variabl
            templDefaultPath=/export/home/liangb/aimc/autocreate
            templFileName=
            codeFileName=
            className=

            # get the code file name
            if [ $# -lt 1 ]
            then
             usage
             exit
            else
             while getopts hvf:t:c: option
             do
              case $option in
              h)
               usage
               exit 1 ;;
              v)
               usage
               exit 1 ;;
              f)
               codeFileName=$OPTARG ;;
              t)
               templFileName=$OPTARG ;;
              c)
               className=$OPTARG ;;
              ?)
               usage
               exit 1 ;;
              esac
             done
            fi

            # test if the code file name is set
            if [ -z "$codeFileName" ]
            then
             echo "You don't input the code file name"
             usage
             exit
            fi

            # test if the class template file name is set
            if [ -z "$className" ]
            then
             if [ -z "$templFileName" ]
             then
              echo "You don't input the template file name"
              usage
              exit
             else
              creat_simple_file $templFileName $codeFileName $templDefaultPath
              exit
             fi
            else
             if [ -z "$templFileName" ]
             then
              templFileName=`echo "class"`
             fi

             creat_class_file $templFileName $className $codeFileName $templDefaultPath
             exit
            fi

            =====================================================================
                                                            模板文件
            =====================================================================
            1.  class.hpp
             
            /**
             *@file  <%file_name%>
             *
             *@brief <%module_name%>.
             *
             *@author <%login_name%>
             *@date  <%date%>
             *@version <%version%>
             *$Id: <%file_name%>,v <%version%> <%date_time%> <%login_name%> Exp $
             */


            #ifndef __<%file_identifier%>_H__
            #define __<%file_identifier%>_H__

             


            <%class_begin%>
            //////////////////////////////////////////////////////////////////////////////////////////
            /**
             *@name  <%class_name%>
             *@brief 
             */
            class <%class_name%> {
             public:
              /**
               *@brief Default constructor
               */
              <%class_name%>( );

              /**
               *@brief Destructor
               */
              ~<%class_name%>( );

             protected:

             private:
              /**
               *@brief Copy constructor
               */
              <%class_name%>( const <%class_name%>& );

              /**
               *@brief Assignment operator
               */
              <%class_name%>& operator=( const <%class_name%>& );

             private:
            };


            <%class_end%>
            #endif//__<%file_identifier%>_H__

            -------------------------------------------------------------------------------------------------

            2. class.cpp

            /**
             *@file  <%file_name%>
             *
             *@brief <%module_name%>.
             *
             *@author <%login_name%>
             *@date  <%date%>
             *@version <%version%>
             *$Id: <%file_name%>,v <%version%> <%date_time%> <%login_name%> Exp $
             */


            #include "<%header_file_name%>"

             


            <%class_begin%>
            //////////////////////////////////////////////////////////////////////////////////////////
            //
            //    <%class_name%>
            //
            //////////////////////////////////////////////////////////////////////////////////////////
            // Default constructor
            <%class_name%>::<%class_name%>( )
            {
            }


            // Destructor
            <%class_name%>::~<%class_name%>( )
            {
            }


            <%class_end%>

            -------------------------------------------------------------------------------------
            3. class.head

            //////////////////////////////////////////////////////////////////////////////////////////
            /**
             *@name  <%class_name%>
             *@brief 
             */
            class <%class_name%> {
             public:
              /**
               *@brief Default constructor
               */
              <%class_name%>( );

              /**
               *@brief Destructor
               */
              ~<%class_name%>( );

             protected:

             private:
              /**
               *@brief Copy constructor
               */
              <%class_name%>( const <%class_name%>& );

              /**
               *@brief Assignment operator
               */
              <%class_name%>& operator=( const <%class_name%>& );

             private:
            };


            -------------------------------------------------------------------------------------
            4. class.src

            //////////////////////////////////////////////////////////////////////////////////////////
            //
            //    <%class_name%>
            //
            //////////////////////////////////////////////////////////////////////////////////////////
            // Default constructor
            <%class_name%>::<%class_name%>( )
            {
            }


            // Destructor
            <%class_name%>::~<%class_name%>( )
            {
            }


            -------------------------------------------------------------------------------------
            5. head.act

            /**
             *@file  <%file_name%>
             *
             *@brief <%module_name%>.
             *
             *@author <%login_name%>
             *@date  <%date%>
             *@version <%version%>
             *$Id: <%file_name%>,v <%version%> <%date_time%> <%login_name%> Exp $
             */


            #ifndef __<%file_identifier%>_H__
            #define __<%file_identifier%>_H__


            //---------------------------The external header------------------------------------------

             


            //---------------------------The macro definition-----------------------------------------

             


            //---------------------------The variable definition--------------------------------------

             


            //---------------------------The function definition--------------------------------------

             


            #endif//__<%file_identifier%>_H__

             

            -------------------------------------------------------------------------------------
            6. main.act

            /**
             *@file  <%file_name%>
             *
             *@brief <%module_name%>.
             *
             *@author <%login_name%>
             *@date  <%date%>
             *@version <%version%>
             *$Id: <%file_name%>,v <%version%> <%date_time%> <%login_name%> Exp $
             */


            #ifdef linux
            #include <getopt.h>
            #endif
            #include <stdlib.h>
            #include <stdio.h>
            #include <string.h>
            #include <strings.h>

             


            //---------------------------The macro definition-----------------------------------------
            #define MODULE_NAME    "<%module_name%>"

             


            //---------------------------The variable definition--------------------------------------

             


            //---------------------------The function prototype---------------------------------------

             


            //---------------------------The function definition--------------------------------------
            // The main function
            int main( int argc, char ** argv )
            {
             int liRet = 0;
             
             return liRet;
            }

            posted on 2010-12-19 00:17 冰果 閱讀(527) 評論(0)  編輯 收藏 引用

                                                        
            99精品国产在热久久| 久久久久久久免费视频| av无码久久久久久不卡网站| 国产精品久久国产精麻豆99网站| 久久久久久亚洲精品成人| 91久久成人免费| 久久久久青草线蕉综合超碰 | 一本色道久久99一综合| av无码久久久久久不卡网站| 久久成人18免费网站| 久久无码人妻一区二区三区| 国产精品永久久久久久久久久 | 久久综合给合综合久久| 精品久久久久久国产| 国产一区二区精品久久凹凸| 99久久精品免费看国产一区二区三区 | 一本久道久久综合狠狠爱| 嫩草影院久久国产精品| 亚洲精品tv久久久久久久久| 国产综合免费精品久久久| 久久国产精品无码一区二区三区| 久久精品国产亚洲7777| 久久久久亚洲av无码专区| 欧美精品国产综合久久| 久久精品国产精品亜洲毛片| 99国产欧美精品久久久蜜芽| 亚洲级αV无码毛片久久精品| 亚洲国产香蕉人人爽成AV片久久 | 久久久久亚洲国产| 久久精品中文字幕第23页| 久久免费小视频| 久久精品嫩草影院| 国产精品久久波多野结衣| 久久久久99精品成人片试看| 久久久精品国产免大香伊| 国内精品综合久久久40p| 麻豆久久久9性大片| 久久人人爽人人人人爽AV| 久久成人小视频| 久久综合给久久狠狠97色 | 最新久久免费视频|