void Main_Copy (void) { USB_INT32S fdr; USB_INT32S fdw; USB_INT32U bytes_read; fdr = FILE_Open(FILENAME_R, RDONLY); if (fdr > 0) { fdw = FILE_Open(FILENAME_W, RDWR); if (fdw > 0) { PRINT_Log("Copying from %s to %s...\n", FILENAME_R, FILENAME_W); do { bytes_read = FILE_Read(fdr, UserBuffer, MAX_BUFFER_SIZE); FILE_Write(fdw, UserBuffer, bytes_read); } while (bytes_read); FILE_Close(fdw); } else { PRINT_Log("Could not open file %s\n", FILENAME_W); return; } FILE_Close(fdr); PRINT_Log("Copy completed\n"); } else { PRINT_Log("Could not open file %s\n", FILENAME_R); return; } }
void Main_Write (void) { USB_INT32S fdw; USB_INT32S fdr; USB_INT32U tot_bytes_written; USB_INT32U bytes_written; fdr = FILE_Open(FILENAME_R, RDONLY); if (fdr > 0) { FILE_Read(fdr, UserBuffer, MAX_BUFFER_SIZE); fdw = FILE_Open(FILENAME_W, RDWR); if (fdw > 0) { tot_bytes_written = 0; PRINT_Log("Writing to %s...\n", FILENAME_W); do { bytes_written = FILE_Write(fdw, UserBuffer, MAX_BUFFER_SIZE); tot_bytes_written += bytes_written; } while (tot_bytes_written < WRITE_SIZE); FILE_Close(fdw); PRINT_Log("Write completed\n"); } else { PRINT_Log("Could not open file %s\n", FILENAME_W); return; } FILE_Close(fdr); } else { PRINT_Log("Could not open file %s\n", FILENAME_R); return; } }
/** * Parse a file given the file name. */ Bool XML_ParseFile(Str path, const XMLCallbacks * cb, void * ctx) { Bool ok = False; File * f = FILE_Open(path, READ_TEXT_MODE, NULL); if (f) { ok = XML_ParseStream(f, cb, ctx); FILE_Close(f); } return ok; }
void Main_Read (void) { USB_INT32S fdr; USB_INT32U bytes_read; fdr = FILE_Open(FILENAME_R, RDONLY); if (fdr > 0) { PRINT_Log("Reading from %s...\n", FILENAME_R); do { bytes_read = FILE_Read(fdr, UserBuffer, MAX_BUFFER_SIZE); } while (bytes_read); FILE_Close(fdr); PRINT_Log("Read Complete\n"); } else { PRINT_Log("Could not open file %s\n", FILENAME_R); return; } }
/** * Initializes GwTrace app context, connects to the gateway and * accepts one incoming connection. */ static Bool GWTRACE_Init(GwTrace* trace, Str host, Port gwPort, Str file) { trace->server = XRPC_CreateServer(NULL); if (trace->server) { /* Register our side of the SEI proxu protocol */ XRpcProtocol protocol; memset(&protocol, 0, sizeof (protocol)); protocol.getmethod = GWTRACE_GetMethod; if (XRPC_RegisterProtocol(trace->server, ECMTGW_LISTENER_PROTOCOL, "EcmtGW listener", "Nokia", 1, 0, &protocol, trace)) { /* connect to the Gateway */ PRINT_Verbose("%s: connecting to %s:%hu\n",pname, host ? host : "localhost", gwPort); trace->session = XRPC_Connect(trace->server, host, gwPort); if (trace->session) { if (file) { trace->file = FILE_Open(file, WRITE_BINARY_MODE, NULL); if (!trace->file) { PRINT_Error("%s: can't create file %s\n",pname,file); } } if (!file || trace->file) { /* Create thread to parse incoming XML */ if (XRPC_CreateSessionThread(trace->session, GWTRACE_Session,trace)) { return True; } if (trace->file) FILE_Close(trace->file); } } XRPC_UnregisterProtocol(trace->server, ECMTGW_LISTENER_PROTOCOL); } XRPC_FreeServer(trace->server); } return False; }
/** * Same as the above, only allows you to specify file mode (text vs binary) * on those platforms where it matters (e.g. Windows). */ Bool FILE_Save2(Str fname, FileSaveCB cb, void * ctx, Bool txt, IODesc io) { Bool success = False; ASSERT(fname && fname[0]); if (fname && fname[0]) { Char * tmp1 = FILE_DirName(fname, TEMP_FILE_NAME_LEN); if (tmp1 && (!tmp1[0] || FILE_MkDir(tmp1))) { const char * mode = txt ? WRITE_TEXT_MODE : WRITE_BINARY_MODE; size_t dirlen = StrLen(tmp1); File * f; /* * write the temp file */ FILE_MakeUnique(tmp1, dirlen, TEMP_FILE_NAME_LEN); f = FILE_Open(tmp1, mode, io); if (f) { Bool saved = (*cb)(f,fname,ctx); FILE_Close(f); if (saved) { if (FILE_CanRead(fname)) { /* * generate another unique file name */ size_t nchars = dirlen + 1 + TEMP_FILE_NAME_LEN; Char * tmp2 = MEM_NewArray(Char,nchars); if (tmp2) { StrCpy(tmp2, tmp1); FILE_MakeUnique(tmp2,dirlen,TEMP_FILE_NAME_LEN); /* * rename target -> tmp2 */ Verbose(TEXT("Renaming %s into %s\n"),fname,tmp2); if (FILE_Rename(fname, tmp2)) { /* * rename tmp1 -> target */ Verbose(TEXT("Renaming %s into %s\n"),tmp1,fname); if (FILE_Rename(tmp1, fname)) { /* * finally, remove tmp2 (old target) * perhaps, we should ignore the error * returned by FILE_Delete? */ Verbose(TEXT("Deleting %s\n"),tmp2); success = FILE_Delete(tmp2); } } MEM_Free(tmp2); } /* * there's no target, just rename tmp1 -> target */ } else { Verbose(TEXT("Renaming %s into %s\n"),tmp1,fname); success = FILE_Rename(tmp1, fname); } } else { /* * just quietly delete the temporary file. An error * message, if any, should have been provided by the * callback. */ FILE_Delete(tmp1); } } } MEM_Free(tmp1); } return success; }