"Strange output of 'text' within text"

Started by JimJoyce, February 17, 2014, 01:14:58 PM

Previous topic - Next topic

JimJoyce

I have some javascript  which generates SVG code
to produces a spectrum of colours within a honeycomb of hexagons.
It works OK. I now trying to re-code it in 'C'

It's simply text generation, but I have often to include quotes within strings.
E.g. "Some text,  id='txt', and continue"

Quite often, in the generated code, the trailing single quote is omitted,
or, strangely, replaced with a hash (#);

In my code, lines 221-222, produces the central hexagon:
   fprintf ( fp, "  <use xlink:href='#hxg' x='0' y='0' " );
   fprintf ( fp, " style='fill: rgb(%d,%d,%d);' />\n",  r, g, b );

Then, in several loops, lines 234-237 produce the outer 'rings'.
  calcXY (ring,sector,segmnt);                   // calculate the x,y position of hexagon
  calcRGB (R,G,B,ring,sector,segmnt,stp); // calculate the rgb colour of the hexagon
   fprintf ( fp, "<use xlink:href='#hxg' x='%d' y='%d' ", r, g, b );
   fprintf ( fp, "style='fill: rgb(%d,%d,%d);' />\n", r, g, b );

In the output:
  221-222 correctly produces line 18:
    <use xlink:href='#hxg' x='0' y='0'  style='fill: rgb(200,200,200);' />
'#hxg' correctly refers to a hexagon defined earlier,
and 'x, y': '0,0'  is the correct location.

  but the loop produces: lines 20 ....
   <use xlink:href='#hx# x='0'   y='-52   style='rgb(208,196,196)' />
   <use xlink:href='#hx# x='45' y='-26   style='rgb(204,204,192)' />
   <use xlink:href='#hx# x='45' y='26    style='rgb(196,208,196)' />
   <use xlink:href='#hx# x='0'   y='52    style='rgb(192,204,204)' />
   etc.etc.
The xlink@href:  '#hx# is complete gibberish
and the trailing quote is missing from the 'y' coordinate

Has anyone else had similar problems?
Is it just a problem of text within text strings?

frankie

Could you post a small code snippet to reproduce the problem?
Maybe you have a mishandled pointer that overwrites the format string.
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

JimJoyce

#2
I thing I have already shown the critical code.
But here is a larger chunk.
I had already commented out the inner loop from drawHcmb()
and replaced it with the doLoops().

My abbreviated arguments!!!
shd = shade =r =g =b = rgb(shd,shd,shd)
rng = ring number ( the hexagonal 'rings' around the central ring
stp = step, the rgb colour adjustment between neighbouring hexagons
sc  = scale  NOT USED.

Note the central hexagon is generated in the 'drawHcmb' function
All the surrounding hexagons are produced by doLoops.


/* * * * * * * * * * * * * * * * * * */

int doLoops ( FILE *fp, int shd, int rng, int stp )
  {
  int   i, j, k;
  char use[] = "  <use xlink:href='#hx# x='%d' y='%d ";
  char styl[] = "  style='rgb(%d,%d,%d)' />\n";
  for ( i=0 ; i<rng ; i++ )
    {
    for ( j=0 ; j<6 ; j++ )
      {
      for ( k=0 ; k<i ; k++ )
        {
        calcXY (i, j, k);                // calculate the x,y position of hexagon
        calcRGB (R, G, B, i, j, k, stp); // calculate the rgb colour of the hexagon
        fprintf ( fp, use, hx, hy );
        fprintf ( fp, styl, r, g, b );
        labelHex( fp, hx, hy, r, g, b);  // Label the individual hexagon with its R,G,B values
        }
      }
    }
  return 1;
  }

/* * * * * * * * * * * * * * * * * * */

int    drawHcmb ( FILE* fp, int W, int H, int shd, int rng, int sc )
  {
  fprintf ( fp, "<g id='BG' transform='rotate(0)'>\n" );
                  // Draw Central hexagon, centred on x=0, y=0
  fprintf ( fp, "  <use xlink:href='#hxg' x='0' y='0' " );
  fprintf ( fp, " style='fill: rgb(%d,%d,%d);' />\n", r, g, b );
  labelDec( fp, 0, 0, R, G, B);  // Label the Central Hexagon
                        // Calculate the rgb(), and the x,y location
                        // for each individual hexagon
/*
  for ( ring=1 ; ring<=rng ; ring++ )   // Count the 'Rings' in the Honeycomb
    {
    fputs ( "\n", fp );
    for ( sector=0 ; sector<6 ; sector++ )      // Count the 6 Sectors in each Ring
      {
      for ( segmnt=0 ; segmnt<ring ; segmnt++ ) // Count the Segments within the Sector
        {
        calcXY (ring,sector,segmnt);            // calculate the hx,hy position of hexagon
        calcRGB (R,G,B,ring,sector,segmnt,stp); // calculate the rgb colour of the hexagon
        fprintf ( fp, "<use xlink:href='#hxg' x='%d' y='%d' ", r, g, b );
        fprintf ( fp, "style='fill: rgb(%d,%d,%d);' />\n", r, g, b );
        labelHex( fp,x,y,r,g,b);   // Label the individual hexagon with its R,G,B values
        }
      }
    }
*/
  doLoops ( fp, shd, rng, stp );

  fprintf ( fp, "</g>\n" );
  return 1;
  }

JimJoyce

APOLOGIES
I now see the error of my ways.
I generated the stray hash in doLoops.
char use[] = "  <use xlink:href='#hx# x='%d' y='%d ";
I mistyped '#' for the trailing quote.
I'm very sorry.
JJ

frankie

JJ don't worry, it happen often that the error is under our eyes, but we don't see it.
Maybe if you have not posted the larger chunk you will have never seen it  :-\
"It is better to be hated for what you are than to be loved for what you are not." - Andre Gide

Bitbeisser

Just as a general note, when posting code snippets, please put "code" tags around it (the
  • button above the smiles of the forum text entry box), thanks.

    Ralf

riya20

I had already commented out the inner loop from drawHcmb I mistyped '#' for the trailing quote.