Talk:Io.js: Difference between revisions

From MozillaZine Knowledge Base
Jump to navigationJump to search
(re Jed)
Line 26: Line 26:
me thinks about creating his own thin wrapper (like [http://mozilla.doslash.org/prefutils/prefutils.xhtml this for prefs]) for file io...
me thinks about creating his own thin wrapper (like [http://mozilla.doslash.org/prefutils/prefutils.xhtml this for prefs]) for file io...


----
@Nick - Why don't you add prefutils to as an article to KB like is.js?
@Nick - Why don't you add prefutils to as an article to KB like is.js?
Also, do you want to use it for QuickNote? Might make things nicer and more module. --[[User:Jedbro|Jed]] 00:25, 13 Mar 2005 (PST)
Also, do you want to use it for QuickNote? Might make things nicer and more module. --[[User:Jedbro|Jed]] 00:25, 13 Mar 2005 (PST)
:Will think about it and open a bug or mail you about prefs in QN.
:I wanted to write about that wrapper and about other wrappers too, but I haven't had time for that yet. [[User:Asqueella|asqueella]]
----


:They're all great ideas, in my opinion, except "move return false statements out of catch blocks." I want to know when the operation has failed (but not through an exception). Also, don't forget to increment the version counter from 0.1 to 0.2, as well as placing your name in the header comments. In fact, it might be a good idea to have a version history section in the header. Up to you. I have a few other suggestions that I'll type here tonight (code fixes and enhancements).
:They're all great ideas, in my opinion, except "move return false statements out of catch blocks." I want to know when the operation has failed (but not through an exception). Also, don't forget to increment the version counter from 0.1 to 0.2, as well as placing your name in the header comments. In fact, it might be a good idea to have a version history section in the header. Up to you. I have a few other suggestions that I'll type here tonight (code fixes and enhancements).

Revision as of 17:34, 13 March 2005

asqueella's suggestions

I'd like to get point-by-point agree/disagree answer below from interested parties (grimholtz?), add long answers as subsections.

  • rename top-level ids -> FileIO2, DirIO2 to avoid conflicts with older code.
  • change Components.classes[this.dirservCID].createInstance(this.propsIID) -> getService (it is a service).
  • uncomment this ((?) I don't really see why that's commented): // data += uniConv.Finish();
  • remove DirIO.join/DirIO.split. File IO article has example for converting native path to URI and vice versa. I don't know how join/split can be used also, and this is not very clean XP code (hard-coding '\' for windows only doesn't seem smart).
  • rewrite FileIO.path()
  • move return false statements out of catch blocks
  • add dump() statements instead of // foobar!
  • personal: I would like the *CID/*IID members to be removed. I like the idea of creating wrappers for getService/createInstance, see below. This makes code much more readable IMO.
{
  createInstance: function(aCID, aIID) {
    return Components.classes["@mozilla.org/"+aCID]
          .createInstance(Components.interfaces[aIID]);
    },

  getService: function(aCID, aIID) {
    return Components.classes["@mozilla.org/"+aCID]
          .getService(Components.interfaces[aIID]);
  }
}
  • replace tab symbols with two-spaces. It is too wide for my 1024x768 screen now.
  • move examples from code to a separate section, add a link to io.js to the code.

me thinks about creating his own thin wrapper (like this for prefs) for file io...


@Nick - Why don't you add prefutils to as an article to KB like is.js? Also, do you want to use it for QuickNote? Might make things nicer and more module. --Jed 00:25, 13 Mar 2005 (PST)

Will think about it and open a bug or mail you about prefs in QN.
I wanted to write about that wrapper and about other wrappers too, but I haven't had time for that yet. asqueella

They're all great ideas, in my opinion, except "move return false statements out of catch blocks." I want to know when the operation has failed (but not through an exception). Also, don't forget to increment the version counter from 0.1 to 0.2, as well as placing your name in the header comments. In fact, it might be a good idea to have a version history section in the header. Up to you. I have a few other suggestions that I'll type here tonight (code fixes and enhancements).
--grimholtz 13:54, 10 Mar 2005 (EST).
I don't think the changelog should be in the reusable js file itself. We can create a changelog section/subpage though. asqueella


OK, but please include authors and contributors in the js file. --grimholtz 15:13 10 Mar 2005 (EST).

regarding returning false, I meant, change

try {
  return "useful value"
}
catch(e) {
  return false;
}

to

try {
  return "useful value"
} catch(e) {}
return false;
Sounds good! --grimholtz 15:14 10 Mar 2005 (EST).

Also I think returning false on failure isn't smart, null would be better. asqueella


hm, why? I like minimizing the number of types that can be returned because it simplifies code which uses the class --grimholtz 15:15 10 Mar 2005 (EST).
I mean return null in functions that return a string in case of success.
returning false for a function that returns a boolean value is of course correct.
asqueella
Sounds good --grimholtz 16:52 10 Mar 2005 (EST).

grimholtz's suggestions

These are the functions I would change and add to FileIO.


First, I would add read() and write() function overloads which take a path/filename as a string. That way you don't have to continually write code this like:

var fileContents = DirIO.get("ProfD");
fileContents.append('extensions');
fileContents.append('{5872365E-67D1-4AFD-9480-FD293BEBD20D}');
fileContents.append('defaults');
fileContents.append(some-file.xml);
fileContents = FileIO.read(fileContents);

you could just write:

var fileContents = FileIO.read("ProfD/extensions/{5872365E-67D1-4AFD-9480-FD293BEBD20D}/defaults/some-file.xml");

Here's an example (this is untested):

  /**
   * readFiles takes a full path/filename as string and returns its contents.
   * First part of the path must be one of the special path names; e.g., ProfD.
   * You can separate paths with forward slash or backslash; it doesn't matter.
  readFile : function(fileAsString, charset) {

    fileAsString = fileAsString.replace("\\", "/", "g");
    var paths = fileAsString.split("/");
    // first part must be one of the "special" path names, like ProfD or TmpD
    // else DirIO.get() will fail.
    var f = DirIO.get(paths[0]);
    for (p in paths) {
      f.append(paths[p]);
    }
    return this.read(f, charset);
  },


Now here are the changes I would make to existing functions:


  read   : function(file, charset) {
    var fiStream = null;
    var siStream = null;
    try {
      fiStream = Components.classes[this.finstreamCID]
                .createInstance(this.finstreamIID);
      siStream = Components.classes[this.sinstreamCID]
		.createInstance(this.sinstreamIID);
      fiStream.init(file, 1, 0, false);
      siStream.init(fiStream);
      var data = new String(siStream.read(-1));
      return charset ? this.toUnicode(charset, data) : data
    } 
    catch(e) {
      return false;
    }
    finally {
      if (siStream != null)
        siStream.close();
      if (fiStream != null)
        fiStream.close();
    }
},

write  : function(file, data, mode, charset) {
  var foStream = null;
  try {
    foStream = Components.classes[this.foutstreamCID]
	      .createInstance(this.foutstreamIID);
    if (charset) {
      data = this.fromUnicode(charset, data);
    }
    var flags = mode == 'a' ? 0x02 | 0x10 : // wronly | append
                              0x02 | 0x08 | 0x20; // wronly | create | truncate
				
    foStream.init(file, flags, 0664, 0);
    foStream.write(data, data.length);
    return true;
  }
  catch(e) {
    return false;
  }
  finally {
    if (foStream != null)
      foStream.close();
  }
},

And here's another function I would add:

/**
 * Adds the given file to an extension's uninstall list
 * so the file doesn't linger after extension uninstallation.
 * The regular expression isn't correct (yet) so the function
 * doesn't prevent duplicate entries in the Uninstall log.
 * 
 */
addToUninstallList : function(file, extensionGUID) {

  var uninstallFile = DirIO.get("ProfD"); // %Profile% dir
  uninstallFile.append("extensions");
  uninstallFile.append(extensionGUID);
  uninstallFile.append("uninstall");
  uninstallFile.append("Uninstall");
  var data = FileIO.read(uninstallFile);
  // file.leafName actually isn't a sufficient check; we
  // should check the full path after 
  if (!data || new RegExp("^add\\s.*" + file.path, "m").test(data)) {
    // Uninstall file doesn't exist or the given file is
    // already listed in it.
    return;
  }
  // Add new file to the Uninstall list.
  // Note we're not using the FileIO.path() function
  // because it returns a URL-encoded path; e.g., ' ' = %20
  var newEntry = "add\t" + file.path + "\n";
  this.write(uninstallFile, newEntry, "a");
},