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

📄 psp_rss_feed.cpp

📁 PSP_RSS_feed-2.0-src.zip这是使用C++实现的RSS FEED想学的下载,不错
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			{
				//Frame Header is four characters,
				//four bytes for size
				//	(Size is 28-bit integer composed of the
				//	 last 7 bits of each byte, most significant 
				//	 first [except in the case of the MCDI frame]. 
				//	 It does not include the 10 byte frame header.),
				//2 bytes for flags, 
				//and 1 byte for text type
				//	(0x00 = ISO-8859-1, 
				//	 0x01 = UTF-16,
				//	 0x02 = UTF-16BE,
				//	 0x03 = UTF-8)
				audiofile.seekg(cursor);
//Debug
//printf("pointer = %X\ncursor = %X\n", (int)audiofile.tellg(), cursor);
				audiofile.readsome(temp, 10);
				cursor += 10;
				i=4;

				//Padding at end of ID3 tag (supposedly version 4 and up only, but it is in version 3 sometimes)
				if (temp[0]==0)
					break;
				//The MCDI frame size is a direct 32-bit integer
				else if (temp[0]=='M' && temp[1]=='C' && temp[2]=='D' && temp[3]=='I')
				{
					//Most Significant byte * 256^3
					frame_length = (temp[i++] & 0xFF)*16777216;
					//Next to Most Significant byte * 256^2
					frame_length += (temp[i++] & 0xFF)*65536;
					//Next Least Significant byte * 256
					frame_length += (temp[i++] & 0xFF)*256;
					//Least Significant byte
					frame_length += (temp[i++] & 0xFF);
				}
				else
				{
					//Most Significant byte * 128^3
					frame_length = (temp[i++] & 0xFF)*2097152;
					//Next to Most Significant byte * 128^2
					frame_length += (temp[i++] & 0xFF)*16384;
					//Next Least Significant byte * 128
					frame_length += (temp[i++] & 0xFF)*128;
					//Least Significant byte
					frame_length += (temp[i++] & 0xFF);
				}

				//Check for major screw up
				if (frame_length > (id3_length - cursor))
					break;

				temp[4]=0;
//Debug
//printf("temp = %s\nframe_length = %X, temp[5-9] = %02X%02X%02X%02X%02X\n", temp, frame_length, temp[5] & 0xFF, temp[6] & 0xFF, temp[7] & 0xFF, temp[8] & 0xFF, temp[9] & 0xFF);

				if (strcmp(temp, "TIT2") == 0 && default_title == true)
				{
					audiofile.seekg(cursor);
					audiofile.readsome(temp, 1);
					cursor += 1;
					frame_length--;

					//If UTF-16
					if (temp[0] == 0x01 || temp[0] == 0x02)
					{
						while (frame_length > 0)
						{
							//If less than 1024 bytes left read difference
							if (frame_length < 1024)
							{
								audiofile.seekg(cursor);
								audiofile.readsome(temp, frame_length);
								cursor += frame_length;
								remove_nulls(temp, frame_length);
								frame_length=0;
							}
							else
							{
								audiofile.seekg(cursor);
								audiofile.readsome(temp, 1024);
								cursor += 1024;
								remove_nulls(temp, 1024);
								frame_length -= 1024;
							}
							title += temp;
							temp[0]=0;
						}
					}
					//If ISO or UTF-8 or other
					else //if (temp[0] == 0x00 || temp[0] == 0x03)
					{
						while (frame_length > 0)
						{
							//If less than 1024 bytes left read difference
							if (frame_length < 1024)
							{
								audiofile.seekg(cursor);
								audiofile.readsome(temp, frame_length);
								cursor += frame_length;
								temp[frame_length]=0;
								frame_length=0;
							}
							else
							{
								audiofile.seekg(cursor);
								audiofile.readsome(temp, 1024);
								cursor += 1024;
								frame_length -= 1024;
							}
							title += temp;
//Debug
//printf("temp = %s\ntitle = %s\n", temp, title.c_str());
							temp[0]=0;
						}
					}
					default_title = false;
				}
				else if (strcmp(temp, "TPE1") == 0 && default_author == true)
				{
					audiofile.seekg(cursor);
					audiofile.readsome(temp, 1);
					cursor += 1;
					frame_length--;
//Debug
//printf("temp[0] = %X\n", temp[0]);

					//If UTF-16
					if (temp[0] == 0x01 || temp[0] == 0x02)
					{
						while (frame_length > 0)
						{
							//If less than 1024 bytes left read difference
							if (frame_length < 1024)
							{
								audiofile.seekg(cursor);
								audiofile.readsome(temp, frame_length);
								cursor += frame_length;
								remove_nulls(temp, frame_length);
								frame_length=0;
							}
							else
							{
								audiofile.seekg(cursor);
								audiofile.readsome(temp, 1024);
								cursor += 1024;
								remove_nulls(temp, 1024);
								frame_length -= 1024;
							}
							author += temp;
							temp[0]=0;
						}
					}
					//If ISO or UTF-8 or other
					else //if (temp[0] == 0x00 || temp[0] == 0x03)
					{
						while (frame_length > 0)
						{
							//If less than 1024 bytes left read difference
							if (frame_length < 1024)
							{
								audiofile.seekg(cursor);
								audiofile.readsome(temp, frame_length);
								cursor += frame_length;
								temp[frame_length]=0;
								frame_length=0;
							}
							else
							{
								audiofile.seekg(cursor);
								audiofile.readsome(temp, 1024);
								cursor += 1024;
								frame_length -= 1024;
							}
							author += temp;
//Debug
//printf("temp = %s\nauthor = %s\n", temp, author.c_str());
							temp[0]=0;
						}
					}
					default_author = false;
				}
				else
				{
					cursor += frame_length;
					frame_length=0;
				}
			}
			//Check for blank values
			if (default_author == false && author == "")
				default_author == true;
			printf("author = %s\n", author.c_str());
			if (default_title == false && title == "")
				default_title == true;
			printf("title = %s\n", title.c_str());
		}
		else if (strcmp(temp+4, "TAG") == 0)
		{
			author = "";
			title = "";

			//Parse file info
			audiofile.seekg(124, ios_base::end);
			audiofile.readsome(temp, 30);
			temp[30]=0;
			title = temp;
			audiofile.seekg(94, ios_base::end);
			audiofile.readsome(temp, 30);
			temp[30]=0;
			author = temp;

			//Check for empty strings
			if (title != "")
				default_title = false;
			if (author != "")
				default_author = false;
		}

		//Reset shared variables
		temp[0] = 0;
	}
	//Close audiofile
	audiofile.close();

	//Add entry to channel
	xmlfile << "  <item>\n";
