Well, I refined a bit. I hope Oystein like this.
This version uses fixed size buffer for formatting url, uses no fp
Some people might think too many sprintf. :P
If you want more cleaner thing, it is better to organize broader range of code,
I think.
Nori
2010/4/9 Øystein Johansen <[email protected]>:
>> 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
>>
>
> Nice.
> Just out of curiosity: Why does you code use a temporary file? Could it not
> be simpler to use a string instead of fiddling with the temporary file?
> -Øystein
>
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..c10df64 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,71 @@ extern void CommandExportPositionGOL2Clipboard( char *sz )
g_free(tmpFile);
}
+/*
+ * UGH! FIXME
+ * find handy urlencode function to import.
+ *
+ */
+static int
+surlencode(char * dst, const char * to_encode)
+{
+ int i = 0;
+ char * s = dst;
+ unsigned char c;
+ while ( c = to_encode[i++] ) {
+ if( (c >= '0' && c <= '9')
+ || (c >= 'A' && c <= 'Z')
+ || (c >= 'a' && c <= 'z')
+ || (c == '-')
+ || (c == '.')
+ || (c == '_') )
+ s += sprintf(s, "%c", c);
+ else if( c == ' ' )
+ s += sprintf(s, "+");
+ else
+ s += sprintf(s, "%%%02X", c);
+ }
+ return s - dst;
+}
+
+
+/*
+ * 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
+ *
+ */
+
+
+extern void CommandExportPositionBGbase2Clipboard(char *sz)
+{
+ char szClipboard[256];
+ int fHistory;
+ moverecord *pmr = get_current_moverecord ( &fHistory );
+ const matchstate *pms = &ms;
+ char * s = szClipboard;
+
+ if( ms.gs == GAME_NONE ) {
+ outputl( _("No game in progress (type `new game' to start one).") );
+ return;
+ }
+
+ if (!pmr)
+ {
+ outputerrf(_("Unable to export this position"));
+ return;
+ }
+
+
+ s += sprintf(s, "http://image.backgammonbase.com/image?gnubgid=");
+ s += surlencode(s, PositionID ( (ConstTanBoard)pms->anBoard ));
+ s += sprintf(s, ":");
+ s += surlencode(s, MatchIDFromMatchState ( pms ));
+ s += sprintf(s, "&height=300&width=400&css=nature&format=png");
+
+ TextToClipboard( szClipboard );
+}
+
_______________________________________________
Bug-gnubg mailing list
[email protected]
http://lists.gnu.org/mailman/listinfo/bug-gnubg