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

📄 psp_rss_feed.cpp

📁 PSP_RSS_feed-2.0-src.zip这是使用C++实现的RSS FEED想学的下载,不错
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/*(c) 2006 Christopher James Meacham (alias Optimus[ Prime])
*
*Usage of the works is permitted provided that this instrument is retained with the works, so that any entity that uses the works is notified of this instrument.
*
*DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
*
*[2004, Fair License: rhid.com/fair]
*/

#include <time.h>
#include <stdio.h>
#include <stdlib.h> //Standard C Strings
#include <iostream>
#include <fstream>
#include <dirent.h> //Directory scanning
#include <errno.h>

//There are no symbolic links in Windows so ELOOP will be undefined
#ifndef ELOOP
const int ELOOP=0;
#endif

using namespace std;

//Encode for URL
void encode_url(string& output, string input)
{
	int pos;
	string temp=input, replace_output, replace_temp;
	unsigned char i;
	char j[3]="";

	output = input;

	i = 0x25;
	while (temp.find(i) != string::npos)
	{
		//find space
		pos = temp.find(i);
		//and replace
		sprintf(j, "%02X", i);
		replace_temp = "0";
		replace_temp += j;
		temp.replace(pos, 1, replace_temp);
		replace_output = "%";
		replace_output += j;
		output.replace(pos, 1, replace_output);
	}
	for (i = 0x20; i < 0x25; i++)
	{
		while (temp.find(i) != string::npos)
		{
			//find space
			pos = temp.find(i);
			//and replace
			sprintf(j, "%02X", i);
			replace_temp = "0";
			replace_temp += j;
			temp.replace(pos, 1, replace_temp);
			replace_output = "%";
			replace_output += j;
			output.replace(pos, 1, replace_output);
		}
	}
	for (i = 0x26; i < 0x2D; i++)
	{
		while (temp.find(i) != string::npos)
		{
			//find space
			pos = temp.find(i);
			//and replace
			sprintf(j, "%02X", i);
			replace_temp = "0";
			replace_temp += j;
			temp.replace(pos, 1, replace_temp);
			replace_output = "%";
			replace_output += j;
			output.replace(pos, 1, replace_output);
		}
	}
	for (i = 0x3B; i < 0x41; i++)
	{
		while (temp.find(i) != string::npos)
		{
			//find space
			pos = temp.find(i);
			//and replace
			sprintf(j, "%02X", i);
			replace_temp = "0";
			replace_temp += j;
			temp.replace(pos, 1, replace_temp);
			replace_output = "%";
			replace_output += j;
			output.replace(pos, 1, replace_output);
		}
	}
	for (i = 0x5B; i < 0x61; i++)
	{
		while (temp.find(i) != string::npos)
		{
			//find space
			pos = temp.find(i);
			//and replace
			sprintf(j, "%02X", i);
			replace_temp = "0";
			replace_temp += j;
			temp.replace(pos, 1, replace_temp);
			replace_output = "%";
			replace_output += j;
			output.replace(pos, 1, replace_output);
		}
	}
	for (i = 0x7B; i <= 0x7F; i++)
	{
		while (temp.find(i) != string::npos)
		{
			//find space
			pos = temp.find(i);
			//and replace
			sprintf(j, "%02X", i);
			replace_temp = "0";
			replace_temp += j;
			temp.replace(pos, 1, replace_temp);
			replace_output = "%";
			replace_output += j;
			output.replace(pos, 1, replace_output);
		}
	}
}

//Check strings for special characters
void replace_special(string& output, string input)
{
	int pos;
	string temp=input;

	output = input;

	while (temp.find('&') != string::npos)
	{
		//find space
		pos = temp.find('&');
		//and replace
		temp.replace(pos, 1, "space");
		output.replace(pos, 1, "&amp;");
	}
	while (output.find('<') != string::npos)
	{
		//find space
		pos = output.find('<');
		//and replace
		output.replace(pos, 1, "&lt;");
	}
	while (output.find('>') != string::npos)
	{
		//find space
		pos = output.find('>');
		//and replace
		output.replace(pos, 1, "&gt;");
	}
	while (output.find('\'') != string::npos)
	{
		//find space
		pos = output.find('\'');
		//and replace
		output.replace(pos, 1, "&apos;");
	}
	while (output.find('\"') != string::npos)
	{
		//find space
		pos = output.find('\"');
		//and replace
		output.replace(pos, 1, "&quot;");
	}
}

