实例介绍
【实例简介】
软件著作权申请时,需要准备前30页后30页网页源代码。
通过这个软件,直接自动将注释和空行都删除,并且合并成差不多30多页的源代码。
【实例截图】
【核心代码】
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace SourceConvert
{
public partial class Form1 : Form
{
int linecount = 0;
List<string> lexts = new List<string>();
List<string> files = new List<string>();
List<string> txtlines = new List<string>();
public Form1()
{
InitializeComponent();
}
private void fileSort()
{
Random random = new Random();
List<string> newList = new List<string>();
foreach (string item in files)
{
newList.Insert(random.Next(newList.Count), item);
}
files = newList;
}
private void btn_submit_Click(object sender, EventArgs e)
{
if (linecount > 4000)
{
linkLabel1_LinkClicked(linkLabel1, null);
}
File.WriteAllText(Application.StartupPath "/SC_Path.txt", txt_path.Text, Encoding.UTF8);
string[] exts = txt_ext.Text.Split(';');
string tmpstr;
int ind, ind2;
lexts.Clear();
files.Clear();
foreach (string ext in exts)
{
tmpstr = ext.Trim();
if(tmpstr.StartsWith("*."))
lexts.Add(tmpstr.Substring(2).ToLower());
}
if (lexts.Count == 0)
{
MessageBox.Show("扩展名没有设置");
return;
}
string[] paths = txt_path.Lines;
btn_submit.Enabled = false;
try
{
foreach(string path in paths)
dirrun(path);
fileSort();
foreach (string file in files)
{
//rtb_source.AppendText(file Environment.NewLine Environment.NewLine Environment.NewLine Environment.NewLine Environment.NewLine);
ind = file.LastIndexOf('.');
if (ind == -1)
continue;
tmpstr = file.Substring(ind 1).ToLower();
if (lexts.Contains(tmpstr))
{
Encoding encode = TxtFileEncoding.getencode_percent(File.ReadAllBytes(file));
string[] lines = File.ReadAllLines(file, encode);
bool bst = false;
foreach (string line in lines)
{
string linen = line;
if (bst)//前面遇到了/*
{
ind2 = linen.IndexOf("*/");
if (ind2 == -1)
continue;
else
{
linen = linen.Substring(ind2 2);
bst = false;
}
}
else
{
ind = linen.IndexOf("/*");
if (ind > -1)
{
ind2 = linen.IndexOf("*/", ind);
if (ind2 > 0)
{
linen = linen.Remove(ind, ind2 - ind 2);
}
else
{
linen = linen.Remove(ind);
bst = true;
}
}
}
ind = linen.IndexOf("//");
if (ind > -1)
linen = linen.Remove(ind);
tmpstr = linen.Trim();
if (tmpstr == "")
continue;
txtlines.Add(linen);
linecount ;
}
}
Application.DoEvents();
if (linecount > 4000)
break;
}
if (txtlines.Count > 3000)
{
txtlines.RemoveRange(30 * 50, txtlines.Count - 60 * 50);
}
rtb_source.Lines = txtlines.ToArray();
}
catch (Exception ex)
{
MessageBox.Show("运行错误:" ex.Message);
return;
}
finally
{
btn_submit.Enabled = true;
}
int page = (int)Math.Ceiling(linecount * 1.0 / 50);
lab_msg.Text = linecount "行," page "页";
}
private void dirrun(string path)
{
if (path.Trim() == "")
return;
if(!Directory.Exists(path))
return;
files.AddRange(Directory.GetFiles(path));
string[] dirs = Directory.GetDirectories(path);
foreach (string dir in dirs)
{
dirrun(dir);
}
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
{
linecount = 0;
rtb_source.Clear();
txtlines.Clear();
lab_msg.Text = "";
}
private void Form1_Load(object sender, EventArgs e)
{
if (File.Exists(Application.StartupPath "/SC_Path.txt"))
txt_path.Text = File.ReadAllText(Application.StartupPath "/SC_Path.txt", Encoding.UTF8);
}
}
public class TxtFileEncoding
{
public static Encoding getencode_percent(byte[] lb)
{
try
{
if (is_utf8_code(lb))
return Encoding.UTF8;
if (is_gb2312_code(lb))
return Encoding.GetEncoding("gb2312");
if (is_big5_code(lb))
return Encoding.GetEncoding("big5");
if (is_gbk_code(lb))
return Encoding.GetEncoding("gbk");
}
catch (Exception ex)
{
}
return Encoding.ASCII;
}
public static bool is_utf8_special_byte(byte c)
{
byte special_byte = 0X02; //binary 00000010
if (c >> 6 == special_byte)
{
return true;
}
else
{
return false;
}
}
public static bool is_utf8_code(byte[] byts)
{
byte one_byte = 0X00; //binary 00000000
byte two_byte = 0X06; //binary 00000110
byte three_byte = 0X0E; //binary 00001110
byte four_byte = 0X1E; //binary 00011110
byte five_byte = 0X3E; //binary 00111110
byte six_byte = 0X7E; //binary 01111110
int utf8_yes = 0;
int utf8_no = 0;
byte k = 0;
byte m = 0;
byte n = 0;
byte p = 0;
byte q = 0;
byte c = 0;
for (int i = 0; i < byts.Length; )
{
c = byts[i];
if (c >> 7 == one_byte)
{
i ;
continue;
}
else if (c >> 5 == two_byte)
{
k = byts[i 1];
if (is_utf8_special_byte(k))
{
utf8_yes ;
i = 2;
continue;
}
}
else if (c >> 4 == three_byte)
{
m = byts[i 1];
n = byts[i 2];
if (is_utf8_special_byte(m)
&& is_utf8_special_byte(n))
{
utf8_yes ;
i = 3;
continue;
}
}
else if (c >> 3 == four_byte)
{
k = byts[i 1];
m = byts[i 2];
n = byts[i 3];
if (is_utf8_special_byte(k)
&& is_utf8_special_byte(m)
&& is_utf8_special_byte(n))
{
utf8_yes ;
i = 4;
continue;
}
}
else if (c >> 2 == five_byte)
{
k = byts[i 1];
m = byts[i 2];
n = byts[i 3];
p = byts[i 4];
if (is_utf8_special_byte(k)
&& is_utf8_special_byte(m)
&& is_utf8_special_byte(n)
&& is_utf8_special_byte(p))
{
utf8_yes ;
i = 5;
continue;
}
}
else if (c >> 1 == six_byte)
{
k = byts[i 1];
m = byts[i 2];
n = byts[i 3];
p = byts[i 4];
q = byts[i 5];
if (is_utf8_special_byte(k)
&& is_utf8_special_byte(m)
&& is_utf8_special_byte(n)
&& is_utf8_special_byte(p)
&& is_utf8_special_byte(q))
{
utf8_yes ;
i = 6;
continue;
}
}
utf8_no ;
i ;
}
if (utf8_yes utf8_no == 0)
return false;
int ret = (100 * utf8_yes) / (utf8_yes utf8_no);
if (ret > 90)
{
return true;
}
else
{
return false;
}
}
public static bool is_gb2312_code(byte[] byts)
{
byte one_byte = 0X00; //binary 00000000
int gb2312_yes = 0;
int gb2312_no = 0;
byte k = 0;
byte c = 0;
for (int i = 0; i < byts.Length; )
{
c = byts[i];
if (c >> 7 == one_byte)
{
i ;
continue;
}
else if (c >= 0XA1 && c <= 0XF7)
{
k = byts[i 1];
if (k >= 0XA1 && k <= 0XFE)
{
gb2312_yes ;
i = 2;
continue;
}
}
gb2312_no ;
i = 2;
}
if (gb2312_yes gb2312_no == 0)
return false;
int ret = (100 * gb2312_yes) / (gb2312_yes gb2312_no);
if (ret > 90)
{
return true;
}
else
{
return false;
}
}
public static bool is_big5_code(byte[] byts)
{
byte one_byte = 0X00; //binary 00000000
int big5_yes = 0;
int big5_no = 0;
byte k = 0;
byte c = 0;
for (int i = 0; i < byts.Length; )
{
c = byts[i];
if (c >> 7 == one_byte)
{
i ;
continue;
}
else if (c >= 0XA1 && c <= 0XF9)
{
k = byts[i 1];
if (k >= 0X40 && k <= 0X7E
|| k >= 0XA1 && k <= 0XFE)
{
big5_yes ;
i = 2;
continue;
}
}
big5_no ;
i = 2;
}
if (big5_yes big5_no == 0)
return false;
int ret = (100 * big5_yes) / (big5_yes big5_no);
if (ret > 90)
{
return true;
}
else
{
return false;
}
}
public static bool is_gbk_code(byte[] byts)
{
byte one_byte = 0X00; //binary 00000000
int gbk_yes = 0;
int gbk_no = 0;
byte k = 0;
byte c = 0;
for (int i = 0; i < byts.Length; )
{
c = byts[i];
if (c >> 7 == one_byte)
{
i ;
continue;
}
else if (c >= 0X81 && c <= 0XFE)
{
k = byts[i 1];
if (k >= 0X40 && k <= 0XFE)
{
gbk_yes ;
i = 2;
continue;
}
}
gbk_no ;
i = 2;
}
if (gbk_yes gbk_no == 0)
return false;
int ret = (100 * gbk_yes) / (gbk_yes gbk_no);
if (ret > 90)
return true;
else
return false;
}
}
}
相关软件
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明


网友评论
我要评论