4 Replies - 1480 Views - Last Post: 12 October 2012 - 05:35 AM Rate Topic: -----

#1 n_techo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-October 12

I wish to replace spaces with underscores in a string

Posted 11 October 2012 - 04:13 AM

Hello experts
maybe this is simple for you,

I have the following function.

void replacestring() {

quality = quality->Replace(" ", "_");

}

I wish to replace spaces with underscores in the string "quality"

When I compile I get the error message:

config_files.c: In function ‘replacestring’:
config_files.c:79: error: request for member ‘Replace’ in something not a structure or union

Before I added the function "replacestring" the code compiled, without errors, so I know for sure the error is caused from something being incorrect about this function.

Here is the code,


include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <libintl.h>

#ifndef __GTK_H__
#include <gtk/gtk.h>
#endif


#ifndef __GLOBAL_H__
#include "global.h"
#endif

#ifndef __CONSTANTS_H__
#include "constants.h"
#endif

#ifndef __CONFIG_FILES_H__
#include "config_files.h"
#endif

gboolean darkice_config_store(char opt, char *save_path) {
  int i;
  char adddate;
  char *bitrate;
  char *bitratemode;
  char *samplerate;
  char *quality;
  char *bitspersample;
  char *channel;
  char *buffersize;
  char *device;

  FILE *f_darkice_cfg;

  adddate = (char ) (gtk_toggle_button_get_active ( GTK_TOGGLE_BUTTON (checkbutton_record) ))? '1': '0';
  bitrate = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_bitrate)->entry));
  bitratemode = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_bitratemode)->entry));
  samplerate = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_samplerate)->entry));
  quality = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_quality)->entry));
  bitspersample = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_bitspersample)->entry));
  channel = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_channel)->entry));
  buffersize = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_buffersize)->entry));
  device = (char *) gtk_entry_get_text ( GTK_ENTRY(GTK_COMBO(combo_device)->entry));


  if (!( f_darkice_cfg = (opt != 0)? fopen(darkice_cfg, "w") : fopen(save_path, "w"))) {
      printf("Error: Cannot open %s\n", (opt != 0)? darkice_cfg: save_path);
      return FALSE;
  }

  /* for compatibility issues, mountpoint can start with slash */
  i = 0;


 void replacestring() {

 quality = quality->Replace(" ", "_");

}



  /* Entry writes */

  fprintf(f_darkice_cfg, "[general]");
  fprintf(f_darkice_cfg, "\nduration = ");  
  fprintf(f_darkice_cfg, "0");        /*  duration of encoding, in seconds. 0 means forever */
  fprintf(f_darkice_cfg, "\nbufferSecs = ");
  fprintf(f_darkice_cfg, "%s", buffersize);        /* size of internal slip buffer, in seconds */
  fprintf(f_darkice_cfg, "\nreconnect = ");
  fprintf(f_darkice_cfg, "%s", "yes");
  fprintf(f_darkice_cfg, "\n\n[input]");
  fprintf(f_darkice_cfg, "\ndevice = ");
  fprintf(f_darkice_cfg, "%s", "ameter"); /* OSS, ALSA or JACK */
  fprintf(f_darkice_cfg, "\nsampleRate = "); 
  fprintf(f_darkice_cfg, "%s", samplerate); /* sample rate in Hz. try 11025, 22050 or 44100 */
  fprintf(f_darkice_cfg, "\nbitsPerSample = ");
  fprintf(f_darkice_cfg, "%s", bitspersample); /* bits per sample. try 16 */

  fprintf(f_darkice_cfg, "\nchannel = "); /* channels. 1 = mono, 2 = stereo */
  if (!strcmp(channel, "1 - Mono"))
    fprintf(f_darkice_cfg, "1"); 
  else
    fprintf(f_darkice_cfg, "2");
  
   fprintf(f_darkice_cfg, "\n\n[icecast2-0]");   /* icecast 2 version server */

  /* write bitratemode */
  fprintf(f_darkice_cfg, "\nbitrateMode = ");
  if (!strcmp( gettext("Constant"), bitratemode))
    fprintf(f_darkice_cfg, "cbr"); /* Constant bitrate mode */
  else 
    if (!strcmp( gettext("Average"), bitratemode))
      fprintf(f_darkice_cfg, "abr"); /* average bitrate mode */
    else 
      if (!strcmp( gettext("Variable"), bitratemode)) 
      fprintf(f_darkice_cfg, "vbr"); /* variable bitrate mode */

  fprintf(f_darkice_cfg, "\nformat = ");
  fprintf(f_darkice_cfg, "%s", "aacp"); /* bitrate of the stream sent to the server */

  fprintf(f_darkice_cfg, "\nbitrate = ");
  fprintf(f_darkice_cfg, "%s", bitrate); /* bitrate of the stream sent to the server */
  fprintf(f_darkice_cfg, "\nquality = ");
  fprintf(f_darkice_cfg, "%s", "1.0"); /*  encoding quality */ 
  fprintf(f_darkice_cfg, "\nserver = ");
  fprintf(f_darkice_cfg, "%s", "127.0.0.1");
  fprintf(f_darkice_cfg, "\nport = ");
  fprintf(f_darkice_cfg, "%s", "8000");  /* port of the IceCast2 server, usually 8000 */
  fprintf(f_darkice_cfg, "\npassword = ");
  fprintf(f_darkice_cfg, "%s", "hackme");   /* source password to the IceCast2 server */
  fprintf(f_darkice_cfg, "\nmountPoint = "); 
  fprintf(f_darkice_cfg, "%s", quality);   /* mount point of this stream on the IceCast2 server */
  fprintf(f_darkice_cfg, "%s", ".aac");
  fprintf(f_darkice_cfg, "\nname = "); 
  fprintf(f_darkice_cfg, "%s", quality);   /* name of the stream */
  fprintf(f_darkice_cfg, "\ndescription = "); 
  fprintf(f_darkice_cfg, "%s", "TautokoFM 97.5 & 99.5");   /* description of the stream */
  fprintf(f_darkice_cfg, "\nurl = ");
  fprintf(f_darkice_cfg, "%s", "");   /* URL related to the stream */
  fprintf(f_darkice_cfg, "\ngenre = ");
  fprintf(f_darkice_cfg, "%s", "");   /* genre of the stream */
  fprintf(f_darkice_cfg, "\npublic = ");
  fprintf(f_darkice_cfg, "%s", "yes");


if  (adddate == '1') {
    fprintf(f_darkice_cfg, "\nlocalDumpFile  = ");
    fprintf(f_darkice_cfg, "%s", "/home/user/Recorded_Shows/");
    fprintf(f_darkice_cfg, "%s", quality);
    fprintf(f_darkice_cfg, "%s", ".aac");
    fprintf(f_darkice_cfg, "\nfileAddDate  = ");
    fprintf(f_darkice_cfg, "%s", "yes");
 }  

 
  fprintf(f_darkice_cfg, "\n");

fclose (f_darkice_cfg);

FILE *f_ameter_snd;

if (!( f_ameter_snd = (opt != 0)? fopen(ameter_snd, "w") : fopen(save_path, "w"))) {
      printf("Error: Cannot open %s\n", (opt != 0)? ameter_snd: save_path);
      return FALSE;
  }

/* print ~/.asoundrc for ameter */
fprintf(f_ameter_snd, "pcm_scope.ameter {");
fprintf(f_ameter_snd, "\n	type ameter");
fprintf(f_ameter_snd, "\n}");
fprintf(f_ameter_snd, "\npcm_scope_type.ameter {");
fprintf(f_ameter_snd, "\n	lib /usr/lib/libameter.so");
fprintf(f_ameter_snd, "\n}");
fprintf(f_ameter_snd, "\npcm.ameter {");
fprintf(f_ameter_snd, "\n	type meter");
fprintf(f_ameter_snd, "\n	slave.pcm '");
fprintf(f_ameter_snd, "%s", device);
fprintf(f_ameter_snd, "%s", "'");
fprintf(f_ameter_snd, "\n	scopes.0 ameter");
fprintf(f_ameter_snd, "\n}");
fprintf(f_ameter_snd, "\npcm.dsp0 ameter");
fprintf(f_ameter_snd, "\n");


