/** * Calls a callback on each entry in the directory. * Returns number of directory entries read, 0 if directory * was empty, -1 if error occurs of callback function returns False. * The current and parent directories ("." and "..") are not included * in the count, and callback is not invoked on those directory entries * because these are always valid file names in any directory. */ int FILE_List(Str dir, FileListCB cb, void * ctx) { int n = -1; StrBuf64 pattern; STRBUF_InitBufXXX(&pattern); if (!dir || !dir[0]) dir = TEXT("."); if (STRBUF_Copy(&pattern.sb, dir) && STRBUF_Append(&pattern.sb, TEXT("\\*.*"))) { WIN32_FIND_DATA data; HANDLE hfind = FindFirstFile(pattern.sb.s, &data); if (hfind != INVALID_HANDLE_VALUE) { Str fname = data.cFileName; n = 0; do { if (StrCmp(fname, TEXT(".")) && StrCmp(fname, TEXT(".."))) { if (cb && !(*cb)(dir, fname, ctx)) { n = -1; break; } n++; } } while (FindNextFile(hfind, &data)); FindClose(hfind); } } STRBUF_Destroy(&pattern.sb); return n; }
/** NOTE: both IP address and port are in host byte order */ STATIC File * SocketOpen2(IPaddr addr, Port port) { Socket sock = INVALID_SOCKET; if (SOCKET_GetTcp(0,&sock)) { if (SOCKET_Connect(sock, addr, port)) { SocketFile * s = MEM_New(SocketFile); if (s) { Bool ok; StrBuf32 nameBuf; STRBUF_InitBufXXX(&nameBuf); STRBUF_Format(&nameBuf.sb, TEXT(IPADDR_FORMAT)TEXT_(":%hu"), HOST_IPADDR_FORMAT_ARG(addr),port); memset(s, 0, sizeof(*s)); ok = FILE_Init(&s->file, nameBuf.sb.s, False, &SocketIO); STRBUF_Destroy(&nameBuf.sb); if (ok) { s->sock = sock; s->eof = False; return &s->file; } } shutdown(sock, SHUT_RDWR); } closesocket(sock); } return NULL; }
/** * Parses Java version string. Also returns the number of digits in the * version string that have been successfully parsed. All non-parsed digits * are filled with zeros. The ndigits pointer may be NULL. */ Bool JVM_ParseVersion2(Str strVersion, JavaVersion * version, int* ndigits) { int i; Str p = strVersion; StrBuf16 buf; STRBUF_InitBufXXX(&buf); memset(version, 0, sizeof(*version)); for (i=0; i<JAVA_VERSION_SIZE; i++) { STRBUF_Clear(&buf.sb); while (*p && !IsDigit(*p)) p++; if (*p) { while (IsDigit(*p)) STRBUF_AppendChar(&buf.sb, *p++); if (STRBUF_Length(&buf.sb) > 0) { if (PARSE_Int(STRBUF_Text(&buf.sb),&(version->v[i]),10)) { continue; } } } break; } if (ndigits) *ndigits = i; STRBUF_Destroy(&buf.sb); return (i >= 2); }
/** * Creates a directory hierarhy. */ Bool FILE_MkDir(Str dir) { Bool ok = False; StrBuf64 entry; StrBuf* sb = &entry.sb; STRBUF_InitBufXXX(&entry); if (STRBUF_Copy(sb, dir)) { while (sb->len > 0 && FILE_IsFileSeparator(STRBUF_LastChar(sb))) { STRBUF_SetLength(sb, sb->len-1); } /* check if the directory already exists */ if (FILE_IsDir(STRBUF_Text(sb)) || FILE_CreateDir(STRBUF_Text(sb))) { ok = True; } else { /* directory does not exists, walk the hierarhy */ int pos = 0; int next = 0; while ((next = FILE_FindSeparator(dir+pos)) >= 0) { STRBUF_Clear(sb); if (next == 0) { pos++; continue; } else { if (!STRBUF_CopyN(sb,dir,pos+next) || !FILE_CreateDir(STRBUF_Text(sb))) { break; } pos += next + 1; } } /* final test */ if (STRBUF_Copy(sb, dir)) { while (sb->len && FILE_IsFileSeparator(STRBUF_LastChar(sb))) { STRBUF_SetLength(sb, sb->len-1); } if (FILE_IsDir(STRBUF_Text(sb)) || FILE_CreateDir(STRBUF_Text(sb))) { ok = True; } } } } STRBUF_Destroy(sb); return ok; }
/** * FILE_RmDir callback. Recursively destroys the directory contents */ STATIC Bool RmDirCB(Str dir, Str fname, void * ctx) { Bool ok = False; StrBuf64 path; UNREF(ctx); STRBUF_InitBufXXX(&path); if (STRBUF_Copy(&path.sb, dir) && STRBUF_AppendChar(&path.sb, FILE_SEPARATOR_CHAR) && STRBUF_Append(&path.sb, fname)) { ok = True; if (FILE_IsDir(path.sb.s)) { FILE_RmDir(path.sb.s, True); } else { FILE_Delete(path.sb.s); } } STRBUF_Destroy(&path.sb); return ok; }