//	cout << "  <item>\n";

	//Check title strings for special characters
	if (default_title)
		replace_special(linktemp, audio.substr(0, length-4));
	else
		replace_special(linktemp, title);

	//Add title
	xmlfile << "   <title>" << linktemp << "</title>\n";
//	cout << "   <title>" << linktemp << "</title>\n";

	//Check file strings for special characters
	replace_special(linktemp, web_directory);
	link = linktemp;
	link += '/';
	replace_special(linktemp, audio);
	link += linktemp;

	//Add link
	xmlfile << "   <link>" << link << "</link>\n";
//	cout << "   <link>" << link << "</link>\n";

	//Add author if ID3 tag exists
	if (!default_author)
	{
		xmlfile << "   <author>" << author << "</author>\n";
//		cout << "   <author>" << author << "</author>\n";
	}

	//Add Date
	xmlfile << "   <pubDate>" << date << "</pubDate>\n";
//	cout << "   <pubDate>" << date << "</pubDate>\n";

	//Check file strings for spaces
	encode_url(linktemp, web_directory);
	link = linktemp;
	link += '/';
	encode_url(linktemp, audio);
	link += linktemp;

	//Add file
	xmlfile << "   <enclosure url=\"" << link << "\" type=\"";
//	cout << "   <enclosure url=\"" << link << "\" type=\"";

	//Add type
	if (extension == ".mp3")
	{
		xmlfile << "audio/mp3";
//		cout << "audio/mp3";
	}
	else if (extension == ".aac" || extension == ".mp4")
	{
		xmlfile << "audio/mp4";
//		cout << "audio/mp4";
	}

	//Finish item
	xmlfile << "\"/>\n";
//	cout << "\"/>\n";

	//Close entry
	xmlfile << "  </item>\n";
//	cout << "  </item>\n";
}

void close_xml_file(fstream& xmlfile)
{
	//Setup XML footer
	xmlfile << " </channel>\n";

	//Add rss close tag if none exists
	xmlfile.peek();
	if (xmlfile.eof())
	{
		//Clear eof bit
		xmlfile.clear();

		xmlfile << "</rss>";
	}

	//close xml file
	xmlfile.close();
}

