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

📄 popen.cpp

📁 这些源代码
💻 CPP
字号:
// Popen.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "Popen.h"
#include <stdio.h>
#include <stdlib.h>

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

struct ENTRY
{
	char	szFile[_MAX_PATH];
	char	mod [20];
	int		size;
	ENTRY	*prev;
	ENTRY	*next;
};

ENTRY	*pHead, *pTail;

void StripChar (char *s);
void StripWhite (char *str);
void ListOne ();
void ListColumns();
void LongList ();
void FormatNumber (char *szBuf, long size);
void InsertChar (TCHAR *sz, TCHAR ch);

CWinApp theApp;

using namespace std;

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;
	pHead = pTail = NULL;
// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		cerr << _T("Fatal Error: MFC initialization failed") << endl;
		return (1);
	}

	char   szBuffer[128];
	FILE   *dir;
//
//	Use the dir command and read its output through
//	a pipe. Open the pipe with a read text attribute.
//	The /on flags lists the files in alphabetical order.
//
	if( (dir = _popen( "dir *.* /on", "rt" )) == NULL )
		return (1);
//
//	Read the pipe as a file. When the command finishes,
//	it will return eof.
//
    while( !feof(dir))
	{
//
//	Junk lines all start with a space or carriage return,
//	so skip them.
//
		if( fgets( szBuffer, 128, dir) == NULL )
			continue;
		if (*szBuffer <= ' ')
			continue;
		char *s = szBuffer + 38;
		*(s++) = '\0';
//
//	Don't list the current and parent directorys
//
		if (*s == '.')
			continue;
//
//	Strip to bare file name.
//
		StripWhite (s);
//
//	Get a place for this entry on the heap.
//
		ENTRY *pEntry = new ENTRY;
		memset (pEntry, '\0', sizeof (ENTRY));
		strcpy (pEntry->szFile, s);
//
//	Add it to the end of the linked list.
//
		if (pHead == NULL)
			pHead = pTail = pEntry;
		else
		{
			pTail->next = pEntry;
			pEntry->prev = pTail;
			pTail = pEntry;
		}
//
//	Get the size as a number
//
		s = szBuffer + 25;
		while (*s)
		{
			if (!isdigit(*s))
				StripChar (s);
			else
				++s;
		}
		s = szBuffer + 25;
		pEntry->size = atol (s);
		szBuffer[16] = '\0';
		StripWhite (szBuffer);
		strcpy (pEntry->mod, szBuffer);
	}
//
//	Check the flags to see how to output the
//	directory list.
//
	char cFlag;
	if (argc < 2)
		cFlag = 'c';
	else if (*argv[1] != '-')
		cFlag = '\0';
	else
		cFlag = tolower (*(argv[1] + 1));
	switch (cFlag)
	{
		case 'c':		// List in columns
			ListColumns();
			break;
		case '1':		// List one file per line
			ListOne ();
			break;
		case 'l':		// Long list
			LongList ();
			break;
		default:
			fprintf (stderr, "Invalid flag %s\n\n", argv[1]);
			break;
	}
//
//	Free the linked list.
//
	ENTRY *pCur = pHead;
	while (pCur)
	{
		ENTRY *pTemp = pCur->next;
		delete pCur;
		pCur = pTemp;
	}

	return (nRetCode);
}

//
//	Strip all leading and trailing white space from a string.
//
void StripWhite (char *str)
{
char	*s, *t;

//
//	First strip off any leading white space
//
	s = str;
	while (*s <= ' ')
    {
		if (!*s)
			break;
		StripChar ((char *) s);
    }
//
//	Now go to the end of the string and backtrack until we find a non-white character
//
	s = str;
	t = str + strlen (str) - 1;
	while (t >= s)
	{
		if (*t > ' ')
			break;
		*t = '\0';
		--t;
	}
}

//
//	Remove the character at *s from the string it
//	is in.
//
void StripChar (char *s)
{
	while (*s)
	{
		*s = *(s+1);
		++s;
	}
}

void ListColumns()
{
	ENTRY *pCur = pHead;
	int nLongest = 0;
	while (pCur)
	{
		int len = strlen (pCur->szFile);
		if (len > nLongest)
			nLongest = len;
		pCur = pCur->next;
	}
	nLongest += 2;
	int nCols = 80 / nLongest;
	TCHAR szBuf [128];
	pCur = pHead;
	while (pCur != NULL)
	{
		memset (szBuf, '\0', sizeof (szBuf));
		for (int i = 0; i < nCols; ++i)
		{
			TCHAR *szName = new char [nLongest + 1];
			memset (szName, '\0', nLongest + 1);
			strcpy (szName, pCur->szFile);
			for (int x = strlen (szName); x < nLongest; ++x)
				szName[x] = ' ';
			strcat (szBuf, szName);
			delete [] szName;
			pCur = pCur->next;
			if (pCur == NULL)
				break;
		}
		if (strlen (szBuf))
			printf ("%s\n", szBuf);
	}		
}

void ListOne ()
{
	ENTRY *pCur = pHead;
	while (pCur)
	{
		printf ("%s\n", pCur->szFile);
		pCur = pCur->next;
	}
}

void LongList ()
{
	TCHAR szLine[512];
	memset (szLine, '\0', sizeof (szLine));
	ENTRY *pCur = pHead;
	while (pCur != NULL)
	{
		TCHAR szSize [16];
		memset (szSize, '\0', sizeof (szSize));
		FormatNumber (szSize, pCur->size);
		printf ("%s  % 13s %s\n", pCur->mod,
					szSize, pCur->szFile);
		pCur = pCur->next;
	}
}

void FormatNumber (char *szBuf, long size)
{
	TCHAR szTemp [32];
	memset (szTemp, '\0', sizeof (szTemp));
	sprintf (szTemp + 1, "%ld", size);
	TCHAR *psz = szTemp + strlen (szTemp + 1);
	for (int i = 1; *(psz - 1); ++i, --psz)
	{
		if (!(i%3))
			InsertChar (psz, ',');
	}
	strcpy (szBuf, psz);
}

void InsertChar (TCHAR *sz, TCHAR ch)
{
	char *s = sz + strlen (sz);
	while (s > sz)
	{
		*s = *(s-1);
		--s;
	}
	*s = ch;
}

⌨️ 快捷键说明

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