Mercurial > hg > nnchat
comparison libnnchat.c @ 396:07a46ca075ab
Add some new functions for socket / connection input buffer
handling/parsing.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 24 May 2012 04:58:43 +0300 |
parents | 74d97581dc46 |
children | 7bec02f382fb |
comparison
equal
deleted
inserted
replaced
395:74d97581dc46 | 396:07a46ca075ab |
---|---|
443 void nn_network_close(void) | 443 void nn_network_close(void) |
444 { | 444 { |
445 #ifdef __WIN32 | 445 #ifdef __WIN32 |
446 WSACleanup(); | 446 WSACleanup(); |
447 #endif | 447 #endif |
448 } | |
449 | |
450 | |
451 BOOL nn_conn_buf_check(nn_conn_t *conn, size_t n) | |
452 { | |
453 return conn && conn->ptr && conn->in_ptr && (conn->ptr + n <= conn->in_ptr); | |
454 } | |
455 | |
456 | |
457 BOOL nn_conn_buf_skip(nn_conn_t *conn, size_t n) | |
458 { | |
459 if (nn_conn_buf_check(conn, n)) | |
460 { | |
461 conn->ptr += n; | |
462 return TRUE; | |
463 } | |
464 else | |
465 return FALSE; | |
466 } | |
467 | |
468 | |
469 int nn_conn_buf_strncmp(nn_conn_t *conn, const char *str, const size_t n) | |
470 { | |
471 int ret; | |
472 if (!nn_conn_buf_check(conn, n)) | |
473 return -1; | |
474 | |
475 if ((ret = strncmp(conn->ptr, str, n)) == 0) | |
476 { | |
477 conn->ptr += n; | |
478 return 0; | |
479 } | |
480 else | |
481 return ret; | |
482 } | |
483 | |
484 | |
485 int nn_conn_buf_strcmp(nn_conn_t *conn, const char *str) | |
486 { | |
487 return nn_conn_buf_strncmp(conn, str, strlen(str)); | |
488 } | |
489 | |
490 | |
491 char *nn_conn_buf_strstr(nn_conn_t *conn, const char *str) | |
492 { | |
493 char *pos; | |
494 size_t n = strlen(str); | |
495 | |
496 if (nn_conn_buf_check(conn, n) && ((pos = strstr(conn->ptr, str)) != NULL)) | |
497 { | |
498 conn->ptr = pos + n; | |
499 return pos; | |
500 } | |
501 else | |
502 return NULL; | |
448 } | 503 } |
449 | 504 |
450 | 505 |
451 #define PUSHCHAR(x) th_vputch(&result, &resSize, &resPos, x) | 506 #define PUSHCHAR(x) th_vputch(&result, &resSize, &resPos, x) |
452 #define PUSHSTR(x) th_vputs(&result, &resSize, &resPos, x) | 507 #define PUSHSTR(x) th_vputs(&result, &resSize, &resPos, x) |