Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/argolab/argon
Browse files Browse the repository at this point in the history
  • Loading branch information
Norman Mo authored and Norman Mo committed Oct 12, 2012
2 parents b3f0fe0 + 8a23443 commit 55420e2
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 12 deletions.
3 changes: 0 additions & 3 deletions ext_module/README.md
Expand Up @@ -8,23 +8,20 @@ How to build ext_user.so
```bash

./python-32bit setup.py build

```

* Make a soft link of ext_user.so to the current directory.

```bash

ln -s ./build/lib-xxxxx/ext_user.so ext_user.so

```

* Start the interpreter:

```bash

./python-32bit

```

```python
Expand Down
25 changes: 25 additions & 0 deletions ext_module/test.py
@@ -0,0 +1,25 @@
#!/usr/bin/env python


def main_test():
import ext_user
a = ext_user.GetUserRec('PASSWDS', 0)
# print dir(a)
# for i in dir(a):
# if not i.startswith('__'):
# print i, str(getattr(a, i))
#
address = a.GetAddress().decode('gb2312')
realname = a.GetRealname().decode('gb2312')
username = a.GetRealname().decode('gb2312')
passwd = a.GetPasswd()

print 'address %s' % address
print 'realname %s' % realname
print 'username %s' % username
print 'passwd %s' % passwd
print len(passwd)

if __name__ == '__main__':
main_test()

54 changes: 45 additions & 9 deletions ext_module/user.c
Expand Up @@ -75,7 +75,44 @@ static PyMemberDef UserRecMember[] = {
{NULL},
};

static PyObject * UserRec_GetAddress(UserRec *self)
{
return PyString_FromStringAndSize(self->address, strlen(self->address));
}

static PyObject * UserRec_GetPasswd( UserRec *self )
{
// Need to transfer binary stream to hex representation
static const char ttb[] = "0123456789abcdef";

char buf[64];
int i;
for ( i = 0; i < sizeof(self->passwd); i++ )
{
buf[i*2] = ttb[self->passwd[i] >> 4];
buf[i*2+1] = ttb[self->passwd[i] & 15];
}
buf[ sizeof(self->passwd) * 2] = '\0';
return PyString_FromStringAndSize( buf, 2 * sizeof(self->passwd));
}

static PyObject * UserRec_GetUsername( UserRec *self )
{
return PyString_FromStringAndSize(self->username, strlen(self->username));
}

static PyObject * UserRec_GetRealname( UserRec *self )
{
return PyString_FromStringAndSize(self->realname, strlen(self->realname));
}

static PyMethodDef UserRecMethods[] = {
{ "GetPasswd", (PyCFunction)UserRec_GetPasswd, METH_NOARGS},
{ "GetAddress", (PyCFunction)UserRec_GetAddress, METH_NOARGS},
{ "GetUsername", (PyCFunction)UserRec_GetUsername, METH_NOARGS},
{ "GetRealname", (PyCFunction)UserRec_GetRealname, METH_NOARGS},
{NULL}
};


static PyTypeObject UserRecType= {
Expand Down Expand Up @@ -107,7 +144,7 @@ static PyTypeObject UserRecType= {
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
0, /* tp_methods */
UserRecMethods, /* tp_methods */
UserRecMember, /* tp_members */
0, /* tp_getset */
0, /* tp_base */
Expand All @@ -125,15 +162,8 @@ static PyObject* Convert2Object(struct userec *ptUrec)
UserRec * ptObj;
ptObj = (UserRec *)PyObject_New(UserRec, &UserRecType);

//printf( "sizeof(long) %ld\n" , sizeof(long));

memcpy(ptObj->userid, ptUrec->userid, sizeof( ptUrec->userid ));
ptObj->userid[4] = '\0';
ptObj->firstlogin = ptUrec->firstlogin;

//printf( "userid: %s\n", ptObj->userid );
//printf( "firstlogin: %ld\n", ptObj->firstlogin);

memcpy(ptObj->lasthost, ptUrec->lasthost, sizeof( ptUrec->lasthost ));
ptObj->numlogins = ptUrec->numlogins;
ptObj->numposts= ptUrec->numposts;
Expand All @@ -149,6 +179,11 @@ static PyObject* Convert2Object(struct userec *ptUrec)
ptObj->stay= ptUrec->stay;
memcpy(ptObj->realname, ptUrec->realname, sizeof( ptUrec->realname ));
memcpy(ptObj->address, ptUrec->address, sizeof( ptUrec->address ));

FILE *fp = fopen("address.txt", "w");
fprintf( fp, "%s\n", ptObj->address );
fclose(fp);

memcpy(ptObj->email, ptUrec->email, sizeof( ptUrec->email ));
ptObj->nummails= ptUrec->nummails;
ptObj->gender= ptUrec->gender;
Expand Down Expand Up @@ -198,7 +233,8 @@ static PyObject * user_GetUserRec(PyObject *self, PyObject *args)
}

static PyMethodDef module_methods[] = {
{ "GetUserRec", (PyCFunction)user_GetUserRec, METH_VARARGS },
{ "GetUserRec", (PyCFunction)user_GetUserRec, METH_VARARGS,
"The first argument is the .PASSWDS file. Second argument represent the num(order) of the record." },
{NULL}
};

Expand Down

0 comments on commit 55420e2

Please sign in to comment.