write.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /* $Id: write.c,v 1.13 2001/04/30 16:02:07 kilobug Exp $ */
  2. #include "net_private.h"
  3. static void net_write_type(FILE *file, net_type_t type)
  4. {
  5. unsigned char c;
  6. if (file == NULL)
  7. return;
  8. c = type;
  9. #ifdef __DEBUG_NETLIB__
  10. fprintf(stderr, "Writing type: %#x\n", c);
  11. #endif
  12. fwrite(&c, 1, 1, file);
  13. }
  14. void net_wr_string(FILE *file, const char *s)
  15. {
  16. int l;
  17. if (file == NULL)
  18. return;
  19. net_write_type(file, net_type_str);
  20. #ifdef __DEBUG_NETLIB__
  21. fprintf(stderr, "Sending string: %s\n", s);
  22. #endif
  23. if (s == NULL)
  24. l = 0;
  25. else
  26. l = strlen(s);
  27. l = g_htonl(l);
  28. fwrite(&l, 4, 1, file);;
  29. l = g_ntohl(l);
  30. fwrite(s, 1, l, file);;
  31. }
  32. void net_wr_int(FILE *file, int val)
  33. {
  34. int l;
  35. if (file == NULL)
  36. return;
  37. net_write_type(file, net_type_int);
  38. #ifdef __DEBUG_NETLIB__
  39. fprintf(stderr, "Sending int: %d\n", val);
  40. #endif
  41. l = g_htonl(val);
  42. fwrite(&l, 4, 1, file);;
  43. }
  44. void net_wr_flag(FILE *file, gboolean val)
  45. {
  46. if (file == NULL)
  47. return;
  48. net_wr_char(file, val);
  49. }
  50. void net_wr_char(FILE *file, char c)
  51. {
  52. if (file == NULL)
  53. return;
  54. net_write_type(file, net_type_char);
  55. #ifdef __DEBUG_NETLIB__
  56. fprintf(stderr, "Sending char: %d\n", c);
  57. #endif
  58. fwrite(&c, 1, 1, file);
  59. }
  60. void net_wr_float(FILE *file, float f)
  61. {
  62. if (file == NULL)
  63. return;
  64. net_write_type(file, net_type_float);
  65. #ifdef __DEBUG_NETLIB__
  66. fprintf(stderr, "Sending float: %f\n", f);
  67. #endif
  68. fwrite(&f, 4, 1, file);
  69. }