getfields.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #define _RESEARCH_SOURCE
  2. #include <libv.h>
  3. #include <string.h>
  4. static char is_sep[256] = {
  5. 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  6. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  7. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  8. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  9. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  10. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  11. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  12. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  13. };
  14. static char is_field[256] = {
  15. 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  16. 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  17. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  18. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  19. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  20. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  21. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  22. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  23. };
  24. static char last_sep[256];
  25. char *
  26. setfields(char *arg)
  27. {
  28. register unsigned char *s;
  29. register i;
  30. for(i = 1, s = (unsigned char *)last_sep; i < 256; i++)
  31. if(is_sep[i])
  32. *s++ = i;
  33. *s = 0;
  34. memset(is_sep, 0, sizeof is_sep);
  35. memset(is_field, 1, sizeof is_field);
  36. for(s = (unsigned char *)arg; *s;){
  37. is_sep[*s] = 1;
  38. is_field[*s++] = 0;
  39. }
  40. is_field[0] = 0;
  41. return(last_sep);
  42. }
  43. int
  44. getfields(char *ss, char **sp, int nptrs)
  45. {
  46. register unsigned char *s = (unsigned char *)ss;
  47. register unsigned char **p = (unsigned char **)sp;
  48. register unsigned c;
  49. for(;;){
  50. if(--nptrs < 0) break;
  51. *p++ = s;
  52. while(is_field[c = *s++])
  53. ;
  54. if(c == 0) break;
  55. s[-1] = 0;
  56. }
  57. if(nptrs > 0)
  58. *p = 0;
  59. else if(--s >= (unsigned char *)ss)
  60. *s = c;
  61. return(p - (unsigned char **)sp);
  62. }
  63. int
  64. getmfields(char *ss, char **sp, int nptrs)
  65. {
  66. register unsigned char *s = (unsigned char *)ss;
  67. register unsigned char **p = (unsigned char **)sp;
  68. register unsigned c;
  69. if(nptrs <= 0)
  70. return(0);
  71. goto flushdelim;
  72. for(;;){
  73. *p++ = s;
  74. if(--nptrs == 0) break;
  75. while(is_field[c = *s++])
  76. ;
  77. /*
  78. * s is now pointing 1 past the delimiter of the last field
  79. * c is the delimiter
  80. */
  81. if(c == 0) break;
  82. s[-1] = 0;
  83. flushdelim:
  84. while(is_sep[c = *s++])
  85. ;
  86. /*
  87. * s is now pointing 1 past the beginning of the next field
  88. * c is the first letter of the field
  89. */
  90. if(c == 0) break;
  91. s--;
  92. /*
  93. * s is now pointing to the beginning of the next field
  94. * c is the first letter of the field
  95. */
  96. }
  97. if(nptrs > 0)
  98. *p = 0;
  99. return(p - (unsigned char **)sp);
  100. }
  101. #ifdef MAIN
  102. #include <fio.h>
  103. main()
  104. {
  105. char *fields[256];
  106. char *s;
  107. int n, i;
  108. char buf[1024];
  109. print("go:\n");
  110. while(s = Frdline(0)){
  111. strcpy(buf, s);
  112. Fprint(1, "getf:");
  113. n = getfields(s, fields, 4);
  114. for(i = 0; i < n; i++)
  115. Fprint(1, " >%s<", fields[i]);
  116. Fputc(1, '\n');
  117. Fprint(1, "getmf:");
  118. n = getmfields(buf, fields, 4);
  119. for(i = 0; i < n; i++)
  120. Fprint(1, " >%s<", fields[i]);
  121. Fputc(1, '\n');
  122. Fflush(1);
  123. }
  124. exit(0);
  125. }
  126. #endif