All pastes #1805694 Raw Edit

Miscellany

public cpp v1 · immutable
#1805694 ·published 2010-02-22 13:23 UTC
rendered paste body
#include <iostream>#include <windows.h>#include <curl/curl.h>#pragma comment(lib, "libcurl")#pragma comment(lib, "zlib")#pragma warning(disable: 4996)using namespace std;char*		pasteSelectFile			( void );char*		pasteGetShortFilePath	( char* fullPath );bool		pasteFileUpload			( CURL* hCon, char* fullPath );char*		pasteGetFileType		( char* filename );char*		pasteGetFileName		( char* filename );const char* pasteGetFileTypeVal		( char* fileType );size_t		pasteRecvData			( void *buffer, size_t size, size_t nmemb, void *userp ); void		pasteAddLink			( char* filePath, char* pasteLink );void		pasteClearLinks			( void );void		pasteCBSet				( const char* data );char*		pasteCBGet				( void );struct pasteNode{	char*		filePath;	char*		pasteLink;	pasteNode	*pNext;	pasteNode( void )	{		filePath = 0;		pasteLink = 0;		pNext = 0;	}	bool Empty( void )	{		if(pasteLink == 0 && filePath == 0)			return true;		return false;	}	void Release( void )	{		delete [] pasteLink;		delete [] filePath;		filePath = 0;		pasteLink = 0;	}} pasteRoot;const char* c_cpAddressPaste	= "http://pastebin.ca/";const char* c_cpAddress			= "http://pastebin.ca";const char* c_cpFieldText		= "content";const char* c_cpFieldTextType	= "type";char		cpCurrentFile[MAX_PATH];struct _sTypes{	const char* c_cpType;	const char* c_cpVal;}			c_cpTypes[]			=	{										{"c",		"3"},										{"cpp",		"4"},										{"h",		"4"},										{"hpp",		"4"},										{"pl",		"6"},										{"php",		"5"},										{"html",	"26"},										{"htm",		"26"},										{"css",		"24"},										{"asp",		"22"},										{"xml",		"15"},										{"java",	"7"},										{"*",		"0"},									};INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd){	CURL				*hCon		= NULL;	char				*pasteFile	= NULL;	struct curl_slist	*slist		= 0;	curl_global_init(CURL_GLOBAL_ALL);	hCon = curl_easy_init();	slist = curl_slist_append(slist, "Expect:");	curl_easy_setopt(hCon, CURLOPT_HTTPHEADER, slist);	curl_easy_setopt(hCon, CURLOPT_POST, 1);	curl_easy_setopt(hCon, CURLOPT_URL,				c_cpAddress);	curl_easy_setopt(hCon, CURLOPT_WRITEFUNCTION,	pasteRecvData);	while( NULL != (pasteFile = pasteSelectFile()) )	{		if(false == pasteFileUpload(hCon, pasteFile))		{			MessageBoxA(NULL, "Failed to Upload File:\r\nInvalid Connectionhandle or File!", "Error", MB_OK);		}	}	curl_slist_free_all(slist);	curl_easy_cleanup(hCon);	pasteClearLinks();	return 0;}char* pasteSelectFile( void ){	OPENFILENAMEA	ofn					= {0};	static char		path[MAX_PATH+1]	= {0};	ofn.lStructSize = sizeof(OPENFILENAMEA);	ofn.hInstance	= GetModuleHandle(NULL);	ofn.lpstrFilter	= "*.*\0";	ofn.lpstrFile	= path;	ofn.nMaxFile	= MAX_PATH;	ofn.lpstrTitle	= "Choose File to Paste";	ofn.Flags		= OFN_FILEMUSTEXIST;	if(FALSE == GetOpenFileNameA(&ofn))	{		return 0;	}	return path;}char* pasteGetFileType( char* filename ){	char *fileType;	if(filename == 0)	{		return NULL;	}	fileType = strrchr(filename, '.')+1;	return fileType;}char* pasteGetFileName( char* filename ){	char* fileName;	if(filename == 0)	{		return NULL;	}	fileName = strtok(filename, ".");	return fileName;}char* pasteGetShortFilePath( char* fullPath ){	char	*shortPath	= NULL;	if( fullPath == 0 )	{		return NULL;	}	shortPath = strrchr(fullPath, '\\')+1;	return shortPath;}bool pasteFileUpload( CURL* hCon, char* fullPath ){	size_t	uiFileSize	= 0;	char	*shortPath	= NULL,			*fileType	= NULL,			*fileTypeVal= NULL;	struct curl_httppost	*post = 0,							*last = 0;	if(hCon == 0 || fullPath == 0)	{		return false;	}	shortPath	= pasteGetShortFilePath(fullPath);	fileType	= pasteGetFileType(shortPath);	fileTypeVal	= (char*)pasteGetFileTypeVal(fileType);	if(shortPath == 0 || fileType == 0 || fileTypeVal == 0)	{		return false;	}	memset(cpCurrentFile, 0x00, MAX_PATH);	strncpy(cpCurrentFile, shortPath, MAX_PATH-1);	curl_easy_setopt(hCon, CURLOPT_FOLLOWLOCATION, 1); 	curl_formadd(&post, &last,	CURLFORM_COPYNAME,		c_cpFieldText,								CURLFORM_FILECONTENT,		fullPath, CURLFORM_END);	curl_formadd(&post, &last,	CURLFORM_COPYNAME,		c_cpFieldTextType,								CURLFORM_COPYCONTENTS,		fileTypeVal, CURLFORM_END);		curl_easy_setopt(hCon, CURLOPT_HTTPPOST, post);	curl_easy_perform(hCon);	curl_formfree(post);	return true;}const char* pasteGetFileTypeVal( char* fileType ){	size_t types = sizeof(c_cpTypes)/sizeof(_sTypes);	for(size_t i=0; i<types; i++)	{		if(!strcmp(c_cpTypes[i].c_cpType, fileType))		{			return c_cpTypes[i].c_cpVal;		}	}	return c_cpTypes[types-1].c_cpVal;}size_t pasteRecvData( void *buffer, size_t size, size_t nmemb, void *userp ){	size_t	pastenum	= 0,			i			= 0,			splitsize	= 0;	char	*splitstr	= "<meta http-equiv=\"refresh\" content=\"7;http://pastebin.ca/",			*split		= 0,			*num		= 0,			*pasteurl	= 0,			*valid		= (char*)buffer;	split		= strstr((char*)buffer, splitstr);	splitsize	= strlen(splitstr);	if(split == NULL)	{		return size*nmemb;	}	while(split[splitsize+i] != '"')	{		i++;	}	num = new char [i+1];	memset(num, 0, i+1);	strncpy(num, split+splitsize, i);	pastenum = atoi(num);		delete [] num;	num = 0;	splitsize	= 8+strlen(c_cpAddressPaste)+1;	pasteurl	= new char [splitsize];	_snprintf(pasteurl, splitsize, "%s%ld", c_cpAddressPaste, pastenum);	pasteAddLink(cpCurrentFile, pasteurl);	delete [] pasteurl;	return size*nmemb;}void pasteAddLink( char* filePath, char* pasteLink ){	pasteNode	*current	= &pasteRoot;	size_t		uiSize		= 0;	if(current->Empty() == true)	{		uiSize = strlen(filePath);		current->filePath = new char [uiSize+1];		memset(current->filePath, 0, uiSize+1);		strcpy(current->filePath, filePath);		uiSize = strlen(pasteLink);		current->pasteLink = new char [uiSize+1];		memset(current->pasteLink, 0, uiSize+1);		strcpy(current->pasteLink, pasteLink);	}	else	{		while(current->pNext != 0)		{			current = current->pNext;		}		current->pNext = new pasteNode;		current = current->pNext;		uiSize = strlen(filePath);		current->filePath = new char [uiSize+1];		memset(current->filePath, 0, uiSize+1);		strcpy(current->filePath, filePath);		uiSize = strlen(pasteLink);		current->pasteLink = new char [uiSize+1];		memset(current->pasteLink, 0, uiSize+1);		strcpy(current->pasteLink, pasteLink);	}}void pasteClearLinks( void ){	char*		pBuffer		= 0;	pasteNode	*current	= &pasteRoot, *temp;	size_t		uiSize		= 0;	bool		bFirst		= true;	if(current->Empty())	{		return;	}	do	{		uiSize	= strlen(pasteCBGet())+strlen(current->pasteLink)+strlen(current->filePath)+2+2;		pBuffer	= new char [uiSize];		if(pBuffer == 0)		{			break;		}				memset(pBuffer, 0, uiSize);		_snprintf(pBuffer, uiSize-1, "%s%s: %s\n", (pasteCBGet()==0||bFirst?"":pasteCBGet()), current->filePath, current->pasteLink);		bFirst = false;		pasteCBSet(pBuffer);		delete [] pBuffer;		pBuffer = 0;		current = current->pNext;	} while(current != 0);	current	= &pasteRoot;	current->Release();	current = current->pNext;	if(current == 0)	{		return;	}		do	{		temp = current;		current = temp->pNext;		temp->Release();		delete temp;	} while(current != 0);}void pasteCBSet( const char* data ){	static bool	bEmpty = true;	char far*	pBuffer;	size_t		uiSize = 0;	HGLOBAL		pCBBuffer;	uiSize	= strlen(data);	OpenClipboard(NULL);		if(bEmpty == true)	{		bEmpty = false;		EmptyClipboard();	}	pCBBuffer	= GlobalAlloc(GMEM_DDESHARE, uiSize+1);	pBuffer		= (char far*)GlobalLock(pCBBuffer);	if(pBuffer == NULL)	{		return;	}	strcpy(pBuffer, data);	GlobalUnlock(pCBBuffer);		SetClipboardData(CF_TEXT, pCBBuffer);	CloseClipboard();}char* pasteCBGet( void ){	char*	pBuffer = 0;	size_t	uiSize	= 0;	HANDLE	hData;	if(OpenClipboard(NULL) == FALSE)	{		return NULL;	}	hData	= GetClipboardData(CF_TEXT);	pBuffer	= (char*)GlobalLock(hData);	GlobalUnlock(hData);	CloseClipboard();	return pBuffer;}