//Check strings for spaces
//Deprecated
//void replace_spaces(string& output, string input)
//{
//	int pos;
//
//	replace_special(output, input);
//
//	while (output.find(' ') != string::npos)
//	{
//		//find space
//		pos = output.find(' ');
//		//and replace
//		output.replace(pos, 1, "%20");
//	}
//}

//Search local directory for audio files
void get_audiolist(string directory, string& audiolist)
{
	DIR *directory_pointer;
	dirent *file;
	int length=0;
	char error[3]="0";

	//Clear audiolist
	audiolist = "";

	//Set Directory
	directory_pointer = opendir(directory.c_str());
	
	//Catch error
	if (directory_pointer == NULL)
	{
		cout << "\nError accessing the directory:\n\tError #";
		sprintf(error, "%d", errno);
		cout << error;
		cout << " = ";
		switch (errno)
		{
			case EACCES:		cout << "Search permission is denied for a component of the path prefix\n";
						cout << "\t\tof directory or read permission is denied for directory.\n";
						break;
			case ELOOP:		cout << "Too many symbolic links were encountered in resolving the path.\n";
						break;
			case ENAMETOOLONG:	cout << "The length of the directory name exceeds {PATH_MAX}, a pathname\n";
						cout << "\t\tcomponent is longer than {NAME_MAX}, or pathname resolution of\n";
						cout << "\t\ta symbolic link produced an intermediate result whose length\n";
						cout << "\t\texceeds {PATH_MAX}.\n";
						break;
			case ENOENT:		cout << "A component of the path does not name an existing directory or\n";
						cout << "\t\ta directory name was not given.\n";
						break;
			case ENOTDIR:		cout << "A component of the path is not a directory.\n";
						break;
			case EMFILE:		cout << "The maximum number of file descriptors are currently open in the\n";
						cout << "\t\tcalling process.\n";
						break;
			case ENFILE:		cout << "Too many files are currently open in the system.\n";
						break;
			default:		cout << "Unknown Error\n";
						break;
		}
	}

	//Compose audio file list
	while ((file = readdir(directory_pointer)) != NULL)
	{
		//Check file extension
		string temp = file->d_name, temp2;

		length = temp.length();
		if (length > 4)
		{
			temp2 = temp.substr(length-4, 4);
			if (temp2 == ".aac" || temp2 == ".mp3" || temp2 == ".mp4")
			{
				audiolist += temp;
				audiolist += "/";
			}
		}
	}
	closedir(directory_pointer);
//Debug
//printf("audiolist ==>%s\n", audiolist.c_str());
}

