C program to convert Dollar to Rupee - c

Is there a way to write a C program to convert say Dollar to Indian Rupee (or visa-versa). The conversion parameter should not be hard coded but dynamic. More preciously it should get the latest value of Rupee vs Dollar automatically(from Internet) ?

Step 1 would be to get the latest conversion rate. You can use a web-service for that. There are many available. You can try this.
Request:
GET /CurrencyConvertor.asmx/ConversionRate?FromCurrency=INR&ToCurrency=USD HTTP/1.1
Host: www.webservicex.net
Response:
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<double xmlns="http://www.webserviceX.NET/">SOME_RATE_IN_DOUBLE</double>
For sending the request you can make use of cURL.
Once you have the response, just parse it to get the rate. Once you've the rate you can easily write the program to convert.
EDIT:
If using cURL is something you are not comfortable with you can make use of good old system and wget. For this you need to construct the URL first like:
www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=INR&ToCurrency=USD
then from the C program you can do:
char cmd[200];
char URL[] = "www.webservicex.net/CurrencyConvertor.asmx/ConversionRate?FromCurrency=INR&ToCurrency=USD";
sprintf(cmd,"wget -O result.html '%s'",URL); // ensure the URL is in quotes.
system(cmd);
After this the conversion rate is in the file result.html as XML. Just open it and parse it.
If you are using windows, you need to install wget for windows if you don't have it. You can get it here.

First, you need to find a server that can provides the conversion rate. After that, you write your program to fetch the rates from that server and use those information further in your program.
This site, http://www.csharphelp.com/2007/01/currency-converter-server-with-c/ although provides a tutorial for C# + Web, it can give you a general technical idea of how to do it.

Related

Specify Content Type For Each Part Of A Multipart Form Request

