File: next_perm.cc

package info (click to toggle)
erlang 1%3A12.b.3-dfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 77,780 kB
  • ctags: 157,528
  • sloc: erlang: 664,178; ansic: 241,119; makefile: 15,725; xml: 8,378; java: 7,780; sh: 6,789; lisp: 5,396; pascal: 3,637; perl: 2,310; asm: 1,438; cpp: 975; tcl: 245; python: 21; sed: 20; awk: 15
file content (137 lines) | stat: -rw-r--r-- 3,819 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* ``The contents of this file are subject to the Erlang Public License,
 * Version 1.1, (the "License"); you may not use this file except in
 * compliance with the License. You should have received a copy of the
 * Erlang Public License along with this software. If not, it can be
 * retrieved via the world wide web at http://www.erlang.org/.
 * 
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 * the License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Initial Developer of the Original Code is Ericsson Utvecklings AB.
 * Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
 * AB. All Rights Reserved.''
 * 
 *     $Id$
 */

/*
 * Purpose: A driver using libpq to connect to Postgres
 * from erlang, a sample for the driver documentation
 */

#include <erl_driver.h>

#include <algorithm>
#include <vector>

using namespace std;

#include <iostream>
#define L     cerr << __LINE__ << "\r\n";

/* Driver interface declarations */
static ErlDrvData start(ErlDrvPort port, char*);
static void output(ErlDrvData drv_data, char *buf, int len);
static void ready_async(ErlDrvData, ErlDrvThreadData);

static ErlDrvEntry next_perm_driver_entry = {
    NULL,			/* init */
    start,
    NULL, 			/* stop */
    output,			
    NULL,			/* ready_input */
    NULL,			/* ready_output */ 
    "next_perm",                /* the name of the driver */
    NULL,			/* finish */
    NULL,			/* handle */
    NULL,			/* control */
    NULL,			/* timeout */
    NULL,			/* outputv */
    ready_async,
    NULL,			/* flush */
    NULL,			/* call */
    NULL			/* event */
};

/* INITIALIZATION AFTER LOADING */

/* 
 * This is the init function called after this driver has been loaded.
 * It must *not* be declared static. Must return the address to 
 * the driver entry.
 */

#ifdef __cplusplus
extern "C" {		// shouldn't this be in the DRIVER_INIT macro?
#endif
DRIVER_INIT(next_perm)
{
    return &next_perm_driver_entry;
}
#ifdef __cplusplus
}
#endif

/* DRIVER INTERFACE */
static ErlDrvData start(ErlDrvPort port, char *)
{ 
    if (port == NULL)
	return ERL_DRV_ERROR_GENERAL;
    return (ErlDrvData)port;
}


struct our_async_data {
    bool prev;
    vector<int> data;
    our_async_data(ErlDrvPort p, int command, const char* buf, int len);
};

our_async_data::our_async_data(ErlDrvPort p, int command,
			       const char* buf, int len)
    : prev(command == 2),
      data((int*)buf, (int*)buf + len / sizeof(int))
{
}

static void do_perm(void* async_data);

static void output(ErlDrvData drv_data, char *buf, int len)
{
    if (*buf < 1 || *buf > 2) return;
    ErlDrvPort port = reinterpret_cast<ErlDrvPort>(drv_data);
    void* async_data = new our_async_data(port, *buf, buf+1, len);
    driver_async(port, NULL, do_perm, async_data, NULL);
}

static void do_perm(void* async_data)
{
    our_async_data* d = reinterpret_cast<our_async_data*>(async_data);
    if (d->prev)
	prev_permutation(d->data.begin(), d->data.end());
    else
	next_permutation(d->data.begin(), d->data.end());
}

static void ready_async(ErlDrvData drv_data, ErlDrvThreadData async_data)
{
    ErlDrvPort port = reinterpret_cast<ErlDrvPort>(drv_data);
    our_async_data* d = reinterpret_cast<our_async_data*>(async_data);
    int n = d->data.size(), result_n = n*2 + 5;
    ErlDrvTermData* result = new ErlDrvTermData[result_n], * rp = result;
    *rp++ = ERL_DRV_PORT;
    *rp++ = driver_mk_port(port);
    for (vector<int>::iterator i = d->data.begin();
	 i != d->data.end(); ++i) {
	*rp++ = ERL_DRV_INT;
	*rp++ = *i;
    }
    *rp++ = ERL_DRV_NIL;
    *rp++ = ERL_DRV_LIST;
    *rp++ = n+2;
    driver_output_term(port, result, result_n);    
    delete[] result;
    delete d;
}