Guest User

Untitled

a guest
Apr 21st, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. #include "ruby.h"
  2.  
  3. #include <ldap.h>
  4.  
  5. static VALUE cRLdap;
  6.  
  7. static char* string_to_char(VALUE string)
  8. {
  9. VALUE str;
  10. char* p;
  11. int i;
  12. char *charstr;
  13. str = StringValue(string);
  14. p = RSTRING_PTR(str);
  15. charstr = ALLOC_N(char, RSTRING_LEN(str)+1);
  16. for(i = 0; i < RSTRING_LEN(str); i++, p++, charstr++) {
  17. *charstr = *p;
  18. }
  19. return charstr;
  20. }
  21.  
  22. static LDAP* l_get_ldap(VALUE klass)
  23. {
  24. LDAP* ldap;
  25. Data_Get_Struct(klass, LDAP, ldap);
  26. return ldap;
  27. }
  28.  
  29. static void l_free(void *p)
  30. {
  31. ldap_memfree(p);
  32. }
  33.  
  34. static VALUE l_alloc(VALUE klass)
  35. {
  36. LDAP* ldap;
  37. VALUE obj;
  38. ldap = ALLOC(LDAP);
  39. obj = Data_Wrap_Struct(klass, 0, l_free, ldap);
  40. return obj;
  41. }
  42.  
  43. static VALUE l_ldap_initialize(VALUE self, VALUE uri)
  44. {
  45. LDAP* ldap;
  46. int rv;
  47. ldap = l_get_ldap(self);
  48. rv = ldap_initialize(*ldap, string_to_char(uri));
  49. return INT2FIX(rv);
  50. }
  51.  
  52. void Init_RLdap() {
  53. cRLdap = rb_define_class("LDAP::C", rb_cObject);
  54.  
  55. }
Add Comment
Please, Sign In to add comment