/** * Handle a call to fopen() made by JavaScript. * * fopen expects 2 parameters: * 0: the path of the file to open * 1: the mode string * on success, fopen returns a result in |output| separated by \1: * 0: "fopen" * 1: the filename opened * 2: the file index * on failure, fopen returns an error string in |output|. * * @param[in] num_params The number of params in |params|. * @param[in] params An array of strings, parameters to this function. * @param[out] output A string to write informational function output to. * @return An errorcode; 0 means success, anything else is a failure. */ int HandleFopen(int num_params, char** params, char** output) { FILE* file; int file_index; const char* filename; const char* mode; if (num_params != 2) { *output = PrintfToNewString("Error: fopen takes 2 parameters."); return 1; } filename = params[0]; mode = params[1]; file = fopen(filename, mode); if (!file) { *output = PrintfToNewString("Error: fopen returned a NULL FILE*."); return 2; } file_index = AddFileToMap(file); if (file_index == -1) { *output = PrintfToNewString( "Error: Example only allows %d open file handles.", MAX_OPEN_FILES); return 3; } *output = PrintfToNewString("fopen\1%s\1%d", filename, file_index); return 0; }
/** * Handle a call to fopen() made by JavaScript. * * fopen expects 2 parameters: * 0: the path of the file to open * 1: the mode string * on success, fopen returns a result in |output|: * 0: "fopen" * 1: the filename opened * 2: the file index * on failure, fopen returns an error string in |out_error|. */ int HandleFopen(struct PP_Var params, struct PP_Var* output, const char** out_error) { CHECK_PARAM_COUNT(fopen, 2); PARAM_STRING(0, filename); PARAM_STRING(1, mode); FILE* file = fopen(filename, mode); if (!file) { *out_error = PrintfToNewString("fopen returned a NULL FILE*"); return 1; } int file_index = AddFileToMap(file); if (file_index == -1) { *out_error = PrintfToNewString("Example only allows %d open file handles", MAX_OPEN_FILES); return 1; } CREATE_RESPONSE(fopen); RESPONSE_STRING(filename); RESPONSE_INT(file_index); return 0; }