Click here to Skip to main content
15,921,028 members
Home / Discussions / C#
   

C#

 
AnswerRe: Validating a "Text" file Pin
EliottA20-Jan-09 4:23
EliottA20-Jan-09 4:23 
GeneralRe: Validating a "Text" file Pin
dwolver20-Jan-09 4:32
dwolver20-Jan-09 4:32 
GeneralRe: Validating a "Text" file Pin
EliottA20-Jan-09 4:35
EliottA20-Jan-09 4:35 
GeneralRe: Validating a "Text" file Pin
musefan20-Jan-09 4:39
musefan20-Jan-09 4:39 
GeneralRe: Validating a "Text" file Pin
Guffa20-Jan-09 6:11
Guffa20-Jan-09 6:11 
AnswerRe: Validating a "Text" file [modified] Pin
Luc Pattyn20-Jan-09 4:38
sitebuilderLuc Pattyn20-Jan-09 4:38 
GeneralRe: Validating a "Text" file Pin
dwolver20-Jan-09 4:42
dwolver20-Jan-09 4:42 
AnswerRe: Validating a "Text" file Pin
Luc Pattyn20-Jan-09 5:09
sitebuilderLuc Pattyn20-Jan-09 5:09 
Hi,

I don't do examples normally, I'll make an exception this once, without warranties, without
service, this is old code, to be taken as is; I use it on a daily basis though.
Class LP_File will not be provided, as far as this method is involved
it just encapsulates simple File methods.

//----------------------------------------------------------------------------------
// Count non-printable characters
// accepts printable characters, plus CR, LF and HTAB
// plus everything in accept string
// return false iff file contains too many non-printable characters
// UTF-8 files (starting with 0xEFBBBF) are always assumed printable.
// Unicode files (starting with 0xFFFE or 0xFEFF) are always assumed printable.
//----------------------------------------------------------------------------------
public static bool IsPrintableFile(string filename, byte[] accept, out long nonPrintables) {
	bool printable=true;
	bool firstRead=true;
	long chars=0;
	int logged=0;
	nonPrintables=0;
	if (accept==null) accept=new byte[0];
	using (LP_File f=new LP_File(filename)) {
		try {
			f.OpenBinaryReader();
			byte[] buffer=new byte[512];
			for ( ; ; ) {
				int len=f.ReadBinary(buffer);
				if (len<=0) break;
				if (firstRead) {
					firstRead=false;
					if (len>=2) {
						byte c0=buffer[0];
						byte c1=buffer[1];
						if (c0==0xFF && c1==0xFE) return true;
						if (c0==0xFE && c1==0xFF) return true;
						if (len>=3) {
							byte c2=buffer[2];
							if (c0==0xEF && c1==0xBB && c2==0xBF) return true;
						}
					}
				}
				for (int j=0 ; j< len ; j++) {
					byte c=buffer[j];
					chars++;
					if (c>=0x20 && c<=0x7F) continue;
					if (c=='\t' || c==0x0A || c==0x0D) continue;
					bool acceptable=false;
					for (int i=0 ; i< accept.Length ; i++) {
						if (c==accept[i]) acceptable=true;
					}
					if (!acceptable) {
						logged++;
						if (logged<20) {
							int i=(int)c;
							Console.WriteLine("Bad character ("+i.ToString("X2")+
								") in "+filename);
						}
						nonPrintables++;
					}
				}
			}
			f.CloseBinaryReader();
			if (logged!=0) Console.WriteLine(logged+" bad chars");
		} catch (Exception exc) {
			env.error("IO error on "+filename+": "+exc);
		}
	}
	if (nonPrintables>(chars/64)) printable=false;
	return printable;
}


I wish tabs were less than 8 positions on CP!
Nowadays I probably would add a few more printable chars such as copyright, euro, and
e with some accents.

Smile | :)
Luc Pattyn [Forum Guidelines] [My Articles]

Good riddance W


GeneralRe: Validating a "Text" file Pin
EliottA20-Jan-09 5:53
EliottA20-Jan-09 5:53 
GeneralRe: Validating a "Text" file Pin
PIEBALDconsult21-Jan-09 5:58
mvePIEBALDconsult21-Jan-09 5:58 
AnswerRe: Validating a "Text" file Pin
Luc Pattyn21-Jan-09 6:10
sitebuilderLuc Pattyn21-Jan-09 6:10 
GeneralRe: Validating a "Text" file Pin
PIEBALDconsult21-Jan-09 6:51
mvePIEBALDconsult21-Jan-09 6:51 
Question[newbie] 'System.Data.SqlDbType.BigInt' is a 'field' but is used like a 'type' Pin
jon-8020-Jan-09 3:51
professionaljon-8020-Jan-09 3:51 
AnswerRe: [newbie] 'System.Data.SqlDbType.BigInt' is a 'field' but is used like a 'type' Pin
Christian Graus20-Jan-09 4:09
protectorChristian Graus20-Jan-09 4:09 
Questionmeasure performance of function Pin
George_George20-Jan-09 1:58
George_George20-Jan-09 1:58 
AnswerRe: measure performance of function Pin
Dave Kreskowiak20-Jan-09 2:05
mveDave Kreskowiak20-Jan-09 2:05 
GeneralRe: measure performance of function Pin
George_George20-Jan-09 2:46
George_George20-Jan-09 2:46 
GeneralRe: measure performance of function Pin
Dave Kreskowiak20-Jan-09 5:39
mveDave Kreskowiak20-Jan-09 5:39 
AnswerRe: measure performance of function Pin
SeMartens20-Jan-09 2:10
SeMartens20-Jan-09 2:10 
GeneralRe: measure performance of function Pin
George_George20-Jan-09 2:47
George_George20-Jan-09 2:47 
GeneralRe: measure performance of function Pin
SeMartens20-Jan-09 2:52
SeMartens20-Jan-09 2:52 
Questionchange the size of pages in rdlc report Pin
bhaumikdv20-Jan-09 1:58
bhaumikdv20-Jan-09 1:58 
AnswerRe: change the size of pages in rdlc report Pin
xxwwyy20-Jan-09 3:00
xxwwyy20-Jan-09 3:00 
Questionusing c header files Pin
lawrenceinba20-Jan-09 1:47
lawrenceinba20-Jan-09 1:47 
AnswerRe: using c header files Pin
musefan20-Jan-09 1:55
musefan20-Jan-09 1:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.