944,132 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 17481
  • C RSS
Aug 7th, 2006
0

Quick search & replace with c-strings?

Expand Post »
I'm in the middle of a project using a mysql database and spanning several languages. A problem has arisen that we need a piece of it to be in C, that reads in a query, replaces some placeholders (wrapped in '?' chars), and then runs said query.

The problem here is, C is not one of my strengths In fact, I'm almost illiterate in it. This is what I have so far, its poorly put together and doesnt even work. What it should do, is replace "?value?" with the value of repl_val.

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int main ()
  5. {
  6. char* query = "SELECT ?value? FROM Table";
  7. char newquery[100];
  8. char* repl_val = "VariableFieldName";
  9.  
  10. char* pch;
  11. int start,end;
  12. int count;
  13.  
  14. // get start and end indices...
  15. pch=strchr(query,'?');
  16. if (pch!=NULL)
  17. {
  18. start = pch-query;
  19. pch=strchr(pch+1,'?');
  20. end = pch-query;
  21. }
  22. // ...or return
  23. if(!start || !end)
  24. {
  25. return 1;
  26. }
  27.  
  28. // copy everything before first '?', concat the replacement value
  29. strncpy(newquery,query,start);
  30. strcat(newquery,repl_val);
  31.  
  32. // concat everything after the second '?', one char at a time
  33. for(count=end; count<strlen(query); count++)
  34. {
  35. strcat(newquery,query[count]);
  36. }
  37.  
  38. printf("new query: %s", newquery);
  39.  
  40. return 0;
  41. }

The last for() loop wont compile, and even if i comment it out, all that prints is the repl_val surrounded by garbage. Am I just going about this all wrong?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
proteusx is offline Offline
3 posts
since Aug 2006
Aug 7th, 2006
0

Re: Quick search & replace with c-strings?

Perhaps this may help a little: Strings: Search and Replace.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Aug 8th, 2006
0

Re: Quick search & replace with c-strings?

  1. char* query = "SELECT %s FROM Table";
  2. char newquery[100];
  3. char* repl_val = "VariableFieldName";
  4. sprintf( newquery, query, repl_value );
Any particular reason why you can't do this?
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Aug 8th, 2006
0

Re: Quick search & replace with c-strings?

well, we've had to revise our design AGAIN. now, what i need to do is strip out every alphanumeric string (0-9,a-z,_) delimited by question marks, then grab an environment variable of the same name. at the same time, we want to silently pass over anything that sint a solid alphanumeric string.

ie, in the following query, i need to replace ?sql_fild1? with the environment variable ${sql_field1}, and so on for sql_field2 and sort_field. We don't want to even touch the question mark for QnAField, however.

  1. "SELECT ?sql_field1?, ?sql_field2? FROM Table WHERE QnAField = "What is the question?" ORDER BY ?sort_field? ASC"
  2.  
  3. // should become:
  4. "SELECT PassedField, AnotherPassedField FROM Table WHERE QnAField = "What is the question?" ORDER BY NameField ASC"
ok, ive managed to put together somthing that *finds* the placeholders (and prints them to stdout for debug purposes), im just not sure how to replace them inline with a getenv() call:

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int do_sql_placeholders(char* source)
  5. {
  6. int in_placeholder = 0; // indicate if we are inside a possible placeholder
  7. int pos = 0;
  8.  
  9. char placeholder[256]; // to store placeholder name
  10. int placepos = 0;
  11.  
  12. for (pos=0; pos < (signed int)strlen(source); pos++)
  13. {
  14. if (in_placeholder)
  15. {
  16. // if continue placeholder
  17. if (
  18. (source[pos] >= '0' && source[pos] <= '9') ||
  19. (source[pos] >= 'A' && source[pos] <= 'Z') ||
  20. (source[pos] >= 'a' && source[pos] <= 'z') ||
  21. source[pos] == '_'
  22. )
  23. {
  24. placeholder[placepos] = source[pos];
  25. placepos++;
  26. }
  27. // else, end place holder
  28. else
  29. {
  30. // valid terminated placeholder, interpolate
  31. if (source[pos] == '?')
  32. {
  33. placeholder[placepos] = '\0';
  34. /*
  35.   instead of printing this out as a match, i need to call getenv(placeholder)
  36.   and replace the placeholder in source with the output of getenv
  37.   */
  38. printf("matched placeholder '%s' at position %d\n", placeholder, pos);
  39. }
  40. // cleanup
  41. in_placeholder = 0;
  42.  
  43. placeholder[0] = '\0';
  44. placepos=0;
  45. }
  46. }
  47. else
  48. {
  49. if (source[pos] == '?') // entering a potential placeholder
  50. {
  51. in_placeholder = 1;
  52. placepos = 0;
  53. }
  54. }
  55. }
  56. return 0;
  57. }
  58.  
  59. int main()
  60. {
  61. char query[100] = "SELECT ?sql_field1?, ?sql field2? FROM Table WHERE QnAField = 'What is the question?' ORDER BY ?sort_field? ASC";
  62.  
  63. do_sql_placeholders(query);
  64. printf("query: %s\n", query);
  65.  
  66. return 0;
  67. }
Last edited by proteusx; Aug 8th, 2006 at 11:44 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
proteusx is offline Offline
3 posts
since Aug 2006
Aug 10th, 2006
1

Re: Quick search & replace with c-strings?

  1. (source[pos] >= '0' && source[pos] <= '9')
Look into isdigit() in <ctype.h>. And isalpha() or isupper()+islower() for the other lines nearby.

Have you every used getenv() before? . . . if not, try reading a man page or searching google for it.
Reputation Points: 185
Solved Threads: 28
Posting Whiz in Training
dwks is offline Offline
269 posts
since Nov 2005
Aug 11th, 2006
0

Re: Quick search & replace with c-strings?

i've managed to get something working using Dave's replace function.

its shaky at best, but it does work for the time being. thanks for the advice. and i'll look into isalpha() and isdigit() as well.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
proteusx is offline Offline
3 posts
since Aug 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Virtual constructor
Next Thread in C Forum Timeline: fread, fwrite issues





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC