📄 utils.c
字号:
rc++;
cp++;
}
}
}
return rc;
}
/*!
* \brief Send complete string via TCP.
*
* The low level NutTcpSend() routine limits the number of bytes to
* the maximum segment size of the connection. This routine makes
* sure, that the complete string is transmitted.
*
* \param sock Socket descriptor. This pointer must have been retrieved
* by calling NutTcpCreateSocket(). In addition a connection
* must have been established by calling NutTcpConnect() or
* NutTcpAccept().
* \param str Pointer to a buffer containing the string to send.
*
* \return 0 on success. Otherwise -1 is returned.
*/
int TcpPutString(TCPSOCKET * sock, char * str)
{
int len = (int)strlen(str);
int c;
while(len) {
if ((c = NutTcpSend(sock, str, (u_short)len)) <= 0) {
break;
}
len -= c;
str += c;
}
return len ? -1 : 0;
}
/*!
* \brief Get header line array from TCP socket connection.
*
* Receives lines from a TCP stream until the first empty line appears
* and stores all lines in an array of strings. The empty line is not
* stored, but the last pointer of the array is set to NULL.
*
* The routine allocates the array and all string. The caller should use
* TcpReleaseHeaderLines() to release all allocated memory.
*
* \param sock Socket descriptor. This pointer must have been retrieved
* by calling NutTcpCreateSocket(). In addition a connection
* must have been established by calling NutTcpConnect() or
* NutTcpAccept().
* \param array Pointer to a string array pointer, which receives the
* address of the newly allocated string array. If no
* non-empty lines were received or if an error occured,
* then the string array pointer will be set to NULL.
*
* \return The number of header lines stored. In case of an error, the
* function result is -1 instead.
*/
int TcpGetHeaderLines(TCPSOCKET * sock, char ***array)
{
int rc = -1;
struct LILI_ {
struct LILI_ *ll_next;
char *ll_line;
};
struct LILI_ *root = NULL;
struct LILI_ *link = NULL;
struct LILI_ **next = &root;
char *buf;
int len;
/*
* Allocate a line buffer. On success, build a linked list of
* incoming lines. Stop on errors or at the first empty line.
*/
if ((buf = malloc(MAX_HEADERLINE_SIZE)) != NULL) {
for (;;) {
if ((len = TcpGetLine(sock, buf, MAX_HEADERLINE_SIZE)) == 0) {
/* Empty line. */
break;
}
if (len < 0) {
/* Error occured. */
rc = -1;
break;
}
/* Allocate a linked list item. */
if ((*next = malloc(sizeof(struct LILI_))) == NULL) {
break;
}
/* Initially set the next link to NULL. */
(*next)->ll_next = NULL;
/* Copy the line to the linked list item. */
(*next)->ll_line = malloc(len + 1);
memcpy((*next)->ll_line, buf, len);
(*next)->ll_line[len] = '\0';
/* Set pointer to the next link. */
next = &((*next)->ll_next);
rc++;
}
free(buf);
}
if (rc > 0) {
/* Allocate a new string array. */
*array = malloc((rc + 1) * sizeof(char *));
rc = 0;
}
else {
/* Error or single empty line only. Set array pointer to NULL. */
*array = NULL;
}
/*
* This loop serves two purposes. It moves the strings from the
* linked list to the string array (if one had been allocated)
* and it releases all memory allocated for the linked list.
*/
while (root) {
if (*array) {
/* Move string to the array. */
(*array)[rc] = root->ll_line;
rc++;
}
else {
/* No array. Just release the string. */
free(root->ll_line);
}
/* Get the next link and release the current entry. */
link = root;
root = root->ll_next;
free(link);
}
/* NULL marks the end of the array. */
if (*array) {
(*array)[rc] = NULL;
}
return rc;
}
/*!
* \brief Release previously allocated memory for the header lines.
*
* Should be called to release all memory allocated by TcpGetHeaderLines().
*
* \param array String array to be released.
*/
void TcpReleaseHeaderLines(char **array)
{
char **ap = array;
if (ap) {
/* Thanks, Dany. */
while (*ap) {
free(*ap);
ap++;
}
free(array);
}
}
void HttpSchemeRelease(HTTP_SCHEME *schm)
{
if (schm) {
if (schm->schm_uri) {
free(schm->schm_uri);
}
free(schm);
}
}
/* [<user>[:<password>]@]<host>[:<port>][/<path>] */
HTTP_SCHEME *HttpSchemeParse(CONST char *uri)
{
HTTP_SCHEME *schm = NULL;
char *cp;
/* Create a blank scheme structure. */
if (*uri && (schm = malloc(sizeof(HTTP_SCHEME))) != NULL) {
memset(schm, 0, sizeof(HTTP_SCHEME));
/* Create a local copy of the URI string. */
if ((schm->schm_uri = strdup(uri)) != NULL) {
/* Split the local copy. */
schm->schm_host = schm->schm_uri;
for (cp = schm->schm_uri; *cp; cp++) {
if (*cp == ':') {
*cp = '\0';
schm->schm_port = cp + 1;
}
else if (*cp == '/') {
*cp = 0;
schm->schm_path = cp + 1;
break;
}
else if (*cp == '@') {
*cp = 0;
schm->schm_user = schm->schm_host;
schm->schm_pass = schm->schm_port;
schm->schm_host = cp + 1;
schm->schm_port = NULL;
}
}
if (schm->schm_port) {
schm->schm_portnum = (u_short)atoi(schm->schm_port);
}
else {
schm->schm_portnum = 80;
}
return schm;
}
}
HttpSchemeRelease(schm);
return NULL;
}
/*!
* \brief Switch LED0 on or off.
*
* On the SAM7X-EK the LED labeled DS4 is used.
*
* On the SAM9260-EK we use the only one user LED.
*
* \param on If 0, then the LED will be switched off. Otherwise it will be lit.
*/
void Led0(int on)
{
#ifdef AT91SAM7X_EK
outr(PIOB_PER, _BV(22));
outr(PIOB_OER, _BV(22));
if (on) {
outr(PIOB_CODR, _BV(22));
}
else {
outr(PIOB_SODR, _BV(22));
}
#elif AT91SAM9260_EK
outr(PIOA_PER, _BV(6));
outr(PIOA_OER, _BV(6));
if (on) {
outr(PIOA_CODR, _BV(6));
}
else {
outr(PIOA_SODR, _BV(6));
}
#endif
}
/*!
* \brief Create a copy of a string.
*
* Allocates sufficient memory from heap for a copy of the string
* does the copy.
*
* Missing Nut/OS function.
*
* \param str Pointer to the string to copy.
*
* \return A pointer to the new string or NULL if allocating memory failed.
*/
char *strdup(CONST char *str)
{
size_t siz;
char *copy;
siz = strlen(str) + 1;
if ((copy = malloc(siz)) == NULL) {
return NULL;
}
memcpy(copy, str, siz);
return copy;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -