Fixed indentation

This commit is contained in:
Maas-Maarten Zeeman
2012-10-28 08:14:21 +01:00
parent 768d4b198d
commit 6624096b93
4 changed files with 519 additions and 548 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -10,8 +10,8 @@
struct qitem_t struct qitem_t
{ {
struct qitem_t* next; struct qitem_t* next;
void* data; void* data;
}; };
typedef struct qitem_t qitem; typedef struct qitem_t qitem;
@@ -32,8 +32,7 @@ queue_create()
queue *ret; queue *ret;
ret = (queue *) enif_alloc(sizeof(struct queue_t)); ret = (queue *) enif_alloc(sizeof(struct queue_t));
if(ret == NULL) if(ret == NULL) goto error;
goto error;
ret->lock = NULL; ret->lock = NULL;
ret->cond = NULL; ret->cond = NULL;
@@ -43,22 +42,20 @@ queue_create()
ret->length = 0; ret->length = 0;
ret->lock = enif_mutex_create("queue_lock"); ret->lock = enif_mutex_create("queue_lock");
if(ret->lock == NULL) if(ret->lock == NULL) goto error;
goto error;
ret->cond = enif_cond_create("queue_cond"); ret->cond = enif_cond_create("queue_cond");
if(ret->cond == NULL) if(ret->cond == NULL) goto error;
goto error;
return ret; return ret;
error: error:
if(ret->lock != NULL) if(ret->lock != NULL)
enif_mutex_destroy(ret->lock); enif_mutex_destroy(ret->lock);
if(ret->cond != NULL) if(ret->cond != NULL)
enif_cond_destroy(ret->cond); enif_cond_destroy(ret->cond);
if(ret != NULL) if(ret != NULL)
enif_free(ret); enif_free(ret);
return NULL; return NULL;
} }
@@ -104,7 +101,7 @@ queue_push(queue *queue, void *item)
{ {
qitem * entry = (qitem *) enif_alloc(sizeof(struct qitem_t)); qitem * entry = (qitem *) enif_alloc(sizeof(struct qitem_t));
if(entry == NULL) if(entry == NULL)
return 0; return 0;
entry->data = item; entry->data = item;
entry->next = NULL; entry->next = NULL;
@@ -114,16 +111,12 @@ queue_push(queue *queue, void *item)
assert(queue->length >= 0 && "Invalid queue size at push"); assert(queue->length >= 0 && "Invalid queue size at push");
if(queue->tail != NULL) if(queue->tail != NULL)
{
queue->tail->next = entry; queue->tail->next = entry;
}
queue->tail = entry; queue->tail = entry;
if(queue->head == NULL) if(queue->head == NULL)
{
queue->head = queue->tail; queue->head = queue->tail;
}
queue->length += 1; queue->length += 1;
@@ -144,9 +137,7 @@ queue_pop(queue *queue)
/* Wait for an item to become available. /* Wait for an item to become available.
*/ */
while(queue->head == NULL) while(queue->head == NULL)
{
enif_cond_wait(queue->cond, queue->lock); enif_cond_wait(queue->cond, queue->lock);
}
assert(queue->length >= 0 && "Invalid queue size at pop."); assert(queue->length >= 0 && "Invalid queue size at pop.");
@@ -157,8 +148,7 @@ queue_pop(queue *queue)
queue->head = entry->next; queue->head = entry->next;
entry->next = NULL; entry->next = NULL;
if(queue->head == NULL) if(queue->head == NULL) {
{
assert(queue->tail == entry && "Invalid queue state: Bad tail."); assert(queue->tail == entry && "Invalid queue state: Bad tail.");
queue->tail = NULL; queue->tail = NULL;
} }
@@ -194,9 +184,7 @@ queue_receive(queue *queue)
/* Wait for an item to become available. /* Wait for an item to become available.
*/ */
while(queue->message == NULL) while(queue->message == NULL)
{
enif_cond_wait(queue->cond, queue->lock); enif_cond_wait(queue->cond, queue->lock);
}
item = queue->message; item = queue->message;
queue->message = NULL; queue->message = NULL;

View File

@@ -22,14 +22,14 @@
%% higher-level export %% higher-level export
-export([open/1, open/2, -export([open/1, open/2,
exec/2, exec/3, exec/2, exec/3,
prepare/2, prepare/3, prepare/2, prepare/3,
step/1, step/2, step/1, step/2,
bind/2, bind/3, bind/2, bind/3,
fetchone/1, fetchone/1,
fetchall/1, fetchall/1,
column_names/1, column_names/2, column_names/1, column_names/2,
close/1, close/2]). close/1, close/2]).
-export([q/2, q/3, map/3, foreach/3]). -export([q/2, q/3, map/3, foreach/3]).
@@ -50,10 +50,10 @@ open(Filename, Timeout) ->
Ref = make_ref(), Ref = make_ref(),
ok = esqlite3_nif:open(Connection, Ref, self(), Filename), ok = esqlite3_nif:open(Connection, Ref, self(), Filename),
case receive_answer(Ref, Timeout) of case receive_answer(Ref, Timeout) of
ok -> ok ->
{ok, Connection}; {ok, Connection};
Other -> Other ->
{error, Other} {error, Other}
end. end.
%% @doc Execute a sql statement, returns a list with tuples. %% @doc Execute a sql statement, returns a list with tuples.
@@ -82,50 +82,50 @@ foreach(F, Sql, Connection) ->
%% %%
foreach_s(F, Statement) when is_function(F, 1) -> foreach_s(F, Statement) when is_function(F, 1) ->
case try_step(Statement, 0) of case try_step(Statement, 0) of
'$done' -> ok; '$done' -> ok;
Row when is_tuple(Row) -> Row when is_tuple(Row) ->
F(Row), F(Row),
foreach_s(F, Statement) foreach_s(F, Statement)
end; end;
foreach_s(F, Statement) when is_function(F, 2) -> foreach_s(F, Statement) when is_function(F, 2) ->
ColumnNames = column_names(Statement), ColumnNames = column_names(Statement),
case try_step(Statement, 0) of case try_step(Statement, 0) of
'$done' -> ok; '$done' -> ok;
Row when is_tuple(Row) -> Row when is_tuple(Row) ->
F(ColumnNames, Row), F(ColumnNames, Row),
foreach_s(F, Statement) foreach_s(F, Statement)
end. end.
%% %%
map_s(F, Statement) when is_function(F, 1) -> map_s(F, Statement) when is_function(F, 1) ->
case try_step(Statement, 0) of case try_step(Statement, 0) of
'$done' -> []; '$done' -> [];
Row when is_tuple(Row) -> Row when is_tuple(Row) ->
[F(Row) | map_s(F, Statement)] [F(Row) | map_s(F, Statement)]
end; end;
map_s(F, Statement) when is_function(F, 2) -> map_s(F, Statement) when is_function(F, 2) ->
ColumnNames = column_names(Statement), ColumnNames = column_names(Statement),
case try_step(Statement, 0) of case try_step(Statement, 0) of
'$done' -> []; '$done' -> [];
Row when is_tuple(Row) -> Row when is_tuple(Row) ->
[F(ColumnNames, Row) | map_s(F, Statement)] [F(ColumnNames, Row) | map_s(F, Statement)]
end. end.
%% %%
fetchone(Statement) -> fetchone(Statement) ->
case try_step(Statement, 0) of case try_step(Statement, 0) of
'$done' -> ok; '$done' -> ok;
Row when is_tuple(Row) -> Row when is_tuple(Row) ->
Row Row
end. end.
%% %%
fetchall(Statement) -> fetchall(Statement) ->
case try_step(Statement, 0) of case try_step(Statement, 0) of
'$done' -> '$done' ->
[]; [];
Row when is_tuple(Row) -> Row when is_tuple(Row) ->
[Row | fetchall(Statement)] [Row | fetchall(Statement)]
end. end.
%% Try the step, when the database is busy, %% Try the step, when the database is busy,
@@ -133,11 +133,11 @@ try_step(_Statement, Tries) when Tries > 5 ->
throw(too_many_tries); throw(too_many_tries);
try_step(Statement, Tries) -> try_step(Statement, Tries) ->
case esqlite3:step(Statement) of case esqlite3:step(Statement) of
'$busy' -> '$busy' ->
timer:sleep(100 * Tries), timer:sleep(100 * Tries),
try_step(Statement, Tries + 1); try_step(Statement, Tries + 1);
Something -> Something ->
Something Something
end. end.
%% @doc Execute Sql statement, returns the number of affected rows. %% @doc Execute Sql statement, returns the number of affected rows.
@@ -226,12 +226,12 @@ add_eos(IoList) ->
receive_answer(Ref, Timeout) -> receive_answer(Ref, Timeout) ->
receive receive
{Ref, Resp} -> {Ref, Resp} ->
Resp; Resp;
Other -> Other ->
throw(Other) throw(Other)
after Timeout -> after Timeout ->
throw({error, timeout, Ref}) throw({error, timeout, Ref})
end. end.

View File

@@ -22,14 +22,14 @@
%% low-level exports %% low-level exports
-export([start/0, -export([start/0,
open/4, open/4,
exec/4, exec/4,
prepare/4, prepare/4,
step/3, step/3,
finalize/3, finalize/3,
bind/4, bind/4,
column_names/3, column_names/3,
close/3 close/3
]). ]).
-on_load(init/0). -on_load(init/0).