//Search web directory for image files and prompt if more than one found
void get_image(string directory, string& image)
{
	DIR *directory_pointer;
	dirent *file;
	int length=0;
	char error[3]="0";

	//Set Directory
	directory_pointer = opendir(directory.c_str());

	//Catch error
	if (directory_pointer == NULL)
	{
		cout << "\nError accessing the directory:\n\tError #";
		sprintf(error, "%d", errno);
		cout << error;
		cout << " = ";
		switch (errno)
		{
			case EACCES:		cout << "Search permission is denied for a component of the path prefix\n";
						cout << "\t\tof directory or read permission is denied for directory.\n";
						break;
			case ELOOP:		cout << "Too many symbolic links were encountered in resolving the path.\n";
						break;
			case ENAMETOOLONG:	cout << "The length of the directory name exceeds {PATH_MAX}, a pathname\n";
						cout << "\t\tcomponent is longer than {NAME_MAX}, or pathname resolution of\n";
						cout << "\t\ta symbolic link produced an intermediate result whose length\n";
						cout << "\t\texceeds {PATH_MAX}.\n";
						break;
			case ENOENT:		cout << "A component of the path does not name an existing directory or\n";
						cout << "\t\ta directory name was not given.\n";
						break;
			case ENOTDIR:		cout << "A component of the path is not a directory.\n";
						break;
			case EMFILE:		cout << "The maximum number of file descriptors are currently open in the\n";
						cout << "\t\tcalling process.\n";
						break;
			case ENFILE:		cout << "Too many files are currently open in the system.\n";
						break;
			default:		cout << "Unknown Error\n";
						break;
		}
	}

	//Get image filename
	while ((file = readdir(directory_pointer)) != NULL)
	{
		//Check file extension
		string temp = file->d_name, temp2;

		length = temp.length();
		if (length > 4)
		{
			temp2 = temp.substr(length-4, 4);
			if (temp2 == ".gif" || temp2 == ".jpg" || temp2 == ".tif" || temp2 == ".png")
			{
				image = temp;
				closedir(directory_pointer);
				return;
			}
		}
	}
	closedir(directory_pointer);
}

int create_xml_file(fstream& xmlfile, string xmlfilename, string directory, string image, string title, string link, string description, string copyright)
{
	FILE *file;
	string temp, image_link="";
	char temp2[1024];

	if (image != "")
	{
		//Check file strings for spaces
		encode_url(temp, directory);
		image_link = temp;
		image_link += '/';
		encode_url(temp, image);
		image_link += temp;
		temp="";
	}

	//Create xml file and setup XML header.
	file = fopen(xmlfilename.c_str(), "a");
	fclose(file);
	xmlfile.open(xmlfilename.c_str(), fstream::in|fstream::out);
	if (!xmlfile.is_open())
	{
		cout << "Error opening xml file:" << xmlfilename << ".\n";
		return 1;
	}

	//If file already exists, seek past header.
	xmlfile.peek();
	if (xmlfile.eof())
	{
		//Clear eof bit
		xmlfile.clear();
//Debug
//printf("line 113");
		//create header
		xmlfile << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
		xmlfile << "<rss version=\"2.0\">\n";
		xmlfile << " <channel>\n";
		xmlfile << "  <title>" << title << "</title>\n";
		if (link != "")
		{
			xmlfile << "  <link>" << link << "</link>\n";
		}
		if (description != "")
		{
			xmlfile << "  <description>" << description << "</description>\n";
		}
		xmlfile << "  <language>en-us</language>\n";
		if (copyright != "")
		{
			xmlfile << "  <copyright>" << copyright << "</copyright>\n";
		}

		if (image_link != "")
		{
			xmlfile << "  <image>\n";
			xmlfile << "   <url>" << image_link << "</url>\n";
			xmlfile << "   <title>" << title << "</title>\n";
			xmlfile << "  </image>\n";
		}

	}
	else
	{
//Debug
//printf("line 132\n");
		//seek past header and any previously created channels
		while (1)
		{
			xmlfile.getline(temp2, 1024);
			temp = temp2;
			if (temp.find("</channel>") != string::npos)
			{
				xmlfile.getline(temp2, 1024);
				temp = temp2;
//Debug
//printf("line 147\n");
				if (temp.find("<channel>") == string::npos)
				{
					int place, count;

					//Clear error flags
					if (xmlfile.eof())
						xmlfile.clear();

					//Do the math
					place=xmlfile.tellg();
					count=xmlfile.gcount();
					xmlfile.seekg(place-count);
					xmlfile.seekp(place-count);
//Debug
//printf("line 154\n");
//printf("place=%d\ncount=%d\nxmlfile.tellg()=%d\nxmlfile.tellp()=%d\n", place, count, (int)xmlfile.tellg(), (int)xmlfile.tellp());
					break;
				}
			}
			if (xmlfile.eof())
			{
				cout << "Error in original XML file. No channel close tag (i.e., \"</channel>\").\n";
				return 1;
			}
		}

//Debug
//printf("line 166\n");
		//Add channel header
		xmlfile << " <channel>\n";
		xmlfile << "  <title>" << title << "</title>\n";
		if (link != "")
		{
			xmlfile << "  <link>" << link << "</link>\n";
		}
		if (description != "")
		{
			xmlfile << "  <description>" << description << "</description>\n";
		}
		xmlfile << "  <language>en-us</language>\n";
		if (copyright != "")
		{
			xmlfile << "  <copyright>" << copyright << "</copyright>\n";
		}

		if (image_link != "")
		{
			xmlfile << "  <image>\n";
			xmlfile << "   <url>" << image_link << "</url>\n";
			xmlfile << "   <title>" << title << "</title>\n";
			xmlfile << "  </image>\n";
		}
	}

	return 0;
}

