runefmtstr.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /*
  2. * The authors of this software are Rob Pike and Ken Thompson.
  3. * Copyright (c) 2002 by Lucent Technologies.
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose without fee is hereby granted, provided that this entire notice
  6. * is included in all copies of any software which is or includes a copy
  7. * or modification of this software and in all copies of the supporting
  8. * documentation for such software.
  9. * THIS SOFTWARE IS BEING PROVIDED "AS IS", WITHOUT ANY EXPRESS OR IMPLIED
  10. * WARRANTY. IN PARTICULAR, NEITHER THE AUTHORS NOR LUCENT TECHNOLOGIES MAKE ANY
  11. * REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
  12. * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
  13. */
  14. #include <stdarg.h>
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include "utf.h"
  18. #include "fmt.h"
  19. #include "fmtdef.h"
  20. static int
  21. runeFmtStrFlush(Fmt *f)
  22. {
  23. Rune *s;
  24. int n;
  25. n = (int)f->farg;
  26. n += 256;
  27. f->farg = (void*)n;
  28. s = (Rune*)f->start;
  29. f->start = realloc(s, sizeof(Rune)*n);
  30. if(f->start == nil){
  31. f->start = s;
  32. return 0;
  33. }
  34. f->to = (Rune*)f->start + ((Rune*)f->to - s);
  35. f->stop = (Rune*)f->start + n - 1;
  36. return 1;
  37. }
  38. int
  39. runefmtstrinit(Fmt *f)
  40. {
  41. int n;
  42. f->runes = 1;
  43. n = 32;
  44. f->start = malloc(sizeof(Rune)*n);
  45. if(f->start == nil)
  46. return -1;
  47. f->to = f->start;
  48. f->stop = (Rune*)f->start + n - 1;
  49. f->flush = runeFmtStrFlush;
  50. f->farg = (void*)n;
  51. f->nfmt = 0;
  52. return 0;
  53. }
  54. Rune*
  55. runefmtstrflush(Fmt *f)
  56. {
  57. *(Rune*)f->to = '\0';
  58. f->to = f->start;
  59. return f->start;
  60. }