fclose (f_ameter_snd); 


FILE *f_add2;

if (!( f_add2 = (opt != 0)? fopen(add2, "w") : fopen(save_path, "w"))) {
      printf("Error: Cannot open %s\n", (opt != 0)? add2: save_path);
      return FALSE;
  }

/* print /Stuff/add2.txt */
fprintf(f_add2, ":8000/");
fprintf(f_add2, quality);
fprintf(f_add2, ".aac");
fprintf(f_add2, "\n");


fclose (f_add2); 



FILE *f_name1;

if (!( f_name1 = (opt != 0)? fopen(name1, "w") : fopen(save_path, "w"))) {
      printf("Error: Cannot open %s\n", (opt != 0)? name1: save_path);
      return FALSE;
  }

/* print /Stuff/name1 */

fprintf(f_name1, quality);
fprintf(f_name1, "\n");


fclose (f_name1); 


  return TRUE;
}




Is This A Good Question/Topic? 0
  • +

Replies To: I wish to replace spaces with underscores in a string

#2 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: I wish to replace spaces with underscores in a string

Posted 11 October 2012 - 04:31 AM

Your global variable ( don't use globals ) is of type char *. There is no method Replace for that type. Indeed, there are no methods at all in C.

Try writing your own function for this. It's all of about four lines.

Since you're using GTK, you might consider using their types. GLib is kind of an "objects for C" framework. This function might work for you: http://www.gtk.org/a...html#g-strcanon
Was This Post Helpful? 1
  • +
  • -

#3 n_techo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-October 12

Re: I wish to replace spaces with underscores in a string

Posted 11 October 2012 - 06:50 AM

Thankyou for your reply I found the solution on the page the link you gave me "g_strcanon ()" did not work for me, "g_strcanon ()" done the opposite, of what I wanted, but "g_strdelimit ()" worked perfect,

here was the solution"

quality = (g_strdelimit (quality, " ", '_'));


Thankyou, I would not have solved this without your help,
Was This Post Helpful? 0
  • +
  • -

#4 n_techo  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 14
  • Joined: 11-October 12

Re: I wish to replace spaces with underscores in a string

Posted 12 October 2012 - 04:10 AM

so when I used.

quality = (g_strdelimit (quality, " ", '_'));


then the above code became part of the "darkice_config_store" function.

When the start button is pushed on the darksnow gui then this function is activated, this function writes the config files for darkice.

Unfortunately the code,

quality = (g_strdelimit (quality, " ", '_'));


It would back feed the text with "spaces replaced with underscores to the gtkcombo box",

so I tried,

strcpy(name2, quality)
quality = (g_strdelimit (quality, " ", '_'));


But the code

strcpy(name2, quality)


kept crashing the gui and doing weird stuff, like if I enter lots of text and pushed start on the gui, it would crash and burn.

after spending all night on this and experimenting with varies String Utility Functions from http://www.gtk.org/a....Functions.html

I found the following code worked perfect.

name2 = g_strdup (quality);
name2 = g_strdelimit (name2, " ", '_');


So the string "name2" has the spaces replaced with under scores and the string "quality" is left with spaces,

Now I can use the string "quality" in the places of the darkice config file where spaces are allowed, like the stream name.
Was This Post Helpful? 0
  • +
  • -

#5 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5832
  • View blog
  • Posts: 12,683
  • Joined: 16-October 07

Re: I wish to replace spaces with underscores in a string

Posted 12 October 2012 - 05:35 AM

Glad you found a solution. However, I wouldn't recommend relying on extant code for everything. What you want is pretty trivial.

e.g.
gchar *replace(gchar *s, gchar from, gchar to) {
	gchar *p;
	for(p=s;*p!=0;p++) { if (*p==from) { *p=to; } }
	return s;
}



I'm concerned that you're making a new c-string to do this. Please make sure you free it after.

Consider using a buffer and g_stpcpy to avoid dynamic allocation ( and deallocation) if you don't need it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1