I would like to be able to specify the content type for each part of a multipart form request individually, as specified by RFC 2388 section 3. (https://www.ietf.org/rfc/rfc2388.txt)
For example, in cURL, one can invoke the command
curl -v -X POST http://myserver.com/v1/form-data
-F 'request={"hello":"world"};type=application/json'
It was noted last year that many people have requested this, and the feature would be considered for implementation in a future release:
https://stackoverflow.com/a/38529839/4521962
Please reconsider this feature request, as it is a requirement for a number of contemporary REST API's.

POST JSON to url using CURL and C

I'm tring to POST JSON data to a url from bash using:
$ curl -v -d '{xxx:200}&apikey=xxxxx' -X POST http://localhost/xxxx/input/post.json -H "Accept: application/json" -H "Content-Type:application/json"
And in C using the following:
int main(void)
{
CURL *easyhandle;
curl_global_init(CURL_GLOBAL_ALL);
easyhandle = curl_easy_init();
if(easyhandle) {
char *data="json={xxx:200}&apikey=xxxxx";
curl_easy_setopt(easyhandle, CURLOPT_POSTFIELDS, data);
curl_easy_setopt(easyhandle, CURLOPT_URL, "http://localhost/xxxx/input/post.json");
curl_easy_perform(easyhandle);
curl_easy_cleanup (easyhandle);
}
curl_global_cleanup();
return 0;
}
This is what i'm trying to achieve actually:
http://localhost/xxxx/input/post.json?json={xxx:200}&apikey=xxxxx
It doesn't seem to work. :(
I'm a complete novice to curl. Please help.
Thanks!
Fortunately the server I was sending the data handled POST and GET requests, so the code in question would suffice.
Others with a similar problem can use a simple workaround (If your code doesn't have real time constraints and is not performance intensive). You may fork a bash process using system() from C. This avoids you the trouble of encoding.
What you are trying to do is not performing a POST request but performing a GET request. However, I'm not sure this is a good idea, since GET parameters are limited in length (to something like 2 kB or so), and - as others have already mentioned - they need to be encoded and decoded and all the funky stuff which is a pain in the neck.
The URL pointing to localhost suggests me that you have control over the server code. If you used the POST parameters instead of the GET ones, you could use your current code as-is (which sets the POST body of the request, which is probably the right thing to do -- so you don't have to change your client code, you only have to change the server code.)

Does the libcurl library give any way to determine which response header came from which command?

Background:
I'm working on my first C program with the library and I need to gather responses from each command sent to a SMTP server.
I've gotten as far as sending commands to the SMTP server and printing the response headers using curl_easy_setopt(curl_handle, CURLOPT_HEADERFUNCTION, parse_head), but I'm using multi threaded options. It is not at all clear when I get a response which command it was caused by. I am assuming that they will not necessarily be received in the same order sent. Is that correct?
Making it more difficult, since the library handles some calls (like setting up the initial connection) without my explicit request, I would need to handle more headers than explicit requests. That would be predictable and repeatable, but definitely adds an extra level of complexity.
Question:
Is there a "good" way to determine exactly which command resulted in which response header using multi thread?
Also, moderately related, does the library support returning the numeric return code or do I have to manually parse that out? Looking through the library, it seems that it doesn't. I just want to be sure.
I am assuming that they will not necessarily be received in the same order sent. Is that correct?
Yes, it is. That's how multithreading works.
Is there a "good" way to determine exactly which command resulted in which response header using multi thread?
Yes. You can set user data (context info, whatever you call it) using the CURLOPT_HEADERDATA option - this will be passed in as the 4th argument of your header function. So you can write code like this:
CURL *hndl = curl_easy_init();
// ...
curl_easy_setopt(hndl, CURLOPT_HEADERFUNCTION, parse_head);
curl_easy_setopt(hndl, CURLOPT_HEADERDATA, some_pointer_that_identifies_the_thread);
// ...
size_t parse_head(void *buf, void *size_t sz, size_t nmemb, void *context)
{
// context will be the pointer identifying the thread
}
does the library support returning the numeric return code or do I have to manually parse that out?
Yes, it does:
long httpStatus;
curl_easy_getinfo(hndl, CURLINFO_RESPONSE_CODE, &httpStatus);
if (200 <= httpStatus && httpStatus < 300) {
// HTTP 2XX OK
} else {
// Error (4XX, 5XX) or redirect (3XX)
}

Remove HTTP Header Info

In C is there a way to exclude the HTTP header information that comes with the data when using recv() on a socket? I am trying to read some binary data and all I want is the actual binary information, not the HTTP header information. The current data received looks like this:
HTTP/1.1 200 OK
Content-Length: 3314
Content-Type: image/jpeg
Last-Modified: Tue, 20 Mar 2012 14:51:34 GMT
Accept-Ranges: bytes
ETag: "45da99f1a86cd1:6b9"
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
Date: Mon, 20 Aug 2012 14:10:08 GMT
Connection: close
╪ α
I would like only to read the binary portion of the file. (That's obviously not all the binary, only that much was displayed since I printed the output from my recv loop as a string and the first NULL char is after that small binary string).
I just need to get rid of the header portion, is there a simple way to do this?
You would be better of using some HTTP parsing library like curl
If you want to do it yourself:
You can search for '\r\n\r\n' (two \r\n) which separates HTTP headers and contents, and use string/buffer after that.
Also, you need to get Content-Length from header and read that many bytes as http content.
Something like:
/* http_resp has data read from recv */
httpbody = strstr(http_resp, "\r\n\r\n");
if(httpbody)
httpbody += 4; /* move ahead 4 chars
/* now httpbody has just data, stripped down http headers */
Note: make sure strstr does not overrun the memory, may be using strnstr (not sure this exists or not) or similar functions.
I think you need to extract the value of the Content-Length to know the size of the binary data to be read otherwise it will be impossible to know whether all data has been received. A simple approach to consume, and mostly ignore, the header portion is to read the incoming data byte-by-byte until "\r\n\r\n" is encountered, which indicates the end of the header section and the beginning of the content.

Streaming a remote file

i'm new to fmod, and I'm trying to use it for a simple application.
I just need to open a remote music file (mostly mp3, and if that can help I can transcode on the server to always have mp3).
When I try to
FMOD_System_CreateSound(system, "http://somewhere.com/song.mp3", FMOD_SOFTWARE | FMOD_2D | FMOD_CREATESTREAM, 0, &song);
That works fine, it open and play the mp3 fine.
But, when I try to do what I realy need :
FMOD_System_CreateSound(system, "http://somewhere.com/somepage.view?id=4324324324556546456457567456ef3345&var=thing", FMOD_SOFTWARE | FMOD_2D | FMOD_CREATESTREAM, 0, &song);
It just don't works.
That link for example would return a stream.mp3 file, but FMOD just fail on it.
Is there a way to make it works ?
I guess the problem is FMOD just don't find the filename in the link, but I can't change the link :/
If it's not possible, is there a way to make fmod works with curl (curl download the file perfectly), like a function to call for each part of the file ?
Thanks
The main issue with session ID based URLs is they can get quite long. Old versions of FMOD only supported 256 characters (causing truncation and failure to load), but any recent supported version allows up to 1024 characters.
I would recommend updating to a more recent version of FMOD and report back if you have any troubles.

Resources