⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pickbin.c

📁 本代码可以区分二进制文件和普通文本(ASCII)文件
💻 C
字号:
/*------------------------------------------------------------------------
 Module:        d:\lccwin32\projects\pickbin\pickbin.c
 Author:        Oscar Cai
 Project:       Pick Text File
 State:         Finished.
 Creation Date: 2005.11.12
 Description:   The purpose of this source file is to pick out
                text files under a specified directory.

				This source will find out all files under the
				specified directory and its subdirectories at
				first, then check out them one by one.

				There are so many kinds of binary files, and
				most of them have one or more zero value byte
				in their content. Therefore, this source
				distinguishs binary files from text stream ones
				by checking out whether there is at least one
				zero value byte in their content.
------------------------------------------------------------------------*/
/* --- The following code comes from d:\lccwin32\lib\wizard\textmode.tpl. */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <io.h>



#define	BUFSIZE 	1024
#undef	RUNTESTUNIT


static int listBinary = 0;
static int quietMode  = 0;
static int usageShown = 0;

/*------------------------------------------------------------------------
 Procedure:     isBinary
 Purpose:       Check out whether a file is a binary file.
 Input:         fname: [in] Specify the file name.
 Output:        If the file is a binary one, return 1. Otherwise,
                return 0. If some error occurs, return 0 too.
 Errors:
------------------------------------------------------------------------*/
int isBinary(char* fname)
{
	static unsigned char buffer[BUFSIZE];
	static FILE *fp;
	static int i, numRead, hasZero;

	if(NULL == fname)
		return 0;

	if(NULL == (fp = fopen(fname, "rb")))
		return 0;

	hasZero = 0;
	while(!feof(fp))
	{
		memset(buffer, 0, BUFSIZE);
		numRead = fread(buffer, 1, BUFSIZE, fp);

		for(i = 0; i < numRead; i++)
		{
			if(0 == buffer[i])
			{
				hasZero = 1;
				break;
			}
		}

		if(hasZero)
			break;
	}

	fclose(fp);
	fp = NULL;

	return hasZero;
}

/*------------------------------------------------------------------------
 Procedure:     searchFiles
 Purpose:       Search and list all files under a directory and its
                subdirectories.
 Input:         dir: [in] Specify the directory.
 Output:
 Errors:
------------------------------------------------------------------------*/
void searchFiles(char * dir)
{
	char fname[BUFSIZE];
	struct _finddata_t file;
	long hFile;

	if(NULL == dir)
		return;

	/**
     * Format the search pattern in fname, cause the search pattern
	 * is used for only once.
	 */
	strcpy(fname, dir);
	if(fname[strlen(fname) - 1] != '\\')
		strcat(fname, "\\*");
	else
		strcat(fname, "*");

	/* Find first file or subdirectory in specified directory */
	if((hFile = _findfirst(fname, &file)) != -1L)
	{
		do
		{
			sprintf(fname, dir[strlen(dir) - 1] == '\\' ? "%s%s" : "%s\\%s",
				dir, file.name);

			if(!(file.attrib & _A_SUBDIR))
			{
				if(isBinary(fname) == listBinary)
					printf("%s\n", fname);
			}
			else if((strcmp(file.name, ".") != 0) && (strcmp(file.name, "..") != 0))
			{
				searchFiles(fname);
			}
		} while(_findnext(hFile, &file) == 0);	// Find the rest files or subdirectories

		_findclose(hFile);
	}
}


#ifdef RUNTESTUNIT

/*****************************************************************************
 * Test functions
 *****************************************************************************/

void testIsBinary(void)
{
	int result;

	result = isBinary("pickbin.exe");
	result = isBinary("D:\\fc2tr.txt");
	result = isBinary("D:\\JBed6WorkDir\\XPSim\\allcfiles.txt");
	result = isBinary("D:\\JBed6WorkDir\\XPSim\\cplib.bat");
	result = isBinary("D:\\JBed6WorkDir\\XPSim\\d.bat");
	result = isBinary("D:\\JBed6WorkDir\\XPSim\\dev.bat");
	result = isBinary("D:\\JBed6WorkDir\\XPSim\\dump.txt");
	result = isBinary("D:\\JBed6WorkDir\\XPSim\\env.txt");
	result = isBinary("D:\\JBed6WorkDir\\XPSim\\Esmertec-RFI-J2ME-CLDC-Devices-1.1 (XP Simulator).xls");
	result = isBinary("D:\\JBed6WorkDir\\XPSim\\Interfaces.doc");
	result = isBinary("D:\\JBed6WorkDir\\XPSim\\oplunit.rar");
	result = isBinary("D:\\JBed6WorkDir\\XPSim\\tmp.htm");
	result = isBinary("D:\\JBed6WorkDir\\XPSim\\WorkLog.txt");
	result = isBinary("D:\\JBed6WorkDir\\XPSim\\WorkSheet.doc");
	result = isBinary("D:\\JBed6WorkDir\\XPSim\\XPSim.txt");
	result = isBinary("D:\\JBed6WorkDir\\XPSim\\os\\win\\32\\x86\\xp\\scripts\\login.bat");
	result = isBinary("D:\\JBed6WorkDir\\XPSim\\ToSvn\\dev.bat");
	result = isBinary("D:\\JBed6WorkDir\\XPSim\\ToSvn\\Esmertec-RFI-J2ME-CLDC-Devices-1.1 (XP Simulator).xls");
	result = isBinary("D:\\JBed6WorkDir\\XPSim\\ToSvn\\Interfaces.doc");
	result = isBinary("D:\\JBed6WorkDir\\XPSim\\ToSvn\\WorkSheet.doc");
	result = isBinary("D:\\JBed6WorkDir\\XPSim\\ToSvn\\.svn\\README.txt");
	result = isBinary("D:\\JBed6WorkDir\\XPSim\\ToSvn\\os\\win\\32\\x86\\xp\\scripts\\login.bat");
	result = isBinary("D:\\JBed6WorkDir\\XPSim\\ToSvn\\os\\win\\32\\x86\\xp\\scripts\\.svn\\README.txt");
	result = isBinary("D:\\JBed6WorkDir\\XPSim\\ToSvn\\os\\win\\32\\x86\\xp\\.svn\\README.txt");
	result = isBinary("D:\\JBed6WorkDir\\XPSim\\ToSvn\\os\\win\\32\\x86\\.svn\\README.txt");
}



