dorfmt.c 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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
  11. * ANY REPRESENTATION OR WARRANTY OF ANY KIND CONCERNING THE MERCHANTABILITY
  12. * OF THIS SOFTWARE OR ITS FITNESS FOR ANY PARTICULAR PURPOSE.
  13. */
  14. #include <u.h>
  15. #include <libc.h>
  16. #include "fmtdef.h"
  17. /* format the output into f->to and return the number of characters fmted */
  18. int
  19. dorfmt(Fmt *f, const Rune *fmt)
  20. {
  21. Rune *rt, *rs;
  22. int r;
  23. char *t, *s;
  24. int nfmt;
  25. nfmt = f->nfmt;
  26. for(;;){
  27. if(f->runes){
  28. rt = f->to;
  29. rs = f->stop;
  30. while((r = *fmt++) && r != '%'){
  31. FMTRCHAR(f, rt, rs, r);
  32. }
  33. f->nfmt += rt - (Rune *)f->to;
  34. f->to = rt;
  35. if(!r)
  36. return f->nfmt - nfmt;
  37. f->stop = rs;
  38. }else{
  39. t = f->to;
  40. s = f->stop;
  41. while((r = *fmt++) && r != '%'){
  42. FMTRUNE(f, t, f->stop, r);
  43. }
  44. f->nfmt += t - (char *)f->to;
  45. f->to = t;
  46. if(!r)
  47. return f->nfmt - nfmt;
  48. f->stop = s;
  49. }
  50. fmt = __fmtdispatch(f, (Rune*)fmt, 1);
  51. if(fmt == nil)
  52. return -1;
  53. }
  54. return 0; /* not reached */
  55. }