You are not logged in.

#1 2013-02-08 15:44:56

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

[SOLVED] C: replace a number in a string

Hello folks,

I'm still working on my RuneScape set of applications, but I'm almost there. Right now I am implementing language support.

To do this, I have to read a file that's made and written to by RuneScape. It containts only one string:

Language=0/1/2/3

I can already read the file and obtain its contents. Now, I will have to remove the Language= part so I only have the number. I don't think that'd be too hard to do.

What would be hard, or at least I think, is that I have a string (-Dcom.jagex.config=http://www.runescape.com/k=3/l=0/jav_config.ws) to feed to java to actually launch the game. I would have to replace the number after l= in that string, with the number I retrieved from the above file. How would I go about doing that? The string must remain a (g)char, since I am combining all variables to one string with g_strjoin.

Could this be done?

Last edited by Unia (2013-02-08 22:18:40)


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#2 2013-02-08 15:57:08

kaszak696
Member
Registered: 2009-05-26
Posts: 543

Re: [SOLVED] C: replace a number in a string

I think the easiest is to cut your string into 2 parts '-Dcom.jagex.config=http://www.runescape.com/k=3/l=' and '/jav_config.ws' and just join them with the number between. Or you could just use regular expressions.
I heven't touched C since forever and my memory is kinda fuzzy, but aren't C strings just arrays of char? You could just find the position of l=0 and write new char in there.

Last edited by kaszak696 (2013-02-08 15:57:41)


'What can be asserted without evidence can also be dismissed without evidence.' - Christopher Hitchens
'There's no such thing as addiction, there's only things that you enjoy doing more than life.' - Doug Stanhope
GitHub Junkyard

Offline

#3 2013-02-08 16:01:03

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] C: replace a number in a string

kaszak696 wrote:

I think the easiest is to cut your string into 2 parts '-Dcom.jagex.config=http://www.runescape.com/k=3/l=' and '/jav_config.ws' and just join them with the number between.

I had thought of that, but that string is also read from a configuration file the user can change so it would take alot of steps to get that going.

kaszak696 wrote:

I didn't touch C since forever and my memory is kinda fuzzy, but aren't C strings just arrays of char? You could just find the position of l=0 and write new char in there.

I have no idea, this is my first project with C, but I was hoping such a solution would be possible.


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#4 2013-02-08 16:15:08

kaszak696
Member
Registered: 2009-05-26
Posts: 543

Re: [SOLVED] C: replace a number in a string

strstr() from C standard library seems promising: http://www.cplusplus.com/reference/cstring/strstr/
The example shows how to replace stuff inside a string, is this useful to you?
If you need more control, i'd recommend regular expressions again.

Last edited by kaszak696 (2013-02-08 16:16:28)


'What can be asserted without evidence can also be dismissed without evidence.' - Christopher Hitchens
'There's no such thing as addiction, there's only things that you enjoy doing more than life.' - Doug Stanhope
GitHub Junkyard

Offline

#5 2013-02-08 16:27:56

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] C: replace a number in a string

Oh hey that does look interesting! Glib does have its own "branded" version of it so it certainly looks like a viable option to use.

I will start tinkering and see if I can get it to work!

EDIT: Yep, that works! Sadly, Glib does not have a viable alternative to strncpy (Glib's alternatives all trail it with a NULL, so the rest of the string doesn't make it out alive) but that's only a minor caveat to my OCD.

Now I just need to figure out how to process Language=X to just X, but I'm sure that won't be too hard wink

EDIT2: Maybe I spoke too soon. I can't seem to find a function that does what I want. Can anyone give me a pointer?

Last edited by Unia (2013-02-08 17:02:55)


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#6 2013-02-08 18:33:29

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,548
Website

Re: [SOLVED] C: replace a number in a string

I think I must be missing something.  Wouldn't this do what you are describing:

/*assuming file opened and pointed to by FILE *in */
int n;
fscanf(in,"Language=%d",&n);
char url[URL_LEN];
sprintf(url,"-Dcom.jagex.config=http://www.runescape.com/k=3/l=%d/jav_config.ws",n);

"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#7 2013-02-08 18:55:34

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] C: replace a number in a string

^ That works, but as stated above I'd like to stick with Glib. I'll see if there are some Glib alternatives to use.

EDIT: They're there. However, I'm worried about this line:

g_sprintf(url, "-Dcom.jagex.config=http://www.runescape.com/k=3/l=%d/jav_config.ws", language);

What if the user, for some reason, would change that -Dcom.jagex.config=http://www.runescape.com/k=3/l=%d/jav_config.ws in the configuration file? For example, RuneScape has different worlds (servers) to play on. The url line above just chooses one based on your location and IP adress, but users can choose a static world by changing url to this:

-Dcom.jagex.config=http://world1.runescape.com/k=3/l=%d/jav_config.ws

Would that break the code?

Last edited by Unia (2013-02-08 18:59:57)


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#8 2013-02-08 19:04:11

digirium
Member
Registered: 2012-11-15
Posts: 51

Re: [SOLVED] C: replace a number in a string

You could do ret = fscanf(...) and check ret==1 to validate the scan.

Offline

#9 2013-02-08 19:05:45

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] C: replace a number in a string

digirium wrote:

You could do ret = fscanf(...) and check ret==1 to validate the scan.

Not sure what that is supposed to do.. I mean, the user is supposed to be able to choose a static world. If it is for error checking on the other hand, I already have this:

FILE *in = g_fopen (appletviewer, "r");
if(!in) {
	g_fprintf(stderr, "Unable to read file: %s. Falling back to defaults.\n", appletviewer);
} else {
	fscanf(in, "Language=%d", &language);
	g_sprintf(url, "-Dcom.jagex.config=http://www.runescape.com/k=3/l=%d/jav_config.ws", language);
}

If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#10 2013-02-08 19:20:12

digirium
Member
Registered: 2012-11-15
Posts: 51

Re: [SOLVED] C: replace a number in a string

The %d would be replaced wherever it was in the url string its a formatting marker.

Offline

#11 2013-02-08 19:22:27

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] C: replace a number in a string

But let's say the user changed url (in the configuration file) to -Dcom.jagex.config=http://world1.runescape.com/k=3/l=%d/jav_config.ws and I have this code:

{
	fscanf(in, "Language=%d", &language);
	g_sprintf(url, "-Dcom.jagex.config=http://www.runescape.com/k=3/l=%d/jav_config.ws", language);
}

As you can see, that g_sprintf will now override the user's choise to start in world1. That's my problem with this approach.

Last edited by Unia (2013-02-08 19:22:44)


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#12 2013-02-08 19:25:07

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,548
Website

Re: [SOLVED] C: replace a number in a string

Then read that string from the configuration file.

Im also not sure why you want to use Glib only functions.  Why use the specialized tool when the general tool will do the same thing?


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#13 2013-02-08 19:33:27

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] C: replace a number in a string

Trilby wrote:

Im also not sure why you want to use Glib only functions.  Why use the specialized tool when the general tool will do the same thing?

Because I also use GTK and Glib functions work better with GTK. So I have to use Glib, in which case I prefer to use as much Glib as possible to avoid a mixed code base between pure C and Glib.


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#14 2013-02-08 19:35:33

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,548
Website

Re: [SOLVED] C: replace a number in a string

GTK is built around the standard libraries, and Glib calls standard library functions.  GTK does not work better with Glib functions.  You already have a "mixed code base" - if you include glib headers, they include stdlib and stdio.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#15 2013-02-08 22:15:47

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] C: replace a number in a string

For now I went on a different approach. Here's what I came up with (which I'm going to improve in the future, probably with Trilby's suggestion)