//length is equal to the total number of characters in the buffer
//(i.e., 0-1023 signifies 1024 characters)
//Debug ==> Untested
void remove_nulls(char text[], int length)
{
	int modified_length=length, i, j;

	for (i=0; i<modified_length; i++)
	{
		//If current character is null, remove
		if (text[i] == 0)
		{
			modified_length--;
			for (j=i+1; j<length; j++)
				text[j-1] = text[j];
			text[length-1]=0;
		}
	}
}

void add_audiofile(fstream& xmlfile, string& audiolist, string local_directory, string web_directory, string date)
{
	bool default_title = true, default_author = true;
	string audio, extension, link, linktemp, title, author;
	char temp[1024];
	fstream audiofile;
	int length=0;

	//Get audio file name
	length = audiolist.find('/');
	audio = audiolist.substr(0, length);

	//Remove audio file from list
	audiolist.replace(0, length+1, "");

	//Check for compatibility
	extension = audio.substr(length-4, 4);
//Debug
//printf("Current file ==>%s\nextension ==>%s\naudiolist ==>%s\n", audio.c_str(), extension.c_str(), audiolist.c_str());
	if (extension != ".mp3" && extension != ".aac" && extension != ".mp4")
	{
		cout << "A format that is unsupported by 2.60 firmware has been scanned into audiolist somehow.\n";
		return;
	}

	//Open audio file to parse info
	audiofile.open((local_directory + "\\" + audio).c_str(), fstream::in | ios_base::binary);
	if (!audiofile.is_open())
	{
		cout << "Error opening" << audio << ".\n";
		cout << "Using defaults.\n";
	}
	else if (extension == ".mp3")
	{
		//Check for ID3v2 tag
		audiofile.readsome(temp, 3);
		temp[3] = 0;

		//Check for ID3v1 tag
		audiofile.seekg(127, ios_base::end);
		audiofile.readsome(temp+4, 3);
		temp[7] = 0;
//Debug
//printf("\nID3v2 = %s\nID3v1 = %s\n", temp, temp+4);

		if (strcmp(temp, "ID3") == 0)
		{
			string temp2;
			int id3_length=0, frame_length=0, i=0, cursor=0;

			//Get id3 length
			//ID3 Header is 3 characters (ID3),
			//2 bytes for version number,
			//1 byte for flags,
			//and 4 bytes for size
			//	(Size is 28-bit integer composed of
			//	 the last 7 bits of each byte, most
			//	 significant first. It does not
			//	 include the 10 byte ID3 header.)
			audiofile.seekg(cursor);
			audiofile.readsome(temp, 10);
			cursor += 10;
			i=6;
			id3_length = 10;
			//Most Significant byte * 128^3
			id3_length += temp[i++]*2097152;
			//Next to Most Significant byte * 128^2
			id3_length += temp[i++]*16384;
			//Next Least Significant byte * 128
			id3_length += temp[i++]*128;
			//Least Significant byte
			id3_length += temp[i++];
			temp[0] = 0;
			author = "";
			title = "";
//Debug
printf("\nFile = %s\nid3_length = %X\n", audio.c_str(), id3_length);

			//Parse file info
			while (cursor < id3_length)

⌨️ 快捷键说明

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