indenting, + don't use men streams in Android

This commit is contained in:
vscosta 2016-04-05 02:18:47 +01:00
parent 636992781c
commit 396bd2f8c3
44 changed files with 241 additions and 252 deletions

407
C/blobs.c
View File

@ -9,10 +9,10 @@
#include <stdio.h>
#include <string.h>
#include "Yap.h"
#include "Yatom.h"
#include "Yap.h"
#include "Yatom.h"
#include "iopreds.h"
#include "yapio.h"
#include "yapio.h"
/* for freeBSD9.1 */
#define _WITH_DPRINTF
@ -21,98 +21,95 @@
#include "fmemopen.h"
#endif
#if __ANDROID__
#undef HAVE_FMEMOPEN
#undef HAVE_OPEN_MEMSTREAM
#endif
#include "blobs.h"
static blob_type_t unregistered_blob_atom =
{ YAP_BLOB_MAGIC_B,
PL_BLOB_NOCOPY|PL_BLOB_TEXT,
"unregistered"
};
static blob_type_t unregistered_blob_atom = {
YAP_BLOB_MAGIC_B, PL_BLOB_NOCOPY | PL_BLOB_TEXT, "unregistered"};
char * Yap_blob_to_string(AtomEntry *ref, const char *s0, size_t sz)
{
char *Yap_blob_to_string(AtomEntry *ref, const char *s0, size_t sz) {
// int rc;
char *s = (char *)s0;
// blob_type_t *type = RepBlobProp(ref->PropsOfAE)->blob_type;
#if HAVE_FMEMOPEN
if (type->write) {
FILE *f = fmemopen( s, sz, "w");
if (f == NULL){
// could not find stream;
return NULL;
char *s = (char *)s0;
// blob_type_t *type = RepBlobProp(ref->PropsOfAE)->blob_type;
#if HAVE_FMEMOPEN && !__ANDROID__
if (type->write) {
FILE *f = fmemopen(s, sz, "w");
if (f == NULL) {
// could not find stream;
return NULL;
}
Atom at = AbsAtom(ref);
rc = type->write(f, at, 0);
if (rc < 0) {
Yap_Error( EVALUATION_ERROR_UNDEFINED, MkAtomTerm(at), "failure in user-defined blob to string code" );
}
fclose(f); // return the final result.
if (rc < 0) {
Yap_Error(EVALUATION_ERROR_UNDEFINED, MkAtomTerm(at),
"failure in user-defined blob to string code");
}
fclose(f); // return the final result.
return s;
} else {
} else {
#endif
#if __APPLE__
size_t sz0 = strlcpy( s, (char *)RepAtom( AtomSWIStream )->StrOfAE, sz);
size_t sz0 = strlcpy(s, (char *)RepAtom(AtomSWIStream)->StrOfAE, sz);
#else
size_t sz0;
char *f = (char *)memcpy(s, (char *)RepAtom( AtomSWIStream )->StrOfAE, sz);
f[0]='\0';
sz0 = f-s;
size_t sz0;
char *f = (char *)memcpy(s, (char *)RepAtom(AtomSWIStream)->StrOfAE, sz);
f[0] = '\0';
sz0 = f - s;
#endif
s = s+sz0;
sz -= sz0;
s = s + sz0;
sz -= sz0;
#if defined(__linux__) || defined(__APPLE__)
snprintf(s+strlen(s), sz0, "(%p)", ref);
snprintf(s + strlen(s), sz0, "(%p)", ref);
#else
snprintf(s+strlen(s), sz0, "(0x%p)", ref);
snprintf(s + strlen(s), sz0, "(0x%p)", ref);
#endif
return s;
return s;
#if HAVE_FMEMOPEN
}
return NULL;
}
return NULL;
#endif
}
int Yap_write_blob(AtomEntry *ref, FILE *stream)
{
blob_type_t *type = RepBlobProp(ref->PropsOfAE)->blob_type;
if (type->write) {
Atom at = AbsAtom(ref);
return type->write(stream, at, 0);
} else {
int Yap_write_blob(AtomEntry *ref, FILE *stream) {
blob_type_t *type = RepBlobProp(ref->PropsOfAE)->blob_type;
if (type->write) {
Atom at = AbsAtom(ref);
return type->write(stream, at, 0);
} else {
#if defined(__linux__) || defined(__APPLE__)
return fprintf(stream, "\'%s\'(%p)", RepAtom(AtomSWIStream)->StrOfAE, ref);
return fprintf(stream, "\'%s\'(%p)", RepAtom(AtomSWIStream)->StrOfAE, ref);
#else
return fprintf(stream, "\'%s\'(0x%p)", RepAtom(AtomSWIStream)->StrOfAE, ref);
return fprintf(stream, "\'%s\'(0x%p)", RepAtom(AtomSWIStream)->StrOfAE,
ref);
#endif
}
return 0;
}
bool
YAP_is_blob(Term t, blob_type_t **type)
{
CACHE_REGS
Term yt = Yap_GetFromSlot(t);
Atom a;
YAP_BlobPropEntry *b;
if (IsVarTerm(yt))
return FALSE;
if (!IsAtomTerm(yt))
return FALSE;
a = AtomOfTerm(yt);
if (!IsBlob(a))
return FALSE;
b = RepBlobProp(a->PropsOfAE);
*type = b->blob_type;
return TRUE;
}
return 0;
}
bool YAP_is_blob(Term t, blob_type_t **type) {
CACHE_REGS
Term yt = Yap_GetFromSlot(t);
Atom a;
YAP_BlobPropEntry *b;
if (IsVarTerm(yt))
return FALSE;
if (!IsAtomTerm(yt))
return FALSE;
a = AtomOfTerm(yt);
if (!IsBlob(a))
return FALSE;
b = RepBlobProp(a->PropsOfAE);
*type = b->blob_type;
return TRUE;
}
/* void check_chain(void); */
@ -126,161 +123,141 @@ YAP_is_blob(Term t, blob_type_t **type)
/* } */
/* } */
AtomEntry *
Yap_lookupBlob(void *blob, size_t len, void *type0, int *new)
{
YAP_BlobPropEntry *b;
AtomEntry *ae;
blob_type_t *type = type0;
if (new)
*new = FALSE;
LOCK(Blobs_Lock);
if (type->flags & PL_BLOB_UNIQUE) {
/* just keep a linked chain for now */
ae = Blobs;
while (ae) {
if (ae->PropsOfAE &&
RepBlobProp(ae->PropsOfAE)->blob_type == type &&
ae->rep.blob->length == len &&
!memcmp(ae->rep.blob->data, blob, len)) {
UNLOCK(Blobs_Lock);
return ae;
}
ae = RepAtom(ae->NextOfAE);
}
}
if (new)
*new = TRUE;
b = (YAP_BlobPropEntry *)Yap_AllocCodeSpace(sizeof(YAP_BlobPropEntry));
if (!b) {
AtomEntry *Yap_lookupBlob(void *blob, size_t len, void *type0, int *new) {
YAP_BlobPropEntry *b;
AtomEntry *ae;
blob_type_t *type = type0;
if (new)
*new = FALSE;
LOCK(Blobs_Lock);
if (type->flags & PL_BLOB_UNIQUE) {
/* just keep a linked chain for now */
ae = Blobs;
while (ae) {
if (ae->PropsOfAE && RepBlobProp(ae->PropsOfAE)->blob_type == type &&
ae->rep.blob->length == len &&
!memcmp(ae->rep.blob->data, blob, len)) {
UNLOCK(Blobs_Lock);
return NULL;
}
b->NextOfPE = NIL;
b->KindOfPE = BlobProperty;
b->blob_type = type;
ae = (AtomEntry *)Yap_AllocCodeSpace(sizeof(AtomEntry)+len+sizeof(size_t));
if (!ae) {
UNLOCK(Blobs_Lock);
return NULL;
}
NOfBlobs++;
INIT_RWLOCK(ae->ARWLock);
ae->PropsOfAE = AbsBlobProp(b);
ae->NextOfAE = AbsAtom(Blobs);
ae->rep.blob->length = len;
memcpy(ae->rep.blob->data, blob, len);
Blobs = ae;
if (NOfBlobs > NOfBlobsMax) {
Yap_signal(YAP_CDOVF_SIGNAL);
return ae;
}
ae = RepAtom(ae->NextOfAE);
}
}
if (new)
*new = TRUE;
b = (YAP_BlobPropEntry *)Yap_AllocCodeSpace(sizeof(YAP_BlobPropEntry));
if (!b) {
UNLOCK(Blobs_Lock);
return ae;
return NULL;
}
b->NextOfPE = NIL;
b->KindOfPE = BlobProperty;
b->blob_type = type;
ae =
(AtomEntry *)Yap_AllocCodeSpace(sizeof(AtomEntry) + len + sizeof(size_t));
if (!ae) {
UNLOCK(Blobs_Lock);
return NULL;
}
NOfBlobs++;
INIT_RWLOCK(ae->ARWLock);
ae->PropsOfAE = AbsBlobProp(b);
ae->NextOfAE = AbsAtom(Blobs);
ae->rep.blob->length = len;
memcpy(ae->rep.blob->data, blob, len);
Blobs = ae;
if (NOfBlobs > NOfBlobsMax) {
Yap_signal(YAP_CDOVF_SIGNAL);
}
UNLOCK(Blobs_Lock);
return ae;
}
bool
YAP_unify_blob(Term *t, void *blob, size_t len, blob_type_t *type)
{
AtomEntry *ae;
if (!blob)
return FALSE;
ae = Yap_lookupBlob(blob, len, type, NULL);
if (!ae) {
return FALSE;
}
if (type->acquire) {
type->acquire(AbsAtom(ae));
}
*t = MkAtomTerm(AbsAtom(ae));
return true;
}
bool YAP_unify_blob(Term *t, void *blob, size_t len, blob_type_t *type) {
AtomEntry *ae;
bool
YAP_get_blob(Term t, void **blob, size_t *len, blob_type_t **type)
{
CACHE_REGS
Atom a;
Term tt;
AtomEntry *ae;
tt = Yap_GetFromSlot(t);
if (IsVarTerm(tt))
return FALSE;
if (!IsAtomTerm(tt))
return FALSE;
a = AtomOfTerm(tt);
if (!IsBlob(a))
return FALSE;
ae = RepAtom(a);
if (type)
*type = RepBlobProp(ae->PropsOfAE)->blob_type;
if (len)
*len = ae->rep.blob[0].length;
if (blob)
*blob = ae->rep.blob[0].data;
return TRUE;
}
void *
YAP_blob_data(Atom x, size_t *len, blob_type_t **type)
{
if (!IsBlob(x)) {
if (IsWideAtom(x)) {
if ( len )
*len = wcslen(x->WStrOfAE);
if ( type )
*type = &unregistered_blob_atom;
return x->WStrOfAE;
}
if ( len )
*len = strlen((char *)x->StrOfAE);
if ( type )
*type = &unregistered_blob_atom;
return x->StrOfAE;
}
if ( len )
*len = x->rep.blob[0].length;
if ( type )
*type = RepBlobProp(x->PropsOfAE)->blob_type;
return x->rep.blob[0].data;
}
void
YAP_register_blob_type(blob_type_t *type)
{
type->next = (void *)BlobTypes;
BlobTypes = (void*)type;
}
blob_type_t*
YAP_find_blob_type(const char* name)
{
AtomEntry *a = RepAtom(Yap_LookupAtom(name));
if (!IsBlob(a)) {
return &unregistered_blob_atom;
}
return RepBlobProp(a->PropsOfAE)->blob_type;
}
bool
YAP_unregister_blob_type(blob_type_t *type)
{
fprintf(stderr,"YAP_unregister_blob_type not implemented yet\n");
if (!blob)
return FALSE;
ae = Yap_lookupBlob(blob, len, type, NULL);
if (!ae) {
return FALSE;
}
if (type->acquire) {
type->acquire(AbsAtom(ae));
}
*t = MkAtomTerm(AbsAtom(ae));
return true;
}
void
Yap_install_blobs(void)
{
bool YAP_get_blob(Term t, void **blob, size_t *len, blob_type_t **type) {
CACHE_REGS
Atom a;
Term tt;
AtomEntry *ae;
tt = Yap_GetFromSlot(t);
if (IsVarTerm(tt))
return FALSE;
if (!IsAtomTerm(tt))
return FALSE;
a = AtomOfTerm(tt);
if (!IsBlob(a))
return FALSE;
ae = RepAtom(a);
if (type)
*type = RepBlobProp(ae->PropsOfAE)->blob_type;
if (len)
*len = ae->rep.blob[0].length;
if (blob)
*blob = ae->rep.blob[0].data;
return TRUE;
}
void *YAP_blob_data(Atom x, size_t *len, blob_type_t **type) {
if (!IsBlob(x)) {
if (IsWideAtom(x)) {
if (len)
*len = wcslen(x->WStrOfAE);
if (type)
*type = &unregistered_blob_atom;
return x->WStrOfAE;
}
if (len)
*len = strlen((char *)x->StrOfAE);
if (type)
*type = &unregistered_blob_atom;
return x->StrOfAE;
}
if (len)
*len = x->rep.blob[0].length;
if (type)
*type = RepBlobProp(x->PropsOfAE)->blob_type;
return x->rep.blob[0].data;
}
void YAP_register_blob_type(blob_type_t *type) {
type->next = (void *)BlobTypes;
BlobTypes = (void *)type;
}
blob_type_t *YAP_find_blob_type(const char *name) {
AtomEntry *a = RepAtom(Yap_LookupAtom(name));
if (!IsBlob(a)) {
return &unregistered_blob_atom;
}
return RepBlobProp(a->PropsOfAE)->blob_type;
}
bool YAP_unregister_blob_type(blob_type_t *type) {
fprintf(stderr, "YAP_unregister_blob_type not implemented yet\n");
return FALSE;
}
void Yap_install_blobs(void) {}
/**
* @}
*/

View File

@ -112,7 +112,7 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen
private final OperationLog mRecentOperations = new OperationLog();
// The native SQLiteConnection pointer. (FOR INTERNAL USE ONLY)
private int mConnectionPtr;
private long mConnectionPtr;
private boolean mOnlyAllowReadOnlyOperations;
@ -122,45 +122,45 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen
// we can ensure that we detach the signal at the right time.
private int mCancellationSignalAttachCount;
private static native int nativeOpen(String path, int openFlags, String label,
private static native long nativeOpen(String path, int openFlags, String label,
boolean enableTrace, boolean enableProfile);
private static native void nativeClose(int connectionPtr);
private static native void nativeRegisterCustomFunction(int connectionPtr,
private static native void nativeClose(long connectionPtr);
private static native void nativeRegisterCustomFunction(long connectionPtr,
SQLiteCustomFunction function);
private static native void nativeRegisterLocalizedCollators(int connectionPtr, String locale);
private static native int nativePrepareStatement(int connectionPtr, String sql);
private static native void nativeFinalizeStatement(int connectionPtr, int statementPtr);
private static native int nativeGetParameterCount(int connectionPtr, int statementPtr);
private static native boolean nativeIsReadOnly(int connectionPtr, int statementPtr);
private static native int nativeGetColumnCount(int connectionPtr, int statementPtr);
private static native String nativeGetColumnName(int connectionPtr, int statementPtr,
private static native void nativeRegisterLocalizedCollators(long connectionPtr, String locale);
private static native long nativePrepareStatement(long connectionPtr, String sql);
private static native void nativeFinalizeStatement(long connectionPtr, long statementPtr);
private static native int nativeGetParameterCount(long connectionPtr, long statementPtr);
private static native boolean nativeIsReadOnly(long connectionPtr, long statementPtr);
private static native int nativeGetColumnCount(long connectionPtr, long statementPtr);
private static native String nativeGetColumnName(long connectionPtr, long statementPtr,
int index);
private static native void nativeBindNull(int connectionPtr, int statementPtr,
private static native void nativeBindNull(long connectionPtr, long statementPtr,
int index);
private static native void nativeBindLong(int connectionPtr, int statementPtr,
private static native void nativeBindLong(long connectionPtr, long statementPtr,
int index, long value);
private static native void nativeBindDouble(int connectionPtr, int statementPtr,
private static native void nativeBindDouble(long connectionPtr, long statementPtr,
int index, double value);
private static native void nativeBindString(int connectionPtr, int statementPtr,
private static native void nativeBindString(long connectionPtr, long statementPtr,
int index, String value);
private static native void nativeBindBlob(int connectionPtr, int statementPtr,
private static native void nativeBindBlob(long connectionPtr, long statementPtr,
int index, byte[] value);
private static native void nativeResetStatementAndClearBindings(
int connectionPtr, int statementPtr);
private static native void nativeExecute(int connectionPtr, int statementPtr);
private static native long nativeExecuteForLong(int connectionPtr, int statementPtr);
private static native String nativeExecuteForString(int connectionPtr, int statementPtr);
long connectionPtr, long statementPtr);
private static native void nativeExecute(long connectionPtr, long statementPtr);
private static native long nativeExecuteForLong(long connectionPtr, long statementPtr);
private static native String nativeExecuteForString(long connectionPtr, long statementPtr);
private static native int nativeExecuteForBlobFileDescriptor(
int connectionPtr, int statementPtr);
private static native int nativeExecuteForChangedRowCount(int connectionPtr, int statementPtr);
long connectionPtr, long statementPtr);
private static native int nativeExecuteForChangedRowCount(long connectionPtr, long statementPtr);
private static native long nativeExecuteForLastInsertedRowId(
int connectionPtr, int statementPtr);
long connectionPtr, long statementPtr);
private static native long nativeExecuteForCursorWindow(
int connectionPtr, int statementPtr, CursorWindow win,
long connectionPtr, long statementPtr, CursorWindow win,
int startPos, int requiredPos, boolean countAllRows);
private static native int nativeGetDbLookaside(int connectionPtr);
private static native void nativeCancel(int connectionPtr);
private static native void nativeResetCancel(int connectionPtr, boolean cancelable);
private static native int nativeGetDbLookaside(long connectionPtr);
private static native void nativeCancel(long connectionPtr);
private static native void nativeResetCancel(long connectionPtr, boolean cancelable);
private static native boolean nativeHasCodec();
public static boolean hasCodec(){ return nativeHasCodec(); }
@ -902,7 +902,7 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen
skipCache = true;
}
final int statementPtr = nativePrepareStatement(mConnectionPtr, sql);
final long statementPtr = nativePrepareStatement(mConnectionPtr, sql);
try {
final int numParameters = nativeGetParameterCount(mConnectionPtr, statementPtr);
final int type = DatabaseUtils.getSqlStatementType(sql);
@ -1003,7 +1003,7 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen
return;
}
final int statementPtr = statement.mStatementPtr;
final long statementPtr = statement.mStatementPtr;
for (int i = 0; i < count; i++) {
final Object arg = bindArgs[i];
switch (ExtraUtils.getTypeOfObject(arg)) {
@ -1088,7 +1088,7 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen
void dumpUnsafe(Printer printer, boolean verbose) {
printer.println("Connection #" + mConnectionId + ":");
if (verbose) {
printer.println(" connectionPtr: 0x" + Integer.toHexString(mConnectionPtr));
printer.println(" connectionPtr: 0x" + Long.toHexString(mConnectionPtr));
}
printer.println(" isPrimaryConnection: " + mIsPrimaryConnection);
printer.println(" onlyAllowReadOnlyOperations: " + mOnlyAllowReadOnlyOperations);
@ -1194,7 +1194,7 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen
return "SQLiteConnection: " + mConfiguration.path + " (" + mConnectionId + ")";
}
private PreparedStatement obtainPreparedStatement(String sql, int statementPtr,
private PreparedStatement obtainPreparedStatement(String sql, long statementPtr,
int numParameters, int type, boolean readOnly) {
PreparedStatement statement = mPreparedStatementPool;
if (statement != null) {
@ -1241,7 +1241,7 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen
// The native sqlite3_stmt object pointer.
// Lifetime is managed explicitly by the connection.
public int mStatementPtr;
public long mStatementPtr;
// The number of parameters that the prepared statement has.
public int mNumParameters;
@ -1287,7 +1287,7 @@ public final class SQLiteConnection implements CancellationSignal.OnCancelListen
if (statement.mInCache) { // might be false due to a race with entryRemoved
String sql = entry.getKey();
printer.println(" " + i + ": statementPtr=0x"
+ Integer.toHexString(statement.mStatementPtr)
+ Long.toHexString(statement.mStatementPtr)
+ ", numParameters=" + statement.mNumParameters
+ ", type=" + statement.mType
+ ", readOnly=" + statement.mReadOnly

View File

@ -14,7 +14,7 @@
* limitations under the License.
*/
/*
** Modified to support SQLite extensions by the SQLite developers:
** Modified to support SQLite extensions by the SQLite developers:
** sqlite-dev@sqlite.org.
*/
@ -111,7 +111,19 @@ public class SQLiteCursor extends AbstractWindowedCursor {
mQuery = query;
mColumns = query.getColumnNames();
mRowIdColumnIndex = ExtraUtils.findRowIdColumnIndex(mColumns);
//mRowIdColumnIndex = ExtraUtils.findRowIdColumnIndex(mColumns);
// try {
// Field field = AbstractCursor.class.getDeclaredField("mRowIdColumnIndex");
// field.setInt(this, ExtraUtils.findRowIdColumnIndex(mColumns));
// } catch (NoSuchFieldException nfe) {
// ;//loaded in system with api level 23 or later
// } catch (IllegalAccessException e) {
// ;//iae
// } catch (IllegalArgumentException e) {
// ;//iae
// }
}
/**
@ -141,10 +153,10 @@ public class SQLiteCursor extends AbstractWindowedCursor {
return mCount;
}
/*
/*
** The AbstractWindowClass contains protected methods clearOrCreateWindow() and
** closeWindow(), which are used by the android.database.sqlite.* version of this
** class. But, since they are marked with "@hide", the following replacement
** class. But, since they are marked with "@hide", the following replacement
** versions are required.
*/
private void awc_clearOrCreateWindow(String name){