[Box Backup-commit] COMMIT r1050 - box/chris/merge/bin/bbackupquery

boxbackup-dev@fluffy.co.uk boxbackup-dev@fluffy.co.uk
Sun, 15 Oct 2006 19:54:23 +0100


Author: chris
Date: 2006-10-15 19:54:23 +0100 (Sun, 15 Oct 2006)
New Revision: 1050

Modified:
   box/chris/merge/bin/bbackupquery/BackupQueries.cpp
   box/chris/merge/bin/bbackupquery/bbackupquery.cpp
Log:
Convert command-line arguments from the system locale/character set to
the console character set (code page), so they they can be converted from
console to UTF-8 (yuck).

Don't try to read from stdin or change its code page when it's not open
(invalid file handle) (refs #3)


Modified: box/chris/merge/bin/bbackupquery/BackupQueries.cpp
===================================================================
--- box/chris/merge/bin/bbackupquery/BackupQueries.cpp	2006-10-15 18:51:44 UTC (rev 1049)
+++ box/chris/merge/bin/bbackupquery/BackupQueries.cpp	2006-10-15 18:54:23 UTC (rev 1050)
@@ -102,12 +102,12 @@
 // --------------------------------------------------------------------------
 //
 // Function
-//		Name:    BackupQueries::DoCommand(const char *)
+//		Name:    BackupQueries::DoCommand(const char *, bool)
 //		Purpose: Perform a command
 //		Created: 2003/10/10
 //
 // --------------------------------------------------------------------------
-void BackupQueries::DoCommand(const char *Command)
+void BackupQueries::DoCommand(const char *Command, bool isFromCommandLine)
 {
 	// is the command a shell command?
 	if(Command[0] == 's' && Command[1] == 'h' && Command[2] == ' ' && Command[3] != '\0')
@@ -168,6 +168,25 @@
 		if(!s.empty()) cmdElements.push_back(s);
 	}
 	
+	#ifdef WIN32
+	if (isFromCommandLine)
+	{
+		for (std::vector<std::string>::iterator 
+			i  = cmdElements.begin();
+			i != cmdElements.end(); i++)
+		{
+			std::string converted;
+			if (!ConvertEncoding(*i, CP_ACP, converted, 
+				GetConsoleCP()))
+			{
+				printf("Failed to convert encoding");
+				return;
+			}
+			*i = converted;
+		}
+	}
+	#endif
+		
 	// Check...
 	if(cmdElements.size() < 1)
 	{
@@ -394,7 +413,7 @@
 // --------------------------------------------------------------------------
 //
 // Function
-//		Name:    BackupQueries::List(int64_t, const std::string &, const bool *)
+//		Name:    BackupQueries::List(int64_t, const std::string &, const bool *, bool)
 //		Purpose: Do the actual listing of directories and files
 //		Created: 2003/10/10
 //
@@ -871,7 +890,7 @@
 //		Created: 2003/10/12
 //
 // --------------------------------------------------------------------------
-void BackupQueries::CommandGet(const std::vector<std::string> &args, const bool *opts)
+void BackupQueries::CommandGet(std::vector<std::string> args, const bool *opts)
 {
 	// At least one argument?
 	// Check args
@@ -891,13 +910,21 @@
 	// BLOCK
 	{
 #ifdef WIN32
-		std::string fileName;
-		if(!ConvertConsoleToUtf8(args[0].c_str(), fileName))
-			return;
-#else
-		std::string fileName(args[0]);
+		for (std::vector<std::string>::iterator 
+			i = args.begin(); i != args.end(); i++)
+		{
+			std::string out;
+			if(!ConvertConsoleToUtf8(i->c_str(), out))
+			{
+				fprintf(stderr, "failed to convert encoding\n");
+				return;
+			}
+			*i = out;
+		}
 #endif
 
+		std::string fileName(args[0]);
+
 		if(!opts['i'])
 		{
 			// does this remote filename include a path?
@@ -961,14 +988,6 @@
 		{				
 			// Specified by name, find the object in the directory to get the ID
 			BackupStoreDirectory::Iterator i(dir);
-#ifdef WIN32
-			std::string fileName;
-			if(!ConvertConsoleToUtf8(args[0].c_str(), fileName))
-				return;
-			BackupStoreFilenameClear fn(fileName);
-#else
-			BackupStoreFilenameClear fn(args[0]);
-#endif
 			BackupStoreDirectory::Entry *en = i.FindMatchingClearName(fn);
 			
 			if(en == 0)

Modified: box/chris/merge/bin/bbackupquery/bbackupquery.cpp
===================================================================
--- box/chris/merge/bin/bbackupquery/bbackupquery.cpp	2006-10-15 18:51:44 UTC (rev 1049)
+++ box/chris/merge/bin/bbackupquery/bbackupquery.cpp	2006-10-15 18:54:23 UTC (rev 1050)
@@ -171,7 +171,8 @@
 		}
 
 		// enable input of Unicode characters
-		if (_setmode(_fileno(stdin), _O_TEXT) == -1)
+		if (_fileno(stdin) != -1 &&
+			_setmode(_fileno(stdin), _O_TEXT) == -1)
 		{
 			perror("Failed to set the console input to "
 				"binary mode");
@@ -245,7 +246,7 @@
 		int c = 0;
 		while(c < argc && !context.Stop())
 		{
-			context.DoCommand(argv[c++]);
+			context.DoCommand(argv[c++], true);
 		}
 	}
 	
@@ -263,7 +264,7 @@
 			// Ctrl-D pressed -- terminate now
 			break;
 		}
-		context.DoCommand(command);
+		context.DoCommand(command, false);
 		if(last_cmd != 0 && ::strcmp(last_cmd, command) == 0)
 		{
 			free(command);
@@ -284,13 +285,16 @@
 #endif
 #else
 	// Version for platforms which don't have readline by default
-	FdGetLine getLine(fileno(stdin));
-	while(!context.Stop())
+	if(fileno(stdin) >= 0)
 	{
-		printf("query > ");
-		fflush(stdout);
-		std::string command(getLine.GetLine());
-		context.DoCommand(command.c_str());
+		FdGetLine getLine(fileno(stdin));
+		while(!context.Stop())
+		{
+			printf("query > ");
+			fflush(stdout);
+			std::string command(getLine.GetLine());
+			context.DoCommand(command.c_str(), false);
+		}
 	}
 #endif