| | |
Quick search & replace with c-strings?
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Aug 2006
Posts: 3
Reputation:
Solved Threads: 0
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.
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?
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.
C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> int main () { char* query = "SELECT ?value? FROM Table"; char newquery[100]; char* repl_val = "VariableFieldName"; char* pch; int start,end; int count; // get start and end indices... pch=strchr(query,'?'); if (pch!=NULL) { start = pch-query; pch=strchr(pch+1,'?'); end = pch-query; } // ...or return if(!start || !end) { return 1; } // copy everything before first '?', concat the replacement value strncpy(newquery,query,start); strcat(newquery,repl_val); // concat everything after the second '?', one char at a time for(count=end; count<strlen(query); count++) { strcat(newquery,query[count]); } printf("new query: %s", newquery); return 0; }
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?
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
C Syntax (Toggle Plain Text)
char* query = "SELECT %s FROM Table"; char newquery[100]; char* repl_val = "VariableFieldName"; sprintf( newquery, query, repl_value );
•
•
Join Date: Aug 2006
Posts: 3
Reputation:
Solved Threads: 0
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
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
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. C Syntax (Toggle Plain Text)
"SELECT ?sql_field1?, ?sql_field2? FROM Table WHERE QnAField = "What is the question?" ORDER BY ?sort_field? ASC" // should become: "SELECT PassedField, AnotherPassedField FROM Table WHERE QnAField = "What is the question?" ORDER BY NameField ASC"
getenv() call: C Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> int do_sql_placeholders(char* source) { int in_placeholder = 0; // indicate if we are inside a possible placeholder int pos = 0; char placeholder[256]; // to store placeholder name int placepos = 0; for (pos=0; pos < (signed int)strlen(source); pos++) { if (in_placeholder) { // if continue placeholder if ( (source[pos] >= '0' && source[pos] <= '9') || (source[pos] >= 'A' && source[pos] <= 'Z') || (source[pos] >= 'a' && source[pos] <= 'z') || source[pos] == '_' ) { placeholder[placepos] = source[pos]; placepos++; } // else, end place holder else { // valid terminated placeholder, interpolate if (source[pos] == '?') { placeholder[placepos] = '\0'; /* instead of printing this out as a match, i need to call getenv(placeholder) and replace the placeholder in source with the output of getenv */ printf("matched placeholder '%s' at position %d\n", placeholder, pos); } // cleanup in_placeholder = 0; placeholder[0] = '\0'; placepos=0; } } else { if (source[pos] == '?') // entering a potential placeholder { in_placeholder = 1; placepos = 0; } } } return 0; } int main() { char query[100] = "SELECT ?sql_field1?, ?sql field2? FROM Table WHERE QnAField = 'What is the question?' ORDER BY ?sort_field? ASC"; do_sql_placeholders(query); printf("query: %s\n", query); return 0; }
Last edited by proteusx; Aug 8th, 2006 at 11:44 am.
C Syntax (Toggle Plain Text)
(source[pos] >= '0' && source[pos] <= '9')
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
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
•
•
Join Date: Aug 2006
Posts: 3
Reputation:
Solved Threads: 0
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.
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.
![]() |
Similar Threads
- IE7 will not let me use quick search toolbar (Web Browsers)
- Home Search Assistent, Search Extender, & Shopping Wizard--I've tried everything! (Viruses, Spyware and other Nasties)
- I quick search??? what is it ??? (Viruses, Spyware and other Nasties)
- Quick Search Tips (Windows tips 'n' tweaks)
Other Threads in the C Forum
- Previous Thread: Virtual constructor
- Next Thread: fread, fwrite issues
| Thread Tools | Search this Thread |
* ansi append array arrays bash binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createprocess() database dynamic execv fflush fgets file floatingpointvalidation fork forloop function getlogicaldrivestrin givemetehcodez grade gtkwinlinux histogram homework i/o ide inches include infiniteloop initialization input interest intmain() iso keyboard km license linked linkedlist linux list looping lowest matrix microsoft multi mysql oddnumber open opendocumentformat openwebfoundation overwrite pdf pointer pointers posix power program programming pyramidusingturboccodes radix read recursion recv recvblocked reversing scheduling segmentationfault send sequential single socket socketprogramming stack standard strchr string suggestions test testautomation testing threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






