Mercurial > hg > nnchat
comparison libnnchat.c @ 89:c2d916b340bf
Change some typedef names; Add struct for user list handling.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Wed, 06 May 2009 05:05:02 +0300 |
parents | e36df57c5b0f |
children | 1e0bf7b4fd41 |
comparison
equal
deleted
inserted
replaced
88:7bf0915c965e | 89:c2d916b340bf |
---|---|
363 | 363 |
364 return sendToSocket(sock, tmpBuf2, strlen(tmpBuf2) + 1); | 364 return sendToSocket(sock, tmpBuf2, strlen(tmpBuf2) + 1); |
365 } | 365 } |
366 | 366 |
367 | 367 |
368 ringbuf_t * newRingBuf(const size_t size) | 368 nn_ringbuf_t * newRingBuf(const size_t size) |
369 { | 369 { |
370 ringbuf_t *res = th_calloc(1, sizeof(ringbuf_t)); | 370 nn_ringbuf_t *res = th_calloc(1, sizeof(nn_ringbuf_t)); |
371 | 371 |
372 res->data = (char **) th_malloc(size * sizeof(char *)); | 372 res->data = (char **) th_malloc(size * sizeof(char *)); |
373 res->size = size; | 373 res->size = size; |
374 res->n = 0; | 374 res->n = 0; |
375 | 375 |
376 return res; | 376 return res; |
377 } | 377 } |
378 | 378 |
379 | 379 |
380 void freeRingBuf(ringbuf_t *buf) | 380 void freeRingBuf(nn_ringbuf_t *buf) |
381 { | 381 { |
382 size_t i; | 382 size_t i; |
383 | 383 |
384 for (i = 0; i < buf->n; i++) | 384 for (i = 0; i < buf->n; i++) |
385 th_free(buf->data[i]); | 385 th_free(buf->data[i]); |
387 th_free(buf->data); | 387 th_free(buf->data); |
388 th_free(buf); | 388 th_free(buf); |
389 } | 389 } |
390 | 390 |
391 | 391 |
392 void addRingBuf(ringbuf_t *buf, const char *str) | 392 void addRingBuf(nn_ringbuf_t *buf, const char *str) |
393 { | 393 { |
394 if (buf->n < buf->size) { | 394 if (buf->n < buf->size) { |
395 buf->data[buf->n] = th_strdup(str); | 395 buf->data[buf->n] = th_strdup(str); |
396 buf->n++; | 396 buf->n++; |
397 } else { | 397 } else { |
400 buf->data[buf->size - 1] = th_strdup(str); | 400 buf->data[buf->size - 1] = th_strdup(str); |
401 } | 401 } |
402 } | 402 } |
403 | 403 |
404 | 404 |
405 int writeBuf(editbuf_t *buf, ssize_t pos, int ch) | 405 int writeBuf(nn_editbuf_t *buf, ssize_t pos, int ch) |
406 { | 406 { |
407 /* Check arguments */ | 407 /* Check arguments */ |
408 if (buf->len+1 >= buf->size) return -3; | 408 if (buf->len+1 >= buf->size) return -3; |
409 | 409 |
410 if (pos < 0) | 410 if (pos < 0) |
416 } | 416 } |
417 return 0; | 417 return 0; |
418 } | 418 } |
419 | 419 |
420 | 420 |
421 int insertBuf(editbuf_t *buf, ssize_t pos, int ch) | 421 int insertBuf(nn_editbuf_t *buf, ssize_t pos, int ch) |
422 { | 422 { |
423 /* Check arguments */ | 423 /* Check arguments */ |
424 if (buf->len+1 >= buf->size) return -3; | 424 if (buf->len+1 >= buf->size) return -3; |
425 | 425 |
426 if (pos < 0) | 426 if (pos < 0) |
434 buf->len++; | 434 buf->len++; |
435 return 0; | 435 return 0; |
436 } | 436 } |
437 | 437 |
438 | 438 |
439 int deleteBuf(editbuf_t *buf, ssize_t pos) | 439 int deleteBuf(nn_editbuf_t *buf, ssize_t pos) |
440 { | 440 { |
441 /* Check arguments */ | 441 /* Check arguments */ |
442 if (pos < 0) | 442 if (pos < 0) |
443 return -1; | 443 return -1; |
444 else if (pos < buf->len) { | 444 else if (pos < buf->len) { |
448 } else | 448 } else |
449 return -2; | 449 return -2; |
450 } | 450 } |
451 | 451 |
452 | 452 |
453 void clearBuf(editbuf_t *buf) | 453 void clearBuf(nn_editbuf_t *buf) |
454 { | 454 { |
455 buf->len = 0; | 455 buf->len = 0; |
456 buf->pos = 0; | 456 buf->pos = 0; |
457 } | 457 } |
458 | 458 |
459 | 459 |
460 editbuf_t * newBuf(ssize_t n) | 460 nn_editbuf_t * newBuf(ssize_t n) |
461 { | 461 { |
462 editbuf_t *res = th_calloc(1, sizeof(editbuf_t)); | 462 nn_editbuf_t *res = th_calloc(1, sizeof(nn_editbuf_t)); |
463 | 463 |
464 res->data = (char *) th_malloc(n); | 464 res->data = (char *) th_malloc(n); |
465 res->size = n; | 465 res->size = n; |
466 | 466 |
467 return res; | 467 return res; |
468 } | 468 } |
469 | 469 |
470 | 470 |
471 void freeBuf(editbuf_t *buf) | 471 void freeBuf(nn_editbuf_t *buf) |
472 { | 472 { |
473 if (buf) { | 473 if (buf) { |
474 th_free(buf->data); | 474 th_free(buf->data); |
475 th_free(buf); | 475 th_free(buf); |
476 } | 476 } |
477 } | 477 } |
478 | 478 |
479 | 479 |
480 editbuf_t * copyBuf(editbuf_t *src) | 480 nn_editbuf_t * copyBuf(nn_editbuf_t *src) |
481 { | 481 { |
482 editbuf_t *res; | 482 nn_editbuf_t *res; |
483 | 483 |
484 assert(src != NULL); | 484 assert(src != NULL); |
485 | 485 |
486 if (src == NULL) return NULL; | 486 if (src == NULL) return NULL; |
487 | 487 |
493 | 493 |
494 return res; | 494 return res; |
495 } | 495 } |
496 | 496 |
497 | 497 |
498 void setBufPos(editbuf_t *buf, ssize_t pos) | 498 void setBufPos(nn_editbuf_t *buf, ssize_t pos) |
499 { | 499 { |
500 /* Check arguments */ | 500 /* Check arguments */ |
501 if (pos < 0) | 501 if (pos < 0) |
502 buf->pos = 0; | 502 buf->pos = 0; |
503 else if (pos >= buf->len) | 503 else if (pos >= buf->len) |