On 29/11/06, Ian Malone <[EMAIL PROTECTED]> wrote:
I'm trying to write a component that takes an nsILocalFile and
reads from it. The attached code works in Linux/Firefox 1.5.0.8,
but in Windows XP/FF 1.5.0.8 it crashes, apparently at fread.
Does anyone know what might be causing it? I've seen a
mention that statically linked msvcrt may cause problems,
but I don't know much about building under Windows.
Sorry, it seems attachments get stripped, here it is:
#include <cstdlib>
#include <cstdio>
#include "nscore.h"
#include "MyComponent.h" //#include "nsILocalFile.h"
#define LOG_LOC "/home/ian/tmp/log/"
NS_IMPL_ISUPPORTS1(simpleread, IMyComponent)
simpleread::simpleread()
{
/* member initializers and constructor code */
}
simpleread::~simpleread()
{
/* destructor code */
}
/* long Read (in nsIFile input); */
NS_IMETHODIMP simpleread::Read(nsILocalFile *input, PRInt32 *_retval)
{
FILE *logfile;
char *buf = NULL;
NS_IMETHODIMP rv;
nsCOMPtr<nsIFile> File= do_QueryInterface(input,&rv);
PRBool exists;
logfile = fopen(LOG_LOC "logging","w");
if(logfile){
fprintf(logfile,"input %p\n",input);
if (NS_FAILED(rv))
fprintf(logfile,"NS_FAILED\n");
fclose(logfile);
if(NS_FAILED(rv))
goto ERROR;;
}
if(!input) {
return NS_ERROR_NULL_POINTER;
}
logfile = fopen(LOG_LOC "logging.2","w");
if(logfile) { fprintf(logfile,"Logging2\n"); fclose(logfile);}
if (!input)
goto ERROR;
logfile = fopen(LOG_LOC "logging.localfile","w");
if(logfile) fclose(logfile);
rv = File->Exists(&exists);
if (NS_FAILED(rv))
goto ERROR;
logfile = fopen(LOG_LOC "logging.exists","w");
if(logfile) fclose(logfile);
if (exists)
{
PRUint32 fs;
PRInt64 fileSize;
long nativesize;
rv = File->GetFileSize(&fileSize);
if (NS_FAILED(rv))
goto ERROR;
nativesize = fileSize;
logfile = fopen(LOG_LOC "logging.gotsize","w");
if(logfile){fprintf(logfile,"size %li\n", nativesize); fclose(logfile);}
LL_L2UI(fs, fileSize);
FILE* openFile;
FILE* writefile;
rv = input->OpenANSIFileDesc("r", &openFile);
logfile = fopen(LOG_LOC "logging.opened","w");
if(logfile){fprintf(logfile, "openFile %p\n", openFile); fclose(logfile);}
if (NS_FAILED(rv))
goto ERROR;
buf = (char *)malloc(10);
if (!buf) {
fclose (openFile);
goto ERROR;
}
logfile = fopen(LOG_LOC "logging.malloced","w");
if(logfile) fclose(logfile);
size_t numRead;
numRead = fread(buf, 1, 10, openFile);
logfile = fopen(LOG_LOC "logging.read","w");
if(logfile){fprintf(logfile,"size %li\n",(long)numRead); fclose(logfile);}
*_retval = NS_STATIC_CAST(PRInt32,numRead);
fclose(openFile);
logfile = fopen(LOG_LOC "logging.closed","w");
if(logfile){fclose(logfile);}
if (numRead == 10) {
logfile = fopen(LOG_LOC "logging.read10","w");
if(logfile) fclose(logfile);
writefile = fopen(LOG_LOC "test.out","w");
if(writefile) {
fwrite(buf, 1, 10, writefile);
fclose(writefile);
}
}
free(buf);
}
return NS_OK;
ERROR:
logfile = fopen(LOG_LOC "logging.error","w");
if(logfile) fclose(logfile);
return NS_ERROR_FAILURE;
}
_______________________________________________
dev-tech-xpcom mailing list
[email protected]
https://lists.mozilla.org/listinfo/dev-tech-xpcom