Quick search & replace with c-strings?

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2006
Posts: 3
Reputation: proteusx is an unknown quantity at this point 
Solved Threads: 0
proteusx proteusx is offline Offline
Newbie Poster

Quick search & replace with c-strings?

 
0
  #1
Aug 7th, 2006
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?
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,362
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 242
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Quick search & replace with c-strings?

 
0
  #2
Aug 7th, 2006
Perhaps this may help a little: Strings: Search and Replace.
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Quick search & replace with c-strings?

 
0
  #3
Aug 8th, 2006
  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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 3
Reputation: proteusx is an unknown quantity at this point 
Solved Threads: 0
proteusx proteusx is offline Offline
Newbie Poster

Re: Quick search & replace with c-strings?

 
0
  #4
Aug 8th, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2005
Posts: 251
Reputation: dwks has a spectacular aura about dwks has a spectacular aura about 
Solved Threads: 25
dwks's Avatar
dwks dwks is offline Offline
Posting Whiz in Training

Re: Quick search & replace with c-strings?

 
1
  #5
Aug 10th, 2006
  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.
dwk

Seek and ye shall find.

"Only those who will risk going too far can possibly find out how far one can go."
-- TS Eliot.

"I have not failed. I've just found 10,000 ways that won't work."
-- Thomas Alva Edison

"The only real mistake is the one from which we learn nothing."
-- John Powell
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 3
Reputation: proteusx is an unknown quantity at this point 
Solved Threads: 0
proteusx proteusx is offline Offline
Newbie Poster

Re: Quick search & replace with c-strings?

 
0
  #6
Aug 11th, 2006
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC