sint_t AStdMemFile::close() { if (isopen()) { if (bAllocated) delete[] pData; pData = NULL; } Pos = Length = AllocatedLength = SizeInc = 0; bAllocated = false; return 0; }
enum nss_status internal_function __nss_cdb_setent(struct nss_cdb *dbp, int stayopen) { enum nss_status r; lock_lock(lock); if (isopen(dbp) || __nss_cdb_dosetent(dbp)) r = NSS_STATUS_SUCCESS, dbp->keepopen |= stayopen; else r = NSS_STATUS_UNAVAIL; lock_unlock(lock); return r; }
bool MySQLConnection::open(const std::string& file) { //use a config file if ( ! isopen() ) { if (s->options(connectionHandle,MYSQL_READ_DEFAULT_FILE,file.c_str()) == 0) { thost = file; if (s->real_connect(connectionHandle, NULL, NULL, NULL, NULL, 0, NULL, 0) == nullptr) { string errorMessage = s->error(connectionHandle); *Logger::log << Log::fatal << Log::LI << "MySQLConnection error:: Connection failed with '" << errorMessage << "'" << Log::LO << Log::blockend; conn_open = false; } else { conn_open = true; } } else { *Logger::log << Log::fatal << Log::LI << "MySQLConnection error: Possibly file '" << file << " is not valid or not found." << Log::LO << Log::blockend; } } else { if (Logger::debugging() && Logger::log != nullptr) { *Logger::log << Log::info << Log::LI << "MySQLConnection error: Each connection can only be opened once. Instantiate a new connection." << Log::LO << Log::blockend; } } return isopen(); }
slong_t AStdMemFile::readdata(void *buf, size_t bytes) { slong_t bytes1 = -1; if (isopen()) { bytes = MIN(bytes, Length - Pos); memcpy(buf, pData + Pos, bytes); Pos += bytes; bytes1 = bytes; } return bytes1; }
int32_t CBaseSocket::recv(void* buf, uint32_t buf_size, Inet_Addr& remote_addr) { if(!isopen()) return -1; #ifdef WIN32 int32_t from_size = sizeof(struct sockaddr_in); #else uint32_t from_size = sizeof(struct sockaddr_in); #endif return ::recvfrom(get_handler(), (char *)buf, buf_size, 0, (struct sockaddr *)remote_addr.get_addr(), &from_size); }
sint_t AStdDev::flush() { sint_t res = -1; if (isopen()) { #ifdef __LINUX__ res = ::fsync(fd); #else res = ::_commit(fd); #endif } return res; }
int main (void){ FILE *pFile; int nopen, fd; pFile = fopen("test.txt","r"); if(pFile == NULL){ printf ("Error opening file unexist.ent: %s\n",strerror(errno)); return 0; } for (nopen = fd = 0; fd < getdtablesize(); fd++) { if (isopen(fd)) nopen++; } printf ("Existem %d descritores abertos\n", nopen); return 0; }
bool MySQLConnection::query(Query*& q,std::string query_str) { bool retval = false; if ( isopen() ) { if ( db_open ) { q = new MySQLQuery(s, this, connectionHandle, query_str); if (q != nullptr) { retval = true; } } else { q = nullptr; } } else { q = nullptr; } return retval; }
enum nss_status internal_function __nss_cdb_byid(struct nss_cdb *dbp, unsigned long id, void *result, char *buf, size_t bufl, int *errnop) { enum nss_status r; if (bufl < 30) return *errnop = ERANGE, NSS_STATUS_TRYAGAIN; lock_lock(lock); if (!isopen(dbp) && !__nss_cdb_dosetent(dbp)) *errnop = errno, r = NSS_STATUS_UNAVAIL; else { r = __nss_cdb_dobyid(dbp, id, result, buf, bufl, errnop); if (!dbp->keepopen) __nss_cdb_doendent(dbp); } lock_unlock(lock); return r; }
enum nss_status internal_function __nss_cdb_byname(struct nss_cdb *dbp, const char *name, void *result, char *buf, size_t bufl, int *errnop) { enum nss_status r; if (*name == ':') return *errnop = ENOENT, NSS_STATUS_NOTFOUND; lock_lock(lock); if (!isopen(dbp) && !__nss_cdb_dosetent(dbp)) *errnop = errno, r = NSS_STATUS_UNAVAIL; else { r = __nss_cdb_dobyname(dbp, name, strlen(name), result, buf, bufl, errnop); if (!dbp->keepopen) __nss_cdb_doendent(dbp); } lock_unlock(lock); return r; }
int main (void) { int nopen, fd, in; in = open("main.c", O_RDONLY); for (nopen = fd = 0; fd < getdtablesize (); fd++) { if (isopen(fd)) { nopen++; } } printf ("Existem %d descritores abertos\n", nopen); return 0; }
sint_t AStdMemFile::seek(slong_t offset, uint_t origin) { sint_t res = -1; if (isopen()) { sint64_t pos = Pos; switch (origin) { case SEEK_SET: pos = offset; break; case SEEK_CUR: pos = (sint64_t)Pos + offset; break; case SEEK_END: pos = (sint64_t)Length + offset; break; default: assert(false); break; } if (RANGE(pos, 0, (sint64_t)Length)) { Pos = (size_t)pos; res = 0; } } return res; }
/* // // ISAM open - INPUT // */ int tcob_open_input_idx(struct file_desc *f) { int r=0, imode=0, ifilehandle; tcb_fileio_vbidx_handle *h; struct stat st1, st2; #ifdef DEBUG_FILEISAM_RTS0 fprintf(stderr, "debug : tcob_open_input_idx : 030.020 : filename=%s;\n", f->filename); #endif h = (tcb_fileio_vbidx_handle *)f->dbp; if ((stat(h->fdatname, &st1) == -1 && errno == ENOENT) || (stat(h->fidxname, &st2) == -1 && errno == ENOENT)) return (35); if (f->flags.file_isvariable == 1) imode = ISINPUT+ISVARLEN+ISAUTOLOCK; else imode = ISINPUT+ISFIXLEN+ISAUTOLOCK; ifilehandle = isopen(f->filename, imode); /* Set return code depending on VB iserrno */ if (ifilehandle < 0) { r = tcob_eisam2ecob(0); } /* Verify ISAM structures */ if (r == 0) { r = tcob_verify_vbidx(f, ifilehandle); if (r != 0) { isclose(ifilehandle); r = 99; } } if (r == 0) h->ifilehandle = ifilehandle; #ifdef DEBUG_FILEISAM_RTS0 fprintf(stderr, "debug : tcob_open_input_idx : 030.140 : iserrno=%d, r=%d\n", iserrno, r); #endif return r; }
bool VFSFileReal::open(const char *mode /* = NULL */) { VFS_GUARD_OPT(this); if(isopen()) close(); dropBuf(true); _fh = real_fopen(fullname(), mode ? mode : "rb"); if(!_fh) return false; real_fseek((FILE*)_fh, 0, SEEK_END); _size = getpos(); real_fseek((FILE*)_fh, 0, SEEK_SET); return true; }
/* // // ISAM open - OUTPUT // */ int tcob_open_output_idx(struct file_desc *f) { int r=0, imode=0, ifilehandle; tcb_fileio_vbidx_handle *h; struct stat st1, st2; #ifdef DEBUG_FILEISAM_RTS0 fprintf(stderr, "debug : tcob_open_output_idx : 040.020 : filename=%s;\n", f->filename); #endif h = (tcb_fileio_vbidx_handle *)f->dbp; /* If the files do NOT exist build the files */ if ((stat(h->fdatname, &st1) == -1 && errno == ENOENT) || (stat(h->fidxname, &st2) == -1 && errno == ENOENT)) { r = tcob_build_vbidx(f); #ifdef DEBUG_FILEISAM_RTS0 fprintf(stderr, "debug : tcob_open_output_idx : 040.040 : r=%d;\n", r); #endif if (r != 0) return (r); } if (f->flags.file_isvariable == 1) imode = ISOUTPUT+ISVARLEN+ISAUTOLOCK; else imode = ISOUTPUT+ISFIXLEN+ISAUTOLOCK; ifilehandle = isopen(f->filename, imode); /* Set return code depending on VB iserrno */ if (ifilehandle < 0) r = tcob_eisam2ecob(0); else h->ifilehandle = ifilehandle; #ifdef DEBUG_FILEISAM_RTS0 fprintf(stderr, "debug : tcob_open_output_idx : 040.140 : iserrno=%d, r=%d\n", iserrno, r); #endif return r; }
const void *VFSFile::getBuf(allocator_func alloc /* = NULL */, delete_func del /* = NULL */) { assert(!alloc == !del); // either both or none may be defined. Checked extra early to prevent possible errors later. VFS_GUARD_OPT(this); if(_buf) return _buf; bool op = isopen(); if(!op && !open()) // open with default params if not open return NULL; unsigned int s = (unsigned int)size(); _buf = allocHelper(alloc, s + 4); // a bit extra padding if(!_buf) return NULL; _delfunc = del; vfspos offs; if(op) { vfspos oldpos = getpos(); seek(0); offs = read(_buf, s); seek(oldpos); } else { offs = read(_buf, s); close(); } // Might as well be text mode reading, which means less actual bytes than size() said, // so this can't be done earlier. memset((char*)_buf + offs, 0, 4); return _buf; }
int idxopen (unit *ftnunit, char *name, int create, flag idxerr) { struct dictinfo info; int mode =(ftnunit->ureadonly ? ISINPUT : ISINOUT) + (ftnunit->ushared ? ISAUTOLOCK : ISEXCLLOCK); register int i; keys = ftnunit->ukeys; if (create) { if (ftnunit->url == 0) err (idxerr, 153, "indexed open"); if (ftnunit->unkeys == 0 || keys == NULL) err (idxerr, 161, "indexed open"); onekey.k_flags = ISNODUPS; KEYOFF (0)--; KEYEND (0)--; dokey (0, ONEKEY); if (ftnunit->unkeys == 1) { /* DLAI: need to change isbuild 2nd arg to XINT */ if ((ftnunit->isfd = isbuild (name, ftnunit->url, &onekey, mode)) < SUCCESS) ierr (idxerr, iserrno, "indexed open"); } else { if ((ftnunit->isfd = isbuild (name, ftnunit->url, &onekey, ISINOUT + ISEXCLLOCK)) < SUCCESS) ierr (idxerr, iserrno, "indexed open"); onekey.k_flags = ISDUPS; for (i = 1; i < ftnunit->unkeys; i++) { KEYOFF (i)--; KEYEND (i)--; dokey (i, ONEKEY); if (isaddindex (ftnunit->isfd, &onekey) < SUCCESS) ierr (idxerr, iserrno, "indexed open"); } if (ftnunit->ushared) { if (isclose (ftnunit->isfd) < SUCCESS) ierr (idxerr, iserrno, "indexed open"); if ((ftnunit->isfd = isopen (name, mode)) < SUCCESS) ierr (idxerr, iserrno, "indexed open"); } } } else { if ((ftnunit->isfd = isopen (name, mode)) < SUCCESS) ierr (idxerr, iserrno, "indexed open"); if (isindexinfo (ftnunit->isfd, &info, 0) < SUCCESS) ierr (idxerr, iserrno, "indexed open"); if (ftnunit->unkeys != info.di_nkeys) { if (ftnunit->unkeys) { err (idxerr, 148, "indexed open"); } else ftnunit->unkeys = info.di_nkeys; } if (!keys) keys = (Keyspec *) malloc (sizeof (Keyspec) * info.di_nkeys); for (i = 0; i < info.di_nkeys; i++) { if (isindexinfo (ftnunit->isfd, &onekey, i + 1) < SUCCESS) ierr (idxerr, iserrno, "indexed open"); /* LHL 5/4/89 * To fix bug 4428, problem about trying to open an existing indexed file. * This is put here because when the indexed file is created, the keys * are being stored like this. Refer to the above code. */ KEYOFF (i)--; KEYEND (i)--; /* end of fix */ if (ftnunit->ukeys && dokey (i, NOMATCH)) { err (idxerr, 148, "indexed open"); } else dokey (i, UNITKEY); } ftnunit->url = info.di_recsize; ftnunit->ukeys = keys; } return SUCCESS; }
void TicTacToeAI::makeMove() { if ( isopen(4) ) { take(4); } else if ( theyhavespaces(0,1) && isopen(2) ) { take(2); } else if ( theyhavespaces(1,2) && isopen(0) ) { take(0); } else if ( theyhavespaces(2,0) && isopen(1) ) { take(1); } // Middle Across else if ( theyhavespaces(3,4) && isopen(5) ) { take(5); } else if ( theyhavespaces(5,4) && isopen(3) ) { take(3); } else if ( theyhavespaces(5,3) && isopen(4) ) { take(4); } // Bottom Across else if ( theyhavespaces(6,7) && isopen(8) ) { take(8); } else if ( theyhavespaces(6,8) && isopen(7) ) { take(7); } else if ( theyhavespaces(7,8) && isopen(6) ) { take(6); } // Row 1 else if ( theyhavespaces(0,3) && isopen(6) ) { take(6); } else if ( theyhavespaces(0,6) && isopen(3) ) { take(3); } else if ( theyhavespaces(6,3) && isopen(0) ) { take(0); } // Row 2 else if ( theyhavespaces(1,4) && isopen(7) ) { take(7); } else if ( theyhavespaces(1,7) && isopen(4) ) { take(4); } else if ( theyhavespaces(7,4) && isopen(1) ) { take(1); } // Row 3 else if ( theyhavespaces(2,5) && isopen(8) ) { take(8); } else if ( theyhavespaces(5,8) && isopen(2) ) { take(2); } else if ( theyhavespaces(2,8) && isopen(5) ) { take(5); } // 0,8 Diaginal else if ( theyhavespaces(0,4) && isopen(8) ) { take(8); } else if ( theyhavespaces(4,8) && isopen(0) ) { take(0); } // 2,6 Diaginal else if ( theyhavespaces(2,4) && isopen(6) ) { take(6); } else if ( theyhavespaces(6,4) && isopen(2) ) { take(2); } // Try2Win // Top Across else if ( havespaces(0,1) && isopen(2) ) { take(2); } else if ( havespaces(1,2) && isopen(0) ) { take(0); } else if ( havespaces(2,0) && isopen(1) ) { take(1); } // Middle Across else if ( havespaces(3,4) && isopen(5) ) { take(5); } else if ( havespaces(5,4) && isopen(3) ) { take(3); } else if ( havespaces(5,3) && isopen(4) ) { take(4); } // Bottom Across else if ( havespaces(6,7) && isopen(8) ) { take(8); } else if ( havespaces(6,8) && isopen(7) ) { take(7); } else if ( havespaces(7,8) && isopen(6) ) { take(6); } // Row 1 else if ( havespaces(0,3) && isopen(6) ) { take(6); } else if ( havespaces(0,6) && isopen(3) ) { take(3); } else if ( havespaces(6,3) && isopen(0) ) { take(0); } // Row 2 else if ( havespaces(1,4) && isopen(7) ) { take(7); } else if ( havespaces(1,7) && isopen(4) ) { take(4); } else if ( havespaces(7,4) && isopen(1) ) { take(1); } // Row 3 else if ( havespaces(2,5) && isopen(8) ) { take(8); } else if ( havespaces(5,8) && isopen(2) ) { take(2); } else if ( havespaces(2,8) && isopen(5) ) { take(5); } // 0,8 Diaginal else if ( havespaces(0,4) && isopen(8) ) { take(8); } else if ( havespaces(4,8) && isopen(0) ) { take(0); } // 2,6 Diaginal else if ( havespaces(2,4) && isopen(6) ) { take(6); } else if ( havespaces(6,4) && isopen(2) ) { take(2); } else { // if all else fails, pick the next available slot for ( int i = 0; i < 9; i++ ) { if ( isopen(i) ) { take(i); return; } } } }
int main (int iArgc, char **ppcArgv) { int iResult, iLoop, iLoop2, iLoop3, iHandle; unsigned char cRecord [256]; struct keydesc sKeydesc; char cLogfileName [100], cCommand [100]; char cFileName [] = "IsamTest"; memset (&sKeydesc, 0, sizeof (sKeydesc)); sKeydesc.k_flags = COMPRESS; sKeydesc.k_nparts = 1; sKeydesc.k_start = 0; sKeydesc.k_leng = 2; sKeydesc.k_type = CHARTYPE; if (iArgc == 1) { printf ("Usage:\n\t%s create\nOR\n\t%s <#iterations>\n", ppcArgv [0], ppcArgv [0]); exit (1); } if (iArgc > 1 && strcmp (ppcArgv [1], "create") == 0) { iserase (cFileName); iHandle = isbuild (cFileName, 255, &sKeydesc, ISINOUT+ISFIXLEN+ISEXCLLOCK); if (iHandle < 0) { printf ("Error creating database: %d\n", iserrno); exit (-1); } sKeydesc.k_flags |= ISDUPS; sKeydesc.k_start = 3; sKeydesc.k_leng = 4; for (sKeydesc.k_start = 1; sKeydesc.k_start < 2; sKeydesc.k_start++) { if (isaddindex (iHandle, &sKeydesc)) { printf ("Error %d adding index %d\n", iserrno, sKeydesc.k_start); } } isclose (iHandle); sprintf (cLogfileName, "RECOVER"); #ifdef _WIN32 sprintf (cCommand, "del /f /q %s", cLogfileName); #else sprintf (cCommand, "rm -f %s; touch %s", cLogfileName, cLogfileName); #endif system (cCommand); return (0); } sprintf (cLogfileName, "RECOVER"); #ifdef _WIN32 iResult = open("RECOVER", O_CREAT | O_TRUNC | O_RDWR | O_BINARY, 0666); #else iResult = open("RECOVER", O_CREAT | O_TRUNC | O_RDWR, 0666); #endif close(iResult); iResult = islogopen (cLogfileName); if (iResult < 0) { printf ("Error opening log: %d\n", iserrno); exit (-1); } /* srand (time (NULL)); */ srand (9); for (iLoop = 0; iLoop < atoi (ppcArgv [1]); iLoop++) { if (!(iLoop % 100)) { printf ("iLoop=%d\n", iLoop); fflush(stdout); } iVBDlCount = 0; iVBRdCount = 0; iVBUpCount = 0; iVBWrCount = 0; iResult = isbegin (); if (iResult < 0) { printf ("Error begin transaction: %d\n", iserrno); exit (-1); } iHandle = isopen (cFileName, ISINOUT+ISFIXLEN+ISTRANS+ISAUTOLOCK); if (iHandle < 0) { printf ("Error opening database: %d\n", iserrno); exit (-1); } for (iLoop2 = 0; iLoop2 < 100; iLoop2++) { for (iLoop3 = 0; iLoop3 < 256; iLoop3++) { cRecord [iLoop3] = rand () % 256; } iResult =rand () % 4; /* fprintf(stderr, "I %d\n", iResult); */ switch (iResult) { case 0: if ((iResult = iswrite (iHandle, (char *) cRecord)) != 0) { if (iserrno != EDUPL && iserrno != ELOCKED) { printf ("Error writing: %d\n", iserrno); goto err; } } else { iVBWrCount++; } break; case 1: if ((iResult = isread (iHandle, (char *)cRecord, ISEQUAL)) != 0) { if (iserrno == ELOCKED) { ; /* printf ("Locked during deletion\n"); */ } else if (iserrno != ENOREC) { printf ("Error reading: %d\n", iserrno); goto err; } } else { iVBRdCount++; } break; case 2: for (iLoop3 = 0; iLoop3 < 256; iLoop3++) { cRecord [iLoop3] = rand () % 256; } if ((iResult = isrewrite (iHandle, (char *)cRecord)) != 0) { if (iserrno == ELOCKED) { ; /* printf ("Locked during rewrite\n"); */ } else if (iserrno != ENOREC) { printf ("Error rewriting: %d\n", iserrno); goto err; } } else { iVBUpCount++; } break; case 3: if ((iResult = isdelete (iHandle, (char *)cRecord)) != 0) { if (iserrno == ELOCKED) { ; /* printf ("Locked during deletion\n"); */ } else if (iserrno != ENOREC) { printf ("Error deleting: %d\n", iserrno); goto err; } } else iVBDlCount++; break; } } iResult = isflush (iHandle); if (iResult < 0) { printf ("Error flush: %d\n", iserrno); exit (-1); } iResult = isclose (iHandle); if (iResult < 0) { printf ("Error closing database: %d\n", iserrno); exit (-1); } iVBDlTotal += iVBDlCount; iVBRdTotal += iVBRdCount; iVBUpTotal += iVBUpCount; iVBWrTotal += iVBWrCount; switch (rand () % 2) { case 0: iVBDlCommit += iVBDlCount; iVBRdCommit += iVBRdCount; iVBUpCommit += iVBUpCount; iVBWrCommit += iVBWrCount; iResult = iscommit (); if (iResult < 0) { printf ("Error commit: %d\n", iserrno); exit (-1); } break; case 1: iResult = isrollback (); if (iResult < 0) { if (iserrno == EDUPL || iserrno == ENOREC) { printf ("Same BUG (%d) as in C-ISAM!\n", iserrno); } else { printf ("Error rollback: %d\n", iserrno); exit (-1); } } break; } } err: printf (" Total Commited\n"); printf (" -------- --------\n"); printf ("Delete Count: %8d %8d\n", iVBDlTotal, iVBDlCommit); printf ("Read Count: %8d %8d\n", iVBRdTotal, iVBRdCommit); printf ("Update Count: %8d %8d\n", iVBUpTotal, iVBUpCommit); printf ("Write Count: %8d %8d\n", iVBWrTotal, iVBWrCommit); printf (" -------- --------\n"); printf ("OPS OVERALL : %8d %8d\n", (iVBDlTotal + iVBRdTotal + iVBUpTotal + iVBWrTotal), (iVBDlCommit + iVBRdCommit + iVBUpCommit + iVBWrCommit)); printf (" ========\n"); printf ("ROWS ADDED THIS RUN: %8d\n", (iVBWrCommit - iVBDlCommit)); printf (" ========\n"); return (iResult); }
fx2::~fx2() { if (isopen()) close(); libusb_exit(libusb_ctx); }