-1

EDIT:

Once again, I ll try to explain it more precisely.

Am writing IRC client - network application based on sockets. Receiving data from server with recv() function and storing them in the rBuf[] array one character after another.
Then copying them to array rBufh[] which is passed to parsing function.
Important thing is that server is sending just one character at a time [one char in one recv()] and keys (char arrays stored in two dim. array [key1]) are the strings which if parsing method finds one of them in received data it needs to react properly basing it reaction on the 3 digits code which meaning is specified in protocol documentation. - as for basics.

Parsing algorithm/function works as follows:

  1. key1 (containing keys) and rBufh arrays are the arguments passed to it
  2. Take first char of the first string stored in key1 and compare it with first char of rBufh
  3. If they match return int ret = 2 (which means 'part of key has been found in received data'

Now recv() adds one char (as the server only sends on char each time) to rBufh array which is passed to parsing function once again and the algorithm starts again till it finds matching key, returning int ret = 1 (found matching key) or int ret = 0 if in step 3 they did not match.

While parser is working, printing to stdout is stoped and basing on value returned by parser printing is either stoped for a longer time (if part of one of the keys from key1 array is found) or allowed to print (if whole key has been found or parser can't match letter passed to it in rBufH array to any of keys stored in key1)

We are getting closer to understanding what I asked for - I hope : )

Quantity of strings stored in key1 array is known beforehand as they are specified in the protocol doc. so initializing it is possible right away while defining.

What i asked is:

is it possible to pass to key1 (not necessarily using sprintf as I did - it just seemed convenient) string which will contain some part of code meaning 'any three digits'.

e.g.
lets mark by XXX the part of code mentioned 3 lines above

key1[0] = "irc2.gbatemp.net XXX nickname"

and now if parser come across any string matching key1[0] which contains ANY three digits in place of XXX it will return 1 - so other function will have posibility to react properly.

This way i don't have to store many strings with different values of XXX in key1 array but just one which maches all of them.


Ok, now I really did my best to help You understand it and so gain an answer : )

Greetings
Tom

P.S.

Feel free to change the title of this topic as I don't really know how should i discribe the problem in few words as You already noticed. (it is as it is because my first idea was to pass some regex to key1)

azrahel
  • 1,143
  • 2
  • 13
  • 31
  • possible duplicate of [Regular expressions in C: examples?](http://stackoverflow.com/questions/1085083/regular-expressions-in-c-examples) – Ed S. May 11 '12 at 21:16
  • *"i cant find it anywhere and regexps are not part of C language as far as i know, so there s no any fitting library to use"* - Sure about that? So regular expressions don't exist in ANSI C. That doesn't mean there are no libraries available. – Ed S. May 11 '12 at 21:17
  • You're printing strings there, not matching input to patterns. Regular expressions would not help you one bit, if that's really your code. What exactly are you trying to do? – Adam Rosenfield May 11 '12 at 21:20

2 Answers2

1

If you just want to insert an arbitrary 3 digit number in your string then do this:

int n = 123; // some integer value that you want to represent as 3 digits
sprintf(key1[1], ":irc2.gbatemp.net %03d %s ", n, nickname);

Note that the %03d format specifier is not a regular expression - it just specifies that the integer parameter will be displayed as a 3 digit decimal value, with leading zeroes if needed.

Paul R
  • 208,748
  • 37
  • 389
  • 560
  • almost what i wanted to do :) i dont want to insert any particular number. I would like to pass to key1[1] string that contains information which can be further used in parsing function which as an argument takes key1 that if any string in parsed buffer contains ":irc2.gbatemp.net >any three digits here< nickname " then it should be parsed. i do not seek algorithm or advices of how to parse it. If these 3 digits number varies from 000 to 666 it doesnt seem too efective to pass 667 strings containing 667 different 3 digits number to key1. That s what I would like to achive. – azrahel May 11 '12 at 21:39
  • I don't get what you're asking. The code that will use key1 - it will use it as a regex? Or it just needs to know that key1 is properly formatted? Or it needs to check if key1 is properly formatted? – djechlin May 11 '12 at 21:46
  • i would like to place this string: ":irc2.gbatemp.net >any three digits here< nickname " in key1 BUT not to specify any paricular three digits, but place in this string some piece of code telling - "there can be any three digits in this place". So when i parse it later and parsing function finds this string but with numbers like 431, 591, 238 and so on, it will parse it. i know am not much of an explainer. :) – azrahel May 11 '12 at 21:56
  • So, just, exit on error if the input n is not between 0 and 999, and the calling function can verify ITS input key1 by regex (or just manually check 0 < character < 9 for each of the three digits to keep it lightweight). There's nothing you can do compile time to force the input, and the only thing it makes sense to do runtime is error if the input isn't valid. I don't understand what other options you could be looking for, because that's about all the tools available to you to confirm the input is correct in C. – djechlin May 11 '12 at 22:23
  • not really what I was looking for and will have to implement it differently, but many thanks for trying :))) Tom – azrahel May 11 '12 at 22:34
  • I don't want to sound harsh, but you really need to formulate your question better - at present your requirements apparently make no sense to me or anyone else. Maybe you could give some examples of what you expect before/after ? – Paul R May 12 '12 at 06:53
  • OK. Edited the question, now we should make it: )) Check above. – azrahel May 12 '12 at 12:00
0

You can't tell the compiler that. Signature is

int sprintf ( char * str, const char * format, ... );

Compiler does not look inside format string and doesn't even make sure you match your %d, %s and int, char* up. This will have to be runtime. Just something like

int printKey(char* key, int n) {
    if(!(0 <= n && n <= 999)) { 
        return -1; //C-style throw an error
    }
    sprintf(key, ":irc2.gbatemp.net %d %s", n, nickname);
    return 0;
}

Also use snprintf not sprintf or you're asking for memory troubles.

djechlin
  • 59,258
  • 35
  • 162
  • 290
  • Please look at the comment to the answer above, maybe You will come up with some solution. Thanks – azrahel May 11 '12 at 21:43
  • 1
    any ideas ? for the comment above ? i comment here, because i dont know if You will be notified without it. greetings – azrahel May 11 '12 at 22:07