string search in c from a http GET request - c

how to extract a specific string from an array of characters?
in my case buff contain what's shown in the image and i want to copy the file name 'hi.jpg' to another array of char, keep in mind that the file name will change depending on the http request sent by the browser.

You can use strstr(buff,"GET ") and strstr(buff," HTTP/1.1") to find the start end end pos, then use strncpy to copy whats in between.

Related

Returning the filename of the current sketch

I am trying to write a GUI that will display the name of the sketch it was generated from using a simple text() command. However, I am running into trouble getting any of the general JS solutions to work for me. Many solutions I have found use the filename reserved word but that does not seem to be reserved in Processing 3.5.4. I have also tried parsing the strings using a similar method to what can be found here. I am very new to processing and this is only my 2nd attempt at using Processing.
Any advice would be greatly appreciated
You can get the path (as a string) to the sketch with sketchPath().
From there you could either parse the string (pull off everything after the last slash) to get the sketch name, or you can use sketchFile() to get a reference to the file itself and get the name from there:
String path = sketchPath();
File file = sketchFile(path);
String sketchName = file.getName();
println(sketchName);
You could combine this all into one line like so:
String sketchName = sketchFile(sketchPath()).getName();

VBSCRIPT REPLACE not removing spaces from Decrypted fields

Got quite a head-scratcher....
I'm using the VBScript function REPLACE to replace spaces in a decrypted field from a MSSQL DB with "/".
But the REPLACE function isn't "seeing" the spaces.
For example, if I run any one of the following, where the decrypted value of the field "ITF_U_ClientName_Denc" is "Johnny Carson":
REPLACE(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc")," ","/")
REPLACE(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc")," ","/")
REPLACE(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc"),"Chr(160)","/")
REPLACE(CSTR(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc"))," ","/")
REPLACE(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc")," ","/",1,-1,1)
REPLACE(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc")," ","/",1,-1,0)
The returned value is "Johnny Carson" (space not replaced with /)
The issue seems to be exclusively with spaces, because when I run this:
REPLACE(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc"),"a","/")
I get "Johnny C/rson".
Also, the issue seems to be exclusively with spaces in the decrypted value, because when I run this:
REPLACE("Johnny Carson"," ","/")
Of course, the returned value is "Johnny/Carson".
I have checked what is being written to the source of the page and it is simply "Johnny Carson" with no encoding or special characters.
I have also tried the SPLIT function to see if it would "see" the space, but it doesn't.
Finally, thanks to a helpful comment, I tried VBS REGEX searching for \s.
Set regExp = New RegExp
regExp.IgnoreCase = True
regExp.Global = True
regExp.Pattern = "\s" 'Add here every character you don't consider as special character
strProcessed = regExp.Replace(ITF_U_Ledger.Fields("ITF_U_ClientName_Denc"), "?")
Unfortunately, strProcessed retruns "Johnny Carson" (ie. spaces not detected/removed).
If I replace regExp.Pattern = "a", strProcessed returns "Johnny C?rson".
Many thanks for your help!!
As we found, the right character code is 160, and that did the trick:
replace(..., ChrW(160), "...")
This seems to be data specific and, additionally, as an alternative you can try to get same encoding of the source script (i.e. save with Save As with Encoding), or convert received database value into a different target encoding.

MQTT payload from script

I have this code for compose and send MQTT payload.
snprintf_P(mqtt_data,
sizeof(mqtt_data),
PSTR("{\"" D_JSON_SYNC "\":%d,\"" D_JSON_LOW "\":%d,\"" D_JSON_HIGH "\":%d,\"" D_JSON_DATA "\":\"%06X\",\"" D_CMND_RFKEY "\":%s, \"" D_TOPIC "\":%s}"),
sync_time,
low_time,
high_time,
received_id,
rfkey,
Settings.mqtt_topic);
MqttPublishPrefixTopic_P(6, PSTR(D_RFRECEIVED));
You can see "Settings.mqtt_topic" is the value for "topic" included in message.
this is mesage received
{"Sync":12230,"Low":390,"High":1190,"Data":"596F91","RfKey":1, "Topic":10101019}
And I have this another code for MQTT payload.
snprintf_P(mqtt_data,
sizeof(mqtt_data),
S_JSON_COMMAND_INDEX_SVALUE,
D_CMND_RFKEY,
sonoff_bridge_learn_key,
D_LEARNED);
MqttPublishPrefixTopic_P(5, PSTR(D_CMND_RFKEY));
this is message received
{"Rfkey1":Learned}
Can somebody give me an approach to get "topic" included in the payload in the second code?
I need to receive message like this
{"Rfkey1":Learned, "Topic":10101019}
I have tried several ways without success.
I tried something like this
snprintf_P(mqtt_data,
sizeof(mqtt_data),
S_JSON_COMMAND_INDEX_SVALUE,
D_CMND_RFKEY,
D_TOPIC,
sonoff_bridge_learn_key,
D_LEARNED,
Settings.mqtt_topic);
This looks to be just using snprintf (doc here)
This takes 3 initial arguments
Output buffer
Size of the output buffer
A format string to use to build the output
The arguments following these 3 are the values to be inserted into the format string. You can not just add extra values on the end without updating the format string with where to put them and what format they should take.
You have not included the format string for your second example so I can't suggest how to edit it.

Replacing text after a specific pattern in C

I have a char buffer which will be of the following format.
Somecontent
.
.
Content-Length: 1570
MoreContent
.
.
EndofContent
I want to replace The more content part with something else say Xyz . But now I need to change content-length value to the new length of xyz. Also more content has to be replaced by xyz.
I have been able to extract the content below content-length and modify it, but placing it back into the original buffer has been an issue for me.
The output should be:
Somecontent
.
.
Content-Length: 100
xyz
I tried using strstr to find location of content-length, and use memcpy to copy contents before that, but it hasn;t worked. Can anyone suggest a method. The buffer is in char *buf;
I assume that, when you replace "More content" with "xyz", you're replacing, the end of "xyz" is the new "End of content". Correct? I'm also assuming the new "xyz" cannot overflow your buffer, correct?
In that case:
1) Search for "Content length". Save the offset (for example, variable "ofs1").
2) Search for the start of "More content". Save the offset (for example, variable "ofs2").
3) If you don't already know the new Content-Length, you can easily compute it as length(More Content) - Length(xyz)
4) Copy xyz over More content (e.g. memcpy)
5) Update "Content-Length". Blank-pad, to keep the same #/digits.
6) Voila! Done.

How do I get a temporary File object (of correct content-type, without writing to disk) directly from a ZipEntry (RubyZip, Paperclip, Rails 3)?

I'm currently trying to attach image files to a model directly from a zip file (i.e. without first saving them on a disk). It seems like there should be a clearer way of converting a ZipEntry to a Tempfile or File that can be stored in memory to be passed to another method or object that knows what to do with it.
Here's my code:
def extract (file = nil)
Zip::ZipFile.open(file) { |zip_file|
zip_file.each { |image|
photo = self.photos.build
# photo.image = image # this doesn't work
# photo.image = File.open image # also doesn't work
# photo.image = File.new image.filename
photo.save
}
}
end
But the problem is that photo.image is an attachment (via paperclip) to the model, and assigning something as an attachment requires that something to be a File object. However, I cannot for the life of me figure out how to convert a ZipEntry to a File. The only way I've seen of opening or creating a File is to use a string to its path - meaning I have to extract the file to a location. Really, that just seems silly. Why can't I just extract the ZipEntry file to the output stream and convert it to a File there?
So the ultimate question: Can I extract a ZipEntry from a Zip file and turn it directly into a File object (or attach it directly as a Paperclip object)? Or am I stuck actually storing it on the hard drive before I can attach it, even though that version will be deleted in the end?
UPDATE
Thanks to blueberry fields, I think I'm a little closer to my solution. Here's the line of code that I added, and it gives me the Tempfile/File that I need:
photo.image = zip_file.get_output_stream image
However, my Photo object won't accept the file that's getting passed, since it's not an image/jpeg. In fact, checking the content_type of the file shows application/x-empty. I think this may be because getting the output stream seems to append a timestamp to the end of the file, so that it ends up looking like imagename.jpg20110203-20203-hukq0n. Edit: Also, the tempfile that it creates doesn't contain any data and is of size 0. So it's looking like this might not be the answer.
So, next question: does anyone know how to get this to give me an image/jpeg file?
UPDATE:
I've been playing around with this some more. It seems output stream is not the way to go, but rather an input stream (which is which has always kind of confused me). Using get_input_stream on the ZipEntry, I get the binary data in the file. I think now I just need to figure out how to get this into a Paperclip attachment (as a File object). I've tried pushing the ZipInputStream directly to the attachment, but of course, that doesn't work. I really find it hard to believe that no one has tried to cast an extracted ZipEntry as a File. Is there some reason that this would be considered bad programming practice? It seems to me like skipping the disk write for a temp file would be perfectly acceptable and supported in something like Zip archive management.
Anyway, the question still stands:
Is there a way of converting an Input Stream to a File object (or Tempfile)? Preferably without having to write to a disk.
Try this
Zip::ZipFile.open(params[:avatar].path) do |zipfile|
zipfile.each do |entry|
filename = entry.name
basename = File.basename(filename)
tempfile = Tempfile.new(basename)
tempfile.binmode
tempfile.write entry.get_input_stream.read
user = User.new
user.avatar = {
:tempfile => tempfile,
:filename => filename
}
user.save
end
end
Check out the get_input_stream and get_output_stream messages on ZipFile.

Resources