//Set Date so it will remain constant throughout process
void set_date(string UTC_offset, string& date)
{
	char temp[1024];
	char * weekday[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
	char * month[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
	tm *t_date;
	time_t rawtime;

	//Format Date
	//Get Timezone offset
	time(&rawtime);
	t_date = localtime(&rawtime);

	//Day of week
	date = weekday[t_date->tm_wday];
	date += ", ";

	//Day of Month
	sprintf(temp, "%d", t_date->tm_mday);
	date += temp;
	date += " ";

	//Month
	date += month[t_date->tm_mon];
	date += " ";

	//Year
	sprintf(temp, "%04d", t_date->tm_year + 1900);
	date += temp;
	date += " ";

	//Hour
	sprintf(temp, "%02d", t_date->tm_hour);
	date += temp;
	date += ":";

	//Minute
	sprintf(temp, "%02d", t_date->tm_min);
	date += temp;
	date += ":";

	//Second
	sprintf(temp, "%02d", t_date->tm_sec);
	date += temp;
	date += " ";

	//Timezone offset
	date += UTC_offset;
}

int main(int argc, char* argv[])
{
	fstream xmlfile;
	string xmlfilename, web_directory, local_directory, image="", audiolist, title, link="", description="", copyright="", UTC_offset="+0000", special, date;
	int error=0, i=1;

//Debug
//printf("line 285\n");
	//If not enough parameters, prompt for values
	if (strcmp(argv[i], "-U") == 0 || strcmp(argv[i], "-u") == 0)
	{
		system("cls");
		cout << "Output File Name (XML) ==>\n";
		getline (cin, xmlfilename);
		cout << "Web Directory (where files are stored online; no ending \"/\") ==>\n";
		getline (cin, web_directory);
		cout << "Local Directory (where files are stored locally; no ending \"/\") ==>\n";
		getline (cin, local_directory);
		cout << "Channel Title ==>\n";
		getline (cin, title);
		cout << "Time Zone Offset ([+-]hhmm format [i.e., -0600]) ==>\n";
		getline (cin, UTC_offset);
		cout << "Channel Hyper Link ==>\n";
		getline (cin, link);
		cout << "Channel Description ==>\n";
		getline (cin, description);
		cout << "Channel Copyright Information ==>\n";
		getline (cin, copyright);
		cout << "Channel Image File (displayed in channel list on PSP) ==>\n";
		getline (cin, image);
	}
	else if (argv[i][0] != '-' & argc > 1)
	{
		if (argc < 5)
		{
			cout << "Gimme a break here. I need input.\n\n";
			cout << "Unix/Linux:\n";
			cout << "Command prompt>PSP_RSS_feed [-U] <xml output file> <web file directory>\n";
			cout << "\t<local file directory> <channel title> <time zone offset>\n";
			cout << "\t[<channel link> [<channel description> [<channel copyright>\n";
			cout << "\t[<channel image filename>]]]]\n\n";
			cout << "DOS/Windows:\n";
			cout << "Command prompt>PSP_RSS_feed [-U] @inputfile\n";
			cout << "Where inputfile has one line containing:\n";
			cout << "\t<xml output file> <web file directory> <local file directory>\n";
			cout << "\t<channel title> <time zone offset>\n";
			cout << "\t[<channel link> [<channel description> [<channel copyright>\n";
			cout << "\t[<channel image filename>]]]]\n\n";
			cout << "Output will set xml links to \n";
			cout << "\t\"<web file directory>/each audio file in<local file directory>\"\n";
			cout << "If specified, output will set image link to \n";
			cout << "\t\"<web file directory>/<channel image filename>\"\n";
			cout << "\tOtherwise the image link will be set to the first image found.\n";
			cout << "The time zone offset is in [+-]hhmm format (i.e., -0600)\n";
			cout << "\nFor a User Input Prompt, add -U as the only parameter.\n";
			return 1;
		}
		else
		{
			//Scan command line for tags
			xmlfilename = argv[i++];
			web_directory = argv[i++];
			local_directory = argv[i++];
			title = argv[i++];
			if (argc >= 5)
				UTC_offset = argv[i++];
			if (argc >= 6)
				link = argv[i++];
			if (argc >= 7)
				description = argv[i++];
			if (argc >= 8)
				copyright = argv[i++];

			//Check for image file
			if (argc == 9)
				image = argv[i];
		}
	}
	else
	{
		while (i < argc)
		{
			if (strcmp(argv[i], "-L") == 0 || strcmp(argv[i], "-l") == 0)
			{
				i++;
				local_directory = argv[i++];
			}
			else if (strcmp(argv[i], "-O") == 0 || strcmp(argv[i], "-o") == 0)
			{
				i++;
				xmlfilename = argv[i++];
			}
			else if (strcmp(argv[i], "-W") == 0 || strcmp(argv[i], "-w") == 0)
			{
				i++;
				web_directory = argv[i++];
			}
			else if (strcmp(argv[i], "-T") == 0 || strcmp(argv[i], "-t") == 0)
			{
				i++;
				title = argv[i++];
			}
			else if (strcmp(argv[i], "-Z") == 0 || strcmp(argv[i], "-z") == 0)
			{
				i++;
				UTC_offset = argv[i++];
			}
			else if (strcmp(argv[i], "-C") == 0 || strcmp(argv[i], "-c") == 0)
			{
				i++;
				link = argv[i++];
			}
			else if (strcmp(argv[i], "-D") == 0 || strcmp(argv[i], "-d") == 0)
			{
				i++;
				description = argv[i++];
			}
			else if (strcmp(argv[i], "-R") == 0 || strcmp(argv[i], "-r") == 0)
			{
				i++;
				copyright = argv[i++];
			}
			else if (strcmp(argv[i], "-I") == 0 || strcmp(argv[i], "-i") == 0)
			{
				i++;
				image = argv[i++];
			}
			else
			{
				cout << "Incorrect parameter " << argv[i] << ". Ignoring.\n";
			}
		}
	}

	//Check for required input
	if (xmlfilename == "" || web_directory == "" || local_directory == "" || title == "")
	{
		cout << "Gimme a break here. I need input.\n\n";
		cout << "Unix/Linux:\n";
		cout << "Command prompt>PSP_RSS_feed [-U] -O <xml output file> -W <web file directory>\n";
		cout << "\t-L <local file directory> -T <channel title>\n";
		cout << "\t[-Z <time zone offset>] [-C <channel link>] [-D <channel description>]\n";
		cout << "\t[-R <channel copyright>] [-I <channel image filename>]\n\n";
		cout << "DOS/Windows:\n";
		cout << "Command prompt>PSP_RSS_feed [-U] @inputfile\n";
		cout << "Where inputfile has one line containing:\n";
		cout << "\t-O <xml output file> -W <web file directory>\n";
		cout << "\t-L <local file directory> -T <channel title>\n";
		cout << "\t[-Z <time zone offset>] [-C <channel link>] [-D <channel description>]\n";
		cout << "\t[-R <channel copyright>] [-I <channel image filename>]\n\n";
		cout << "Output will set xml links to \n";
		cout << "\t\"<web file directory>/each audio file in<local file directory>\"\n";
		cout << "If specified, output will set image link to \n";
		cout << "\t\"<web file directory>/<channel image filename>\"\n";
		cout << "\tOtherwise the image link will be set to the first image found.\n";
		cout << "The time zone offset is in [+-]hhmm format (i.e., -0600)\n";
		cout << "\nFor a User Input Prompt, add -U as the only parameter.\n";
		return 1;
	}

	//Check for image file
	if (image == "")
		get_image(local_directory, image);

//TODO: Add sanity checks to input

	//Search directory for files
	get_audiolist(local_directory, audiolist);

	//Replace Special Characters
	replace_special(special, web_directory);
	web_directory = special;
	replace_special(special, title);
	title = special;
	replace_special(special, link);
	link = special;
	replace_special(special, description);
	description = special;
	replace_special(special, copyright);
	copyright = special;
	replace_special(special, image);
	image = special;

	//Create xml file
	error = create_xml_file(xmlfile, xmlfilename, web_directory, image, title, link, description, copyright);
	if (error == 1)
	{
		return 1;
	}

	//Set Date
	set_date(UTC_offset, date);

	//Add values to xml file
	while(audiolist.length() > 6)
		add_audiofile(xmlfile, audiolist, local_directory, web_directory, date);

	//close xml file
	close_xml_file(xmlfile);

	return 0;
}

⌨️ 快捷键说明

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