#endif // RUNTESTUNIT

void Usage(char *programName)
{
	if(!usageShown && !quietMode)
	{
		char * baseName = strrchr(programName, '\\');
		if(NULL == baseName)
			baseName = programName;
		else
			baseName++;

		printf("\nPick Files v1.0\n");
		printf("\t- Written by Oscar Cai<oscarcai@esmertec.com.cn>\n");
		printf("\t- 2005.11.12\n\n");
//		printf("\t- Copyright 2000-2005 Esmertec AG. All Rights Reserved.\n\n");
		printf("PURPOSE:\n");
		printf("\tThis tool is used to pick out text or binary files\n");
		printf("\tunder a specified directory.\n");
		printf("USAGE:\n");
		printf("\t%s [-?|h|H] [-b|B] [-q|Q] directory\n\n", baseName);
		printf("OPTIONS:\n");
		printf("\t-?|h|H      Show this message.\n");
		printf("\t-b|B        Opposite to default performance, list\n");
		printf("\t            binary files under the directory.\n");
		printf("\t-q|Q        Quiet work mode, only file pathnames.\n");
		printf("\t            will be dumped to stdout.\n");
		printf("\tdirectory   Specify the directory to search.\n\n");

		usageShown = 1;
	}
}

/* returns the index of the first argument that is not an option; i.e.
   does not start with a dash or a slash
*/
int HandleOptions(int argc,char *argv[])
{
	int i = 0;
	int needToShowUsage = 0;
	int firstNonOption  = 0;
	char buffer[BUFSIZE];

	memset(buffer, 0, BUFSIZE);

	for (i=1; i< argc;i++)
	{
		if (argv[i][0] == '/' || argv[i][0] == '-')
		{
			switch (argv[i][1])
		    {
				/* An argument -? means help is requested */
				case '?':
					if(argv[i][2] == 0 || argv[i][2] == ' ')
					{
						needToShowUsage = 1;
						break;
					}
				/**
			     * If the option -h means anything else
				 * in your application add code here
				 * Note: this falls through to the default
				 * to print an "unknow option" message
				 */
				case 'h':
				case 'H':
					if (!stricmp(argv[i]+1,"help")
						|| argv[i][2] == 0 || argv[i][2] == ' ')
					{
						needToShowUsage = 1;
						break;
					}

				/* add your option switches here */
				case 'b':
				case 'B':
					if(argv[i][2] == 0 || argv[i][2] == ' ')
					{
						listBinary = 1;
						break;
					}

				case 'q':
				case 'Q':
					if(argv[i][2] == 0 || argv[i][2] == ' ')
					{
						quietMode = 1;
						break;
					}

				default:
					needToShowUsage = 1;
					strcat(buffer, " ");
					strcat(buffer, argv[i]);
					break;
			}
		}
		else if(firstNonOption == 0)
		{
			firstNonOption = i;
		}
	}

	if(!quietMode)
	{
		if(needToShowUsage)
			Usage(argv[0]);

		if(strlen(buffer))
			printf("unknown option:%s\n", buffer);
	}

	return firstNonOption;
}

int main(int argc,char *argv[])
{
	int i;

	if (argc == 1) {
		/* If no arguments we call the Usage routine and exit */
		Usage(argv[0]);
		return 1;
	}

	/* handle the program options */
	if(0 == (i = HandleOptions(argc,argv)))
	{
		Usage(argv[0]);
		if(!quietMode)
			printf("A directory must be specified!\n");
		return 1;
	}

#ifdef RUNTESTUNIT

	testIsBinary();

#endif // RUNTESTUNIT

	if(i > 0)
	{
		if(!quietMode)
		{
			printf("The %s files under directory '%s' are:\n\n",
				listBinary ? "binary" : "text", argv[i]);
		}
		searchFiles(argv[i]);
	}

	return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -