冰果

技术群: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 冰果 阅读(516) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理