Commits

Oleg Oshmyan committed fd7a76c

Fixed Cygwin support broken by the previous Win32 fix wave

Comments (0)

Files changed (9)

src/AtomicParsley.h

                                                                    */
 //==================================================================//
 
+#if defined HAVE_WINDOWS_H && !defined _WIN32
+#define _WIN32
+#endif
+
 #ifdef _WIN32
 #ifndef _UNICODE
 #define _UNICODE
 	return utf8_string_length;
 }
 
-#if defined (_WIN32)
+#if defined (_WIN32) && !defined (__CYGWIN__)
 unsigned char APar_Return_rawutf8_CP(unsigned short cp_bound_glyph) {
 	unsigned short total_known_points = 0;
 	unsigned int win32cp = GetConsoleCP();
 #elif defined (HAVE_LINUX_CDROM_H)
 "  AudioCD ID frame :           (/path)\n"
 #elif defined (_WIN32)
-"  AudioCD ID frame :           (num)\n"
+"  AudioCD ID frame :           (letter)\n"
 #endif
 "  described text frame :       (str) [desc=(str)] [encoding]\n"
 "  picture frame :              (/path) [desc=(str)] [mimetype=(str)] [imagetype=(hex)] [encoding]\n"
 				const char* derived_filename = NULL;
 #if defined (_WIN32)
 				derived_filename = strrchr(frame_payload, '\\');
+#if defined (__CYGWIN__)
+				const char* derived_filename2 = strrchr(frame_payload, '/');
+				if (derived_filename2 > derived_filename) {
+					derived_filename = derived_filename2;
+				}
+#endif
 #else
 				derived_filename = strrchr(frame_payload, '/');
 #endif
 static const char* fileLevelHelp_text =
 "AtomicParsley help page for general & file level options.\n"
 #if defined (_WIN32)
+#ifndef __CYGWIN__
 "  Note: you can change the input/output behavior to raw 8-bit utf8 if the program name\n"
 "        is appended with \"-utf8\". AtomicParsley-utf8.exe will have problems with files/\n"
 "        folders with unicode characters in given paths.\n"
+#else
+"  Note: you can change the input/output behavior for MCDI functions to raw 8-bit utf8\n"
+"        if the program name is appended with \"-utf8\".\n"
+#endif
 "\n"
 #endif
 "------------------------------------------------------------------------------------------------\n"
 "  rewrite of the original file. Another case where a full rewrite will occur is when the original file\n"
 "  is not optimized and has 'mdat' preceding 'moov'.\n"
 "\n"
-#if defined (_WIN32)
+#if defined (_WIN32) && !defined (__CYGWIN__)
 "Examples:\n"
 "   c:> SET AP_PADDING=\"DEFAULT_PAD=0\"      or    c:> SET AP_PADDING=\"DEFAULT_PAD=3128\"\n"
 "   c:> SET AP_PADDING=\"DEFAULT_PAD=5128:MIN_PAD=200:MAX_PAD=6049\"\n"
 " is to preserve it - if it is present at all. You can choose to eliminate it by setting the environ-\n"
 " mental preference for AP_PADDING to have DEFAULT_PAD=0\n"
 "\n"
-#if defined (_WIN32)
+#if defined (_WIN32) && !defined (__CYGWIN__)
 "Example:\n"
 "   c:> SET AP_PADDING=\"DEFAULT_PAD=0\"\n"
 #else
             show_short_help();
             exit(0);
         } else if ( (strcmp(argv[1],"--longhelp") == 0) || (strcmp(argv[1],"-longhelp") == 0) || (strcmp(argv[1],"-Lh") == 0) ) {
-#if defined (_WIN32)
+#if defined (_WIN32) && !defined (__CYGWIN__)
             if (UnicodeOutputStatus == WIN32_UTF16) { //convert the helptext to utf16 to preserve � characters
                 int help_len = strlen(longHelp_text)+1;
                 wchar_t* Lhelp_text = (wchar_t *)malloc(sizeof(wchar_t)*help_len);
 
 #if defined (_WIN32)
 
+#if !defined (__CYGWIN__)
+
 int wmain( int argc, wchar_t *arguments[])
 {
     int return_val=0;
 
 #endif
 
+#else // defined __CYGWIN__
+
+int main( int argc, char *argv[])
+{
+    size_t name_len = strlen(argv[0]);
+    if (name_len >= 5 &&
+        (strcmp(argv[0] + (name_len-5), "-utf8") == 0 ||
+         strcmp(argv[0] + (name_len-5), "-UTF8") == 0)) {
+        UnicodeOutputStatus = UNIVERSAL_UTF8;
+    } else {
+        UnicodeOutputStatus = WIN32_UTF16;
+    }
+    return real_main(argc, argv);
+}
+
+#endif
+
 #else
 
 int main( int argc, char *argv[])
 void printBOM() {
 	if (BOM_printed) return;
 	
-#if defined (_WIN32)
+#if defined (_WIN32) && !defined (__CYGWIN__)
 	if (UnicodeOutputStatus == WIN32_UTF16) {
 		APar_unicode_win32Printout(L"\xEF\xBB\xBF", "\xEF\xBB\xBF");
 	}
 	return;
 }
 
-#if defined (_WIN32)
+#if defined (_WIN32) && !defined (__CYGWIN__)
 void APar_unicode_win32Printout(wchar_t* unicode_out, char* utf8_out) { //based on http://blogs.msdn.com/junfeng/archive/2004/02/25/79621.aspx
 	//its possible that this isn't even available on windows95
 	DWORD dwBytesWritten;
 #endif
 
 void APar_fprintf_UTF8_data(const char* utf8_encoded_data) {
-#if defined (_WIN32)
+#if defined (_WIN32) && !defined (__CYGWIN__)
 	if (GetVersion() & 0x80000000 || UnicodeOutputStatus == UNIVERSAL_UTF8) {
 		fprintf(stdout, "%s", utf8_encoded_data); //just printout the raw utf8 bytes (not characters) under pre-NT windows
 	} else {
 		}
 		unsigned char* utf8_data = Convert_multibyteUTF16_to_UTF8(unicode_string, asset_length * 6, asset_length);
 
-#if defined (_WIN32)
+#if defined (_WIN32) && !defined (__CYGWIN__)
 		if (GetVersion() & 0x80000000 || UnicodeOutputStatus == UNIVERSAL_UTF8) { //pre-NT or AP-utf8.exe (pish, thats my win98se, and without unicows support convert utf16toutf8 and output raw bytes)
 			unsigned char* utf8_data = Convert_multibyteUTF16_to_UTF8(unicode_string, asset_length * 6, asset_length-14);
 			fprintf(stdout, "%s", utf8_data);
 		
 		unsigned char* utf8_data = Convert_multibyteUTF16_to_UTF8(unicode_string, (asset_length-13) * 6, asset_length-14);
 
-#if defined (_WIN32)
+#if defined (_WIN32) && !defined (__CYGWIN__)
 		if (GetVersion() & 0x80000000 || UnicodeOutputStatus == UNIVERSAL_UTF8) { //pre-NT or AP-utf8.exe (pish, thats my win98se, and without unicows support convert utf16toutf8 and output raw bytes)
 			unsigned char* utf8_data = Convert_multibyteUTF16_to_UTF8(unicode_string, (asset_length -13) * 6, asset_length-14);
 			fprintf(stdout, "%s", utf8_data);
 void ShowVersionInfo() {
 
 #if defined (_WIN32)
-	char unicode_enabled[12];
-	memset(unicode_enabled, 0, 12);
+	char *unicode_enabled;
 	if (UnicodeOutputStatus == WIN32_UTF16) {
-		memcpy(unicode_enabled, "(utf16)", 7);
+#ifndef __CYGWIN__
+		unicode_enabled = "(utf16)";
+#else
+		unicode_enabled = "(utf8 with utf16 CD access)";
+#endif
 
 		// its utf16 in the sense that any text entering on a modern Win32 system
 		// enters as utf16le - but gets converted immediately after AP.exe starts
 		// Printing out to the console should be utf8.
 
 	} else if (UnicodeOutputStatus == UNIVERSAL_UTF8) {
-		memcpy(unicode_enabled, "(raw utf8)", 10);
+#ifndef __CYGWIN__
+		unicode_enabled = "(raw utf8)";
+#else
+		unicode_enabled = "(utf8 with raw utf8 CD access)";
+#endif
 
 		// utf8 in the sense that any text entered had its utf16 upper byte
 		// stripped and reduced to (unchecked) raw utf8 for utilities that work in
 		if (parsedAtoms[iter].AtomicLength > file_size && file_size > 300000) {
 			if (parsedAtoms[iter].AtomicData == NULL) {
 				fprintf(stderr, "AtomicParsley error: an atom was detected that presents as larger than filesize. Aborting. %c\n", '\a');
-#if defined (_WIN32) /* apparently, long long is forbidden there*/
+#if defined (_WIN32) && !defined (__CYGWIN__) /* apparently, long long is forbidden there*/
 				fprintf(stderr, "atom %s is %I64u\n bytes long which is greater than the filesize of %I64u\n", parsedAtoms[iter].AtomicName, parsedAtoms[iter].AtomicLength, file_size);
 #else
 				fprintf(stderr, "atom %s is %" PRIu64 " bytes long which is greater than the filesize of %" PRIu64 "\n", parsedAtoms[iter].AtomicName, parsedAtoms[iter].AtomicLength, file_size);
 		memcpy(temp_path + base_len, file_kind, strlen(file_kind));
 
 	} else if (output_type == -1) { //make the output file invisible by prefacing the filename with '.'
-#if defined (_WIN32)
+#if defined (_WIN32) && !defined (__CYGWIN__)
 		memcpy(temp_path, filePath, base_len);
 		memcpy(temp_path + base_len, file_kind, strlen(file_kind));
 #else
 #if defined(_WIN32)
 			fpos_t file_offset = dest_position + file_pos;
 #elif defined(__GLIBC__)
-      fpos_t file_offset = {0};
+			fpos_t file_offset = {0};
 			file_offset.__pos = dest_position + file_pos;
 #else
 			off_t file_offset = dest_position + file_pos;
 #if defined(_WIN32)
 			fpos_t file_offset = dest_position + file_pos;
 #elif defined(__GLIBC__)
-      fpos_t file_offset = {0};
+			fpos_t file_offset = {0};
 			file_offset.__pos = dest_position + file_pos;
 #else
 			off_t file_offset = dest_position + file_pos;
 		}
 	}
 	if (dynUpd.optimization_flags & MEDIADATA__PRECEDES__MOOV) {
-#if defined (_WIN32)
+#if defined (_WIN32) && !defined (__CYGWIN__)
 		fflush(dest_file);
 		SetEndOfFile((HANDLE)_get_osfhandle(_fileno(dest_file)));
 #else
 	if (dynUpd.updage_by_padding) {
 		APar_DeriveNewPath(ISObasemediafile, temp_file_name, 0, "-data-", NULL); //APar_DeriveNewPath(ISObasemediafile, temp_file_name, -1, "-data-", NULL);
 		temp_file = APar_OpenFile(temp_file_name, "wb");
-#if defined (_WIN32)
+#if defined (_WIN32) && !defined (__CYGWIN__)
 		char* invisi_command=(char*)malloc(sizeof(char)*2*MAXPATHLEN);
 		sprintf (invisi_command,"ATTRIB +S +H \"%s\"",temp_file_name);
 
 	} else if (rewrite_original && !outfile) { //disable overWrite when writing out to a specifically named file; presumably the enumerated output file was meant to be the final destination
 		fclose(source_file);
 
-#if defined (_WIN32) /* native Windows requires removing the file first; rename() on POSIX does the removing automatically as needed */
+#if defined (_WIN32) && !defined (__CYGWIN__) /* native Windows requires removing the file first; rename() on POSIX does the removing automatically as needed */
 		if ( IsUnicodeWinOS() && UnicodeOutputStatus == WIN32_UTF16) {
 			wchar_t* utf16_filepath = Convert_multibyteUTF8_to_wchar(ISObasemediafile);
 
 
 			free(utf16_filepath);
 			utf16_filepath = NULL;
-		} else
-#endif
-		{
+		} else {
 			remove(ISObasemediafile);
 		}
+#endif
 
 		int err = 0;
 
 			originating_file = (char*)ISObasemediafile;
 		}
 
-#if defined (_WIN32)
+#if defined (_WIN32) && !defined (__CYGWIN__)
 		if ( IsUnicodeWinOS() && UnicodeOutputStatus == WIN32_UTF16) {
 			wchar_t* utf16_filepath = Convert_multibyteUTF8_to_wchar(originating_file);
 			wchar_t* temp_utf16_filepath = Convert_multibyteUTF8_to_wchar(temp_file_name);
 		(native-endian) filepath & pass that to a wide stat. Or stat it with a utf8 filepath on Unixen & win32 (stripped utf8).
 ----------------------*/
 off_t findFileSize(const char *utf8_filepath) {
-#if defined (_WIN32)
+#if defined (_WIN32) && !defined (__CYGWIN__)
 	if ( IsUnicodeWinOS() && UnicodeOutputStatus == WIN32_UTF16) {
 		wchar_t* utf16_filepath = Convert_multibyteUTF8_to_wchar(utf8_filepath);
 		
 ----------------------*/
 FILE* APar_OpenFile(const char* utf8_filepath, const char* file_flags) {
 	FILE* aFile = NULL;
-#if defined (_WIN32)
+#if defined (_WIN32) && !defined (__CYGWIN__)
 	if ( IsUnicodeWinOS() && UnicodeOutputStatus == WIN32_UTF16) {
 		wchar_t* Lfile_flags = (wchar_t *)malloc(sizeof(wchar_t)*4);
 		memset(Lfile_flags, 0, sizeof(wchar_t)*4);
 //                                Win32 functions                                    //
 ///////////////////////////////////////////////////////////////////////////////////////
 
+#ifndef HAVE_FSEEKO
+
 int fseeko(FILE *stream, uint64_t pos, int whence) { //only using SEEK_SET here
 	if (whence == SEEK_SET) {
 		fpos_t fpos = pos;
 	return -1;
 }
 
+#endif
+
 /*----------------------
 APar_OpenFileWin32
 	utf8_filepath - a pointer to a string (possibly utf8) of the full path to the file
 }
 
 uint32_t APar_get_mpeg4_time() {
-#if defined (_WIN32)
+#if defined (_WIN32) && !defined (__CYGWIN__)
 	FILETIME  file_time;
 	uint64_t wintime = 0;
 	GetSystemTimeAsFileTime (&file_time);
 void TestFileExistence(const char *filePath, bool errorOut);
 
 #if defined (_WIN32)
+#ifndef HAVE_FSEEKO
 int fseeko(FILE *stream, uint64_t pos, int whence);
+#endif
 HANDLE APar_OpenFileWin32(const char* utf8_filepath, DWORD dwDesiredAccess, DWORD dwShareMode, LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, HANDLE hTemplateFile);
 #endif
 bool IsUnicodeWinOS();
 	char *uuid_str, *end_uuid_str, *uuid_byte;
 	uint8_t uuid_pos, uuid_len;
 	uint8_t keeprap = 0;
-#if defined (_WIN32)
+#if defined (_WIN32) && !defined (__CYGWIN__)
 	char *raw_uuid = _strdup(raw_uuid_in);
 #else
 	char *raw_uuid = strdup(raw_uuid_in);