Hi.

I wrote a patch to support image.backgammonbase.com url generation.

What it does:
 Let you copy position as an url, using image.backgammonbase.com.

Motivation:
 2009 World Champion Mochy requested me to ease use of image.backgammonbase.com
 This site needs gnubgid, but gnubg do not proved it in UI to copy & paste.
 With this patch, only single step of copy and paste from edit menu,
 let user to generate a URL for intended position image.

Reference:
 http://image.backgammonbase.com/form


Nori
diff --git a/backgammon.h b/backgammon.h
index 0860daa..ddaaeea 100644
--- a/backgammon.h
+++ b/backgammon.h
@@ -588,6 +588,7 @@ extern void CommandExportMatchPDF(char *);
 extern void CommandExportMatchPS(char *);
 extern void CommandExportMatchText(char *);
 extern void CommandExportPositionGammOnLine(char *);
+extern void CommandExportPositionBGbase2Clipboard(char *);
 extern void CommandExportPositionGOL2Clipboard(char *);
 extern void CommandExportPositionHtml(char *);
 extern void CommandExportPositionJF(char *);
diff --git a/commands.inc b/commands.inc
index 6ca89a5..a0cf6e1 100644
--- a/commands.inc
+++ b/commands.inc
@@ -211,6 +211,10 @@ command cER = {
       N_("Save the current position in .html format "
          "(special for GammOnLine)"), 
       szFILENAME, &cFilename },
+    { "backgammonbase2clipboard", CommandExportPositionBGbase2Clipboard,
+      N_("Save the current position in html img tag fragment to clipboard"
+         "(special for Backgammonbase)"), 
+      szFILENAME, &cFilename },
     { "gol2clipboard", CommandExportPositionGOL2Clipboard,
       N_("Copy the current position in .html format to clipboard"
          "(special for GammOnLine)"), 
diff --git a/gtkgame.c b/gtkgame.c
index ae4c81a..7b5844d 100644
--- a/gtkgame.c
+++ b/gtkgame.c
@@ -1212,6 +1212,13 @@ static void NewClicked(gpointer  p, guint n, GtkWidget * pw)
 	GTKNew();
 }
 
+static void CopyAsBGbase(gpointer p, guint n, GtkWidget * pw)
+{
+
+	UserCommand("export position backgammonbase2clipboard");
+
+}
+
 static void CopyAsGOL(gpointer p, guint n, GtkWidget * pw)
 {
 
@@ -2964,6 +2971,8 @@ GtkItemFactoryEntry aife[] = {
 	  CommandCopy, 0, NULL, NULL },
 	{ N_("/_Edit/Copy as/GammOnLine (HTML)"), NULL,
 	  CopyAsGOL, 0, NULL, NULL },
+	{ N_("/_Edit/Copy as/Backgammonbase.com (url)"), NULL,
+	  CopyAsBGbase, 0, NULL, NULL },
 
 	{ N_("/_Edit/_Paste Position ID"), "<control>V", PasteIDs, 0,
 		"<StockItem>", GTK_STOCK_PASTE},
@@ -5242,6 +5251,7 @@ extern void GTKProgressValue ( int iValue, int iMax )
     gdouble frac = 1.0 * iValue / (1.0 * iMax );
     gsz = g_strdup_printf("%d/%d (%.0f%%)", iValue, iMax, 100 * frac);
     gtk_progress_bar_set_text( GTK_PROGRESS_BAR( pwProgress ), gsz);
+    printf("%d, %d, %f\n", iValue, iMax, frac);
     gtk_progress_bar_set_fraction( GTK_PROGRESS_BAR( pwProgress ), frac);
     g_free(gsz);
 
diff --git a/html.c b/html.c
index a52b80b..6a17cbc 100644
--- a/html.c
+++ b/html.c
@@ -3654,7 +3654,6 @@ extern void CommandExportPositionGammOnLine ( char *sz ) {
 
 }
 
-
 extern void CommandExportPositionGOL2Clipboard( char *sz )
 {
     char *szClipboard;
@@ -3711,3 +3710,118 @@ extern void CommandExportPositionGOL2Clipboard( char *sz )
     g_free(tmpFile);
 }
 
+/* 
+ * UGH! FIXME
+ *  find handy urlencode function to import.
+ * 
+ */
+static void
+furlencode(FILE* pf, const char * to_encode)
+{
+   int i = 0;
+   unsigned char c;
+   while ( c = to_encode[i++] ) {
+           if( (c >= '0' && c <= '9')
+           ||  (c >= 'A' && c <= 'Z')
+           ||  (c >= 'a' && c <= 'z')
+           ||  (c == '-')
+           ||  (c == '.')
+           ||  (c == '_') )
+               fprintf(pf, "%c", c);
+           else if( c == ' ' )
+               fprintf(pf, "+");
+           else
+               fprintf(pf, "%%%02X", c);
+   }
+}
+
+
+/*
+ * Print URL of position image
+ *  i.e. http://image.backgammonbase.com/image?gnubgid=4HPwATDgc%2FABMA%3AMAAAAAAAAAAA&height=300&width=400&css=minimal&format=png 
+ *
+ * Input:
+ *   pf: output file
+ *   ms: current match state
+ *
+ */
+
+static void
+ExportPositionBGbase( FILE *pf )
+{
+    int fHistory;
+    int iMove;
+    moverecord *pmr = get_current_moverecord ( &fHistory );
+    const matchstate *pms = &ms;
+    printf("ExportPositionBGbase() \n");
+
+    if (!pmr)
+    {
+	    outputerrf(_("Unable to export this position"));
+	    return;
+    }
+
+    fprintf(pf, "http://image.backgammonbase.com/image?gnubgid=";);
+    furlencode(pf, PositionID ( (ConstTanBoard)pms->anBoard ));
+    fprintf(pf, ":");
+    furlencode(pf, MatchIDFromMatchState ( pms ));
+    fprintf(pf, "&height=300&width=400&css=nature&format=png");
+}
+
+
+extern void CommandExportPositionBGbase2Clipboard(char *sz)
+{
+    char *szClipboard;
+    long l;
+	FILE *pf;
+	char *tmpFile;
+
+    if( ms.gs == GAME_NONE ) {
+      outputl( _("No game in progress (type `new game' to start one).") );
+      return;
+    }
+    
+    /* get tmp file */
+
+	pf = GetTemporaryFile(NULL, &tmpFile);
+
+    /* generate file */
+
+    ExportPositionBGbase( pf );
+
+    /* find size of file */
+
+    if ( fseek( pf, 0L, SEEK_END ) ) {
+      outputerr( "temporary file" );
+      return;
+    }
+
+    l = ftell( pf );
+
+    if ( fseek( pf, 0L, SEEK_SET ) ) {
+      outputerr( "temporary file" );
+      return;
+    }
+
+    /* copy file to clipboard */
+
+    szClipboard = (char *) malloc ( l + 1 );
+
+    if (fread( szClipboard, 1, l, pf ) != (unsigned long) l)
+    {
+	    outputerr("temporary file");
+	    free(szClipboard);
+	    fclose(pf);
+    }
+
+    szClipboard[ l ] = 0;
+
+    TextToClipboard( szClipboard );
+
+    free( szClipboard );
+
+    fclose( pf );
+    g_unlink(tmpFile);
+    g_free(tmpFile);
+}
+
_______________________________________________
Bug-gnubg mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/bug-gnubg

Reply via email to