gchar *
parselanguage(gchar *appletviewer) {
	gchar *langstring = NULL, *replace;
	gsize length;
	GError *error_opening_appletviewer = NULL;

	if(g_file_get_contents(appletviewer, &langstring, &length, &error_opening_appletviewer) == FALSE) {
		if(error_opening_appletviewer != NULL) {
			g_fprintf(stderr, "Unable to read file: %s. Falling back to defaults.\n", error_opening_appletviewer->message);
			g_error_free(error_opening_appletviewer);
		}
	} else {
		langstring[strlen(langstring)-1] = '\0';
		if(g_strcmp0(langstring, "Language=0") == 0) {
			language = "0";
		} else if(g_strcmp0(langstring, "Language=1") == 0) {
			language = "1";
		} else if(g_strcmp0(langstring, "Language=2") == 0) {
			language = "2";
		} else if(g_strcmp0(langstring, "Language=3") == 0) {
			language = "3";
		}
		replace = g_strrstr(url, "?");
		strncpy (replace, language, 1);
	}

	return language;
}

If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#16 2013-02-08 22:50:51

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,548
Website

Re: [SOLVED] C: replace a number in a string

There is no need to use a strcmp function to compare one character, a numerical comparison will be much faster.  For example, the following two lines are equivalent:

if (g_strcmp0(langstring, "Language=0")==0) {
if (langstring[9] == '0') {

The same would apply to all the else ifs.

Also, a strncpy(...,...,1) can be replaced by simply setting the appropriate byte.  The following two lines are equivalent:

strncpy(replace,language,1);
*replace = language[0];

In each of these, either one will be functionally equivalent, but the first uses complex string functions with a bit of overhead, where the second only uses basic operators.

Last edited by Trilby (2013-02-08 22:59:30)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#17 2013-02-08 23:19:03

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] C: replace a number in a string

Trilby wrote:

Also, a strncpy(...,...,1) can be replaced by simply setting the appropriate byte.  The following two lines are equivalent:

strncpy(replace,language,1);
*replace = language[0];

Can I also use a similar approach when language is an integer? If so, I could make this work, which is an improvement over my above approach:

if(g_file_get_contents(appletviewer, &langstring, NULL, NULL) == FALSE) {
	g_fprintf(stderr, "Unable to read file: %s. Falling back to defaults.\n", appletviewer);
} else {
	sscanf(langstring, "Language=%d", &language);
	replace = g_strrstr(url, "?");
	*replace = language;
}

EDIT: I'm thinking sprintf is the way to go to parse the language, but then I would need to get the -Dcom string from a configuration file and I'm not sure how I would go about doing that. If sprintf is indeed the way to go, how would I do that?

Last edited by Unia (2013-02-09 00:11:11)


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#18 2013-02-09 00:18:41

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,548
Website

Re: [SOLVED] C: replace a number in a string

You couldn't do it just like that, but if language (as an int) will always only be one digit you can do the "cheap" conversion from int to a char for an int with

*replace = language + 48

This works because the Zero character is ascii code 48, the One character is 49, Two is 50, etc.

But why would you want to do this?  You are pulling a character for a number out of langstring, converting it to an integer, then converting it back to a character for a number.  Just grab the character and put it in the other string:

*replace = langstring[9];

No need for any of the conditionals nor string functions.


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#19 2013-02-09 00:22:20

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,548
Website

Re: [SOLVED] C: replace a number in a string

gchar 
parselanguage(gchar *appletviewer) {
	gchar *langstring = NULL;
	gsize length;
	GError *error_opening_appletviewer = NULL;

	if(g_file_get_contents(appletviewer, &langstring, &length, &error_opening_appletviewer) == FALSE) {
		if(error_opening_appletviewer != NULL) {
			g_fprintf(stderr, "Unable to read file: %s. Falling back to defaults.\n", error_opening_appletviewer->message);
			g_error_free(error_opening_appletviewer);
		}
	} else {
		*g_strrstr(url, "?") = langstring[9];
	}

	return langstring[9];
}

"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#20 2013-02-09 00:22:32

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] C: replace a number in a string

Oh man, I can't see the forest anymore because of the trees. There's just so many ways to go about this...

But! What you just suggested is what I think the best way to go about this, so now it's really fixed! Thanks again Trilby, you're always helping me out big_smile


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#21 2013-02-09 00:35:24

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] C: replace a number in a string

Nevermind

Last edited by Unia (2013-02-09 00:57:28)


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#22 2013-02-09 12:11:00

jjacky
Member
Registered: 2011-11-09
Posts: 347
Website

Re: [SOLVED] C: replace a number in a string

Trilby wrote:
gchar 
parselanguage(gchar *appletviewer) {
	gchar *langstring = NULL;
	gsize length;
	GError *error_opening_appletviewer = NULL;

	if(g_file_get_contents(appletviewer, &langstring, &length, &error_opening_appletviewer) == FALSE) {
		if(error_opening_appletviewer != NULL) {
			g_fprintf(stderr, "Unable to read file: %s. Falling back to defaults.\n", error_opening_appletviewer->message);
			g_error_free(error_opening_appletviewer);
		}
	} else {
		*g_strrstr(url, "?") = langstring[9];
	}

	return langstring[9];
}

Quick notes about this, just in case:
- if g_file_get_contents fails langstring will be set to NULL, so "return langstring[9]" will likely segfault. You should e.g. return a default value in that case, or something.
- if g_file_get_contents suceeds, you are to call g_free on langstring when you're done, to free the memory. Here it's not done, hence a memory leak. You need to e.g. store that char in a variable, free the memory, then return the variable.

Offline

#23 2013-02-09 15:01:57

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] C: replace a number in a string

jjacky wrote:

Quick notes about this, just in case:
- if g_file_get_contents fails langstring will be set to NULL, so "return langstring[9]" will likely segfault. You should e.g. return a default value in that case, or something.
- if g_file_get_contents suceeds, you are to call g_free on langstring when you're done, to free the memory. Here it's not done, hence a memory leak. You need to e.g. store that char in a variable, free the memory, then return the variable.

Thanks for your interest. I had already done so, like this:

void
parselanguage(gchar *appletviewer) {
	gchar *langstring = NULL;

	if(g_file_get_contents(appletviewer, &langstring, NULL, NULL) == FALSE) {
		g_fprintf(stderr, "Unable to read file: %s. Falling back to defaults.\n", appletviewer);
		langstring = "Language=0";
		*g_strrstr(url, "?") = langstring[9];
	} else {
		*g_strrstr(url, "?") = langstring[9];
		g_free(langstring);
	}
}

Any other way of handling it segfaults.


If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#24 2013-02-09 16:13:55

Unia
Member
From: Stockholm, Sweden
Registered: 2010-03-30
Posts: 2,486
Website

Re: [SOLVED] C: replace a number in a string

Nevermind. I got rid of the url option in the configuration altogether. Now, the user can specify world=worldX and I'm parsing both world and language at once:

if(world) {
    url = g_strdup_printf("-Dcom.jagex.config=http://%s.runescape.com/k=3/l=%d/jav_config.ws", world, language[9]-48);
} else {
    url = g_strdup_printf("-Dcom.jagex.config=http://www.runescape.com/k=3/l=%d/jav_config.ws", language[9]-48);
}

If you can't sit by a cozy fire with your code in hand enjoying its simplicity and clarity, it needs more work. --Carlos Torres

Offline

#25 2013-02-09 17:09:34

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,548
Website

Re: [SOLVED] C: replace a number in a string

On more minor adjustment - and this one was my fault: the above lines convert the 9th character of language to a number, then the printf converts it back to a character.  Instead you can just do the following:

    url = g_strdup_printf("-Dcom.jagex.config=http://www.runescape.com/k=3/l=%c/jav_config.ws", language[9]);

"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

Board footer

Powered by FluxBB