3 ms·
It's good. I fed it the urlparse function from here: https://github.com/chbarts/web/blob/master/web.c https://github.com/chbarts/web/blob/master/web.c And it
by msla 4y ago
It's good.
I fed it the urlparse function from here:
https://github.com/chbarts/web/blob/master/web.c https://github.com/chbarts/web/blob/master/web.c
And it came up with this:
https://whatdoesthiscodedo.com/g/e2a519c https://whatdoesthiscodedo.com/g/e2a519c
This is a C function that takes a URL as input and parses it to extract the host, path and port.
The function starts by initializing variables i and j. Then it checks if the length of the URL is less than 7 characters. If so, it sets i to 0. Otherwise, it checks if the URL starts with "http:// http://" using the strncmp function. If it does start with "http:// http://", then i is set to 7. Otherwise, i is set to 0.
The next block of code checks if i is 0 and if the URL contains "://". If this condition is true, then the function returns -1 indicating an error.
The next line sets the host variable to point to the string starting at position i in the URL.
The following loop iterates over the characters in the URL from position i until it reaches either a forward slash (/), a colon (:), or the end of the string. The switch statement then handles each case separately.
If the loop finds a forward slash, it replaces it with a null character (\0) and sets port to null, indicating that no port was specified in the URL. It then sets the path variable to point to the string immediately following the null character.
If the loop finds a colon, it replaces it with a null character and increments i. It then sets port to point to the string starting at position i in the URL. The loop then continues to iterate over the remaining characters in the URL until it reaches a forward slash or the end of the string. If it finds a forward slash, it replaces it with a null character and sets the path variable to point to the string immediately following the null character. Otherwise, it sets path to point to the end of the URL.
If the loop finds the end of the string, it sets *port to null and sets path to point to the end of the URL.
Finally, the function returns 0 to indicate success. The host, path, and port variables are passed as pointers, so they will be modified by the function and their values can be accessed outside of the function.
- msla 4y agoIt even does Common Lisp: Admittedly, this is ultimately from Rosetta Code, but it's somewhat modified: https://rosettacode.org/wiki/MAC_vendor_lookup#Common_Lisp https://rosettacode.org/wiki/MAC_vendor_lookup#Common_Lisp But I fed it this code: (defun mac-lookup (mac) (check-type mac string "A MAC address as a string") (multiple-value-bind (vendor status) (drakma:http-request (format nil "http://api.macvendors.com/~a" mac)) (if (= 200 status) (format t "~a~C~a~%" mac #\Tab vendor) (if (= 429 status) (format *error-output* "error: Too many requests at ~a~%" mac) (format *error-output* "error: ~a~%" mac))))) And it got this out of it: https://whatdoesthiscodedo.com/g/5ae8501 https://whatdoesthiscodedo.com/g/5ae8501 This code defines a function named "mac-lookup" that takes a MAC address as a string argument. It then checks the type of the argument to ensure it is a string. The function then sends an HTTP request to the API at http://api.macvendors.com http://api.macvendors.com with the provided MAC address appended to the URL. The response is stored in two variables, vendor and status, using the multiple-value-bind function. If the status code of the response is 200, the function outputs the MAC address, a tab character, and the vendor name to the console. If the status code is 429, which indicates too many requests have been made, the function outputs an error message to the error-output stream indicating that there were too many requests made for the provided MAC address. Otherwise, if any other status code is received, the function outputs a generic error message to the error-output stream indicating that there was an error for the provided MAC address.