unsigned long fileSize(FILE *stream, char *name) { long n = 0; FILE *f = stream; if (!stream) { if (!name) return 0; f = fileOpen(name, "r"); if (!f) return 0; } fileSeek(f, 0, SEEK_END); n = fileTell(f); if (!stream) { fileClose(f); } else { fileSeek(f, 0, SEEK_SET); } return n; }
int64_t fgetsize(FILE *f) { if (!f) return 0; int64_t size; // int64_t pos = _ftelli64(f); int64_t pos = fileTell(f); fileSeek(f, 0, SEEK_END); size = fileTell(f); fileSeek(f, pos, SEEK_SET); /*_fseeki64(f, 0, SEEK_END); size = _ftelli64(f); _fseeki64(f, pos, SEEK_SET); */ return size; }
uint32_t fileGetSize(const String fileName) { file_t file = fileOpen(fileName.c_str(), eFO_ReadOnly); // Get size fileSeek(file, 0, eSO_FileEnd); int size = fileTell(file); fileClose(file); return size; }
void HttpClient::onFinished(TcpClientState finishState) { if (finishState == eTCS_Failed) code = 0; if (mode == eHCM_File) { debugf("Download file len written: %d, res^ %d", fileTell(saveFile), isSuccessful()); if (!isSuccessful()) fileDelete(saveFile); fileClose(saveFile); } if (onCompleted) onCompleted(*this, isSuccessful()); TcpClient::onFinished(finishState); }
FileStream::FileStream(String fileName) { handle = fileOpen(fileName.c_str(), eFO_ReadOnly); if (handle == -1) { debugf("File wasn't found: %s", fileName.c_str()); size = -1; pos = 0; } // Get size fileSeek(handle, 0, eSO_FileEnd); size = fileTell(handle); fileSeek(handle, 0, eSO_FileStart); pos = 0; debugf("send file: %s (%d bytes)", fileName.c_str(), size); }
int fileGetContent(const String fileName, char* buffer, int bufSize) { if (buffer == NULL || bufSize == 0) return 0; *buffer = 0; file_t file = fileOpen(fileName.c_str(), eFO_ReadOnly); // Get size fileSeek(file, 0, eSO_FileEnd); int size = fileTell(file); if (size <= 0 || bufSize <= size) { fileClose(file); return 0; } buffer[size] = 0; fileSeek(file, 0, eSO_FileStart); fileRead(file, buffer, size); fileClose(file); return size; }
String fileGetContent(const String fileName) { file_t file = fileOpen(fileName.c_str(), eFO_ReadOnly); // Get size fileSeek(file, 0, eSO_FileEnd); int size = fileTell(file); if (size <= 0) { fileClose(file); return ""; } fileSeek(file, 0, eSO_FileStart); char* buffer = new char[size + 1]; buffer[size] = 0; fileRead(file, buffer, size); fileClose(file); String res = buffer; delete[] buffer; return res; }
bool FileStream::attach(String fileName, FileOpenFlags openFlags) { handle = fileOpen(fileName.c_str(), openFlags); if (handle == -1) { debugf("File wasn't found: %s", fileName.c_str()); size = -1; pos = 0; return false; } // Get size fileSeek(handle, 0, eSO_FileEnd); size = fileTell(handle); fileSeek(handle, 0, eSO_FileStart); pos = 0; debugf("attached file: %s (%d bytes)", fileName.c_str(), size); return true; }
FileStream::FileStream(String fileName) { handle = fileOpen(fileName.c_str(), eFO_ReadOnly); if (handle == -1) { debugf("File wasn't found: %s", fileName.c_str()); buffer = NULL; size = -1; pos = 0; } // Get size fileSeek(handle, 0, eSO_FileEnd); size = fileTell(handle); fileSeek(handle, 0, eSO_FileStart); pos = 0; buffer = new char[min(size, NETWORK_SEND_BUFFER_SIZE)]; debugf("send file: %s (%d bytes)", fileName.c_str(), size); }
unsigned long fileSize(FILE *stream, char *name) { #ifdef USE_FILE_API long n = 0; FILE *f = stream; if (!f) { if (!name) return 0; f = fileOpen(name, "r"); if (!f) return 0; } fileSeek(f, 0, SEEK_END); n = fileTell(f); if (!stream) { fileClose(f); } else { fileSeek(f, 0, SEEK_SET); } return n; #else FILE *f = stream; Err error = errNone; UInt32 size = 0; if (!f || !f->fileRef) return 0; error = VFSFileSize(f->fileRef, &size); return (error)?-1:size; #endif }
void bmpDraw(Adafruit_ST7735 tft, String fileName, uint8_t x, uint8_t y) { file_t handle; int bmpWidth, bmpHeight; // W+H in pixels uint8_t bmpDepth; // Bit depth (currently must be 24) uint32_t bmpImageoffset; // Start of image data in file uint32_t rowSize; // Not always = bmpWidth; may have padding uint8_t sdbuffer[3 * BUFFPIXEL]; // pixel buffer (R+G+B per pixel) uint8_t buffidx = sizeof(sdbuffer); // Current position in sdbuffer boolean goodBmp = false; // Set to true on valid header parse boolean flip = true; // BMP is stored bottom-to-top int w, h, row, col; uint8_t r, g, b; uint32_t pos = 0, startTime = millis(); if ((x >= tft.width()) || (y >= tft.height())) return; Serial.println(); Serial.print("Loading image '"); Serial.print(fileName); Serial.println('\''); handle = fileOpen(fileName.c_str(), eFO_ReadOnly); if (handle == -1) { debugf("File wasn't found: %s", fileName.c_str()); fileClose(handle); return; } // Parse BMP header if (read16(handle) == 0x4D42) { // BMP signature debugf("File size: %d\n", read32(handle)); // get File Size (void)read32(handle); // Read & ignore creator bytes bmpImageoffset = read32(handle); // Start of image data debugf("Image Offset: %d\n", bmpImageoffset); debugf("Header size: %d\n", read32(handle)); // Read DIB header bmpWidth = read32(handle); bmpHeight = read32(handle); if(read16(handle) == 1) { // # planes -- must be '1' bmpDepth = read16(handle); // bits per pixel debugf("Bit Depth: %d\n", bmpDepth); if((bmpDepth == 24) && (read32(handle) == 0)) { // 0 = uncompressed goodBmp = true; // Supported BMP format -- proceed! debugf("Image size: %d x %d\n", bmpWidth, bmpHeight); // BMP rows are padded (if needed) to 4-byte boundary rowSize = (bmpWidth * 3 + 3) & ~3; // If bmpHeight is negative, image is in top-down order. // This is not canon but has been observed in the wild. if(bmpHeight < 0) { bmpHeight = -bmpHeight; flip = false; } // Crop area to be loaded w = bmpWidth; h = bmpHeight; if((x+w-1) >= tft.width()) w = tft.width() - x; if((y+h-1) >= tft.height()) h = tft.height() - y; // Set TFT address window to clipped image bounds tft.setAddrWindow(x, y, x+w-1, y+h-1); for (row=0; row<h; row++) { // For each scanline... // Seek to start of scan line. It might seem labor- // intensive to be doing this on every line, but this // method covers a lot of gritty details like cropping // and scanline padding. Also, the seek only takes // place if the file position actually needs to change // (avoids a lot of cluster math in SD library). if(flip) // Bitmap is stored bottom-to-top order (normal BMP) pos = bmpImageoffset + (bmpHeight - 1 - row) * rowSize; else // Bitmap is stored top-to-bottom pos = bmpImageoffset + row * rowSize; if (fileTell(handle) != pos) { fileSeek(handle, pos, eSO_FileStart); buffidx = sizeof(sdbuffer); // Force buffer reload } for (col=0; col<w; col++) { // For each pixel... // Time to read more pixel data? if (buffidx >= sizeof(sdbuffer)) { // Indeed fileRead(handle, sdbuffer, sizeof(sdbuffer)); buffidx = 0; // Set index to beginning } // Convert pixel from BMP to TFT format, push to display b = sdbuffer[buffidx++]; g = sdbuffer[buffidx++]; r = sdbuffer[buffidx++]; tft.pushColor(tft.Color565(r,g,b)); } // end pixel } // end scanline Serial.printf("Loaded in %d ms\n", millis() - startTime); } // end goodBmp } } fileClose(handle); if(!goodBmp) Serial.println("BMP format not recognized."); }
/* Split font set file into arg list */ static void makeArgs(char *filename) { int state; long i; long length; File file; char *start = NULL; /* Suppress optimizer warning */ /* Read whole file into buffer */ fileOpen(&file, cbctx, filename, "r"); fileSeek(&file, 0, SEEK_END); length = fileTell(&file); script.buf = cbMemNew(cbctx, length + 1); fileSeek(&file, 0, SEEK_SET); fileReadN(&file, length, script.buf); fileClose(&file); script.buf[length] = '\n'; /* Ensure termination */ /* Parse buffer into args */ state = 0; for (i = 0; i < length + 1; i++) { int c = script.buf[i]; switch (state) { case 0: switch (c) { case ' ': case '\n': case '\t': case '\r': case '\f': break; case '#': state = 1; break; case '"': start = &script.buf[i + 1]; state = 2; break; default: start = &script.buf[i]; state = 3; break; } break; case 1: /* Comment */ if (c == '\n' || c == '\r') { state = 0; } break; case 2: /* Quoted string */ if (c == '"') { script.buf[i] = '\0'; /* Terminate string */ *dnaNEXT(script.args) = start; state = 0; } break; case 3: /* Space-delimited string */ if (isspace(c)) { script.buf[i] = '\0'; /* Terminate string */ *dnaNEXT(script.args) = start; state = 0; } break; } } }