Click here to Skip to main content
15,881,173 members
Articles / Programming Languages / C#

XDD-Tools for developer

Rate me:
Please Sign up or sign in to vote.
5.00/5 (9 votes)
24 Jun 2012CPOL2 min read 22.4K   449   14  
XDD-Tools for developer , count source code line,copy project file, remove comment in source code.
/*
	
	C# ����Դ���������
	
	���� Ԭ���� 2007-5-13
	
*/
using System;
using System.IO;
using System.Text;

namespace XDesigner.Development.Dom
{
	/// <summary>
	/// ����CS �����ļ��Ķ���
	/// </summary>
	/// <remarks>
	/// �����������ڷ���C#�����ļ���ͳ�Ƴ�Դ�����һЩ��Ϣ
	/// </remarks>
	public class CSFileAnalyser : FileAnalyser
	{
		public override bool CheckFileExtension(string strExtension)
		{
			return strExtension == "CS" ;
		}

		protected override void InnerAnalyse()
		{
			//string a = @"aaa/* */aaaaa";
			// �ַ��������־ 0:δ�����ַ��� 1:���嵥���ַ��� 2:��������ַ���
			int DefineString = 0 ;
			// ע�Ͷ����־
			bool DefineComment = false ;
			foreach( LineInfo info in myLines )
			{
				info.CodeFlag = false;
				if( info.LineText.Length == 0 && DefineString == 2)
					info.BlankLine = false;
				for(int iCount = 0 ; iCount < info.LineText.Length ; iCount ++)
				{
					// ��ǰ�ַ�
					char c = info.LineText[iCount] ;
					// ��һ���ַ�
					char c2 = (char)0;
					if( iCount < info.LineText.Length - 1 )
						c2 = info.LineText[ iCount + 1 ];

					if(! char.IsWhiteSpace( c ))
						info.BlankLine = false;

					// �����ڶ���ע��
					if( DefineComment )
					{
						info.BlankLine = false;
						info.CommentFlag = true;
						// ���� */ ����ע��
						if( c == '*' &&  c2 == '/' )
						{
							DefineComment = false;
							iCount ++ ;
						}
						continue ;
					}
					if( DefineString == 0 )
					{
						// ��δ��ʼ�����ַ���
						if( c == '/' && c2 != 0 )
						{
							if( c2 == '/' )
							{
								// ���� // ��ʼ����ע��
								info.CommentFlag = true;
								DefineComment = false;
								goto NextLine ;
							}
							if( c2 == '*' )
							{
								// ���� /* ��ʼ����ע��
								if( iCount > 0 )
									info.CodeFlag = true;
								info.CommentFlag = true;
								DefineComment = true;
								iCount ++ ;
								continue;
								//goto NextLine ;
							}
						}
						if( c == '\"')
						{
							if( iCount > 0 && info.LineText[iCount-1] == '@')
								// ���� @" ��ʼ��������ַ���
								DefineString = 2 ;
							else
								// ���� " ��ʼ���嵥���ַ���
								DefineString = 1 ;
						}
					}
					else
					{
						info.BlankLine = false;
						// ���ڶ����ַ���
						if( c == '\"'  )
						{
							// ��������ַ��������� " ����������ַ���
							if( iCount > 0 && info.LineText[iCount-1] !='\\' )
								DefineString = 0 ;
						}
					}
					if( ! char.IsWhiteSpace( c ))
						info.CodeFlag = true;
				}//for(int iCount = 0 ; iCount < info.LineText.Length ; iCount ++)
			NextLine:;
			}//foreach( LineInfo info in myLines )
		}//protected override void InnerAnalyse()


        public override bool HandleComment(string fileName, bool removeComment, string headerComment)
        {
            string text = null;
            //if (fileName.IndexOf("ScriptAssemblyBuffer.cs")>= 0 )
            //{
            //    System.Console.WriteLine("");
            //}
            using (StreamReader reader = new StreamReader(
                fileName,
                Encoding.GetEncoding(936),
                true))
            {
                text = reader.ReadToEnd();
                if (text.Length > 0 && text[0] == 65279)
                {
                    text = text.Substring(1);
                }
            }
            if (string.IsNullOrEmpty(text) == false)
            {
                string newText = HandleSourceComment(text, removeComment, headerComment, true);
                if (text != newText)
                {
                    using(StreamWriter mywriter = new StreamWriter(
                        fileName, 
                        false, 
                        Encoding.GetEncoding(936)))
                    {
                        mywriter.Write(newText);
                    }
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// ����Դ�����е�ע��
        /// </summary>
        /// <param name="sourceCode">�ɴ���</param>
        /// <param name="removeComment">�Ƿ�ɾ��ע��</param>
        /// <param name="headerComment">�Ƿ���ӿ�ͷ��ע��</param>
        /// <param name="allowMultiStringDefine">�������Ƿ������������ַ���ֵ��Ŀǰֻ��C#�ܶ�������ַ���ֵ��</param>
        /// <returns>������Դ����</returns>
        public static string HandleSourceComment(
            string sourceCode,
            bool removeComment,
            string headerComment,
            bool allowMultiStringDefine )
        {
            StringBuilder result = new StringBuilder();
            if (headerComment != null && headerComment.Length > 0)
            {
                result.AppendLine("/*");
                result.AppendLine(headerComment);
                result.AppendLine("*/");
            }
            else
            {
            }

            CSCodeState state = CSCodeState.Normal;

            int txtLength = sourceCode.Length;
            bool currentLineHasCode = false;
            int lastLineIndex = result.Length ;
            bool hasComment = false;
            for (int iCount = 0; iCount < txtLength; iCount++)
            {
                char c = sourceCode[iCount];
                if (c == '\"')
                {
                    if (state == CSCodeState.Normal)
                    {
                        if ( allowMultiStringDefine 
                            && iCount > 0
                            && sourceCode[iCount - 1] == '@')
                        {
                            state = CSCodeState.MultiString;
                        }
                        else
                        {
                            state = CSCodeState.SingleString;
                        }
                    }
                    else if (state == CSCodeState.SingleString)
                    {
                        // ���й��˵�ת������
                        state = CSCodeState.Normal;
                        if (iCount > 2)
                        {
                            if (sourceCode[iCount - 1] == '\\'
                                && sourceCode[iCount - 2] != '\\')
                            {
                                state = CSCodeState.SingleString;
                            }
                        }
                    }
                    else if ( state == CSCodeState.MultiString)
                    {
                        state = CSCodeState.Normal;
                    }
                }
                else if (c == '\r' || c == '\n')
                {
                    if (state == CSCodeState.Comment)
                    {
                        state = CSCodeState.Normal;
                    }
                    else if (state == CSCodeState.SingleString)
                    {
                        state = CSCodeState.Normal;
                    }
                }
                else if (c == '/')
                {
                    if (state == CSCodeState.Normal)
                    {
                        if (iCount < sourceCode.Length - 2)
                        {
                            char nextC = sourceCode[iCount + 1];
                            if (nextC == '/')
                            {
                                state = CSCodeState.Comment;
                                iCount++;
                            }
                            else if (nextC == '*')
                            {
                                state = CSCodeState.MultiComment;
                                iCount++;
                            }
                        }
                    }
                }
                else if (c == '*')
                {
                    if (state == CSCodeState.MultiComment)
                    {
                        if (iCount < sourceCode.Length - 2)
                        {
                            char nextC = sourceCode[iCount + 1];
                            if (nextC == '/')
                            {
                                state = CSCodeState.Normal;
                                iCount++;
                                continue;
                            }
                        }
                    }
                }
                if (state == CSCodeState.Comment || state == CSCodeState.MultiComment)
                {
                    hasComment = true;
                }
                if (state != CSCodeState.Comment && state != CSCodeState.MultiComment)
                {
                    if (char.IsWhiteSpace(c) == false 
                        || state == CSCodeState.SingleString
                        || state == CSCodeState.MultiString )
                    {
                        currentLineHasCode = true;
                    }
                    if (c == '\r' )
                    {
                        if (currentLineHasCode == false
                            && hasComment == true )
                        {
                            // last line full of comment and whitespace , remove it.
                            result.Remove(lastLineIndex, result.Length - lastLineIndex);
                        }
                        lastLineIndex = result.Length ;
                        hasComment = false;
                        currentLineHasCode = false;
                    }
                    result.Append(c);
                }

            }//for

            return result.ToString();
        }
    

        private enum CSCodeState
        {
            Normal ,
            SingleString ,
            MultiString ,
            Comment ,
            MultiComment
        }


	}//public class CSFileAnalyser : FileAnalyser
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer duchang soft
China China
yuan yong fu of duchang soft , come from CHINA , 2008 Microsoft MVP,Use GDI+,XML/XSLT, site:http://www.cnblogs.com/xdesigner/

Comments and Discussions