<commit_before>'use babel';

import { CompositeDisposable } from 'atom';

const fs = require('fs');

function toHex(filename){
  var data = fs.readFileSync(filename);

  return data
}

function formatHex(arr){
  var finalString = '0000000 ';
  var lineNumber = 1;
  var lastLineLength = 0;
  for (var i = 0; i < arr.length; i++){
    var byte = arr[i].toString(16);

    if (byte.length == 1){
      byte = '0' + byte;
    }
    if ((i+1) % 16 == 0 && i != 0){
      var formattedLnNum = lineNumber.toString(16) + '0';
      while (formattedLnNum.length < 7){
        formattedLnNum = '0' + formattedLnNum;
      }
      finalString += byte + ' ' + '\n'+ formattedLnNum + ' ';
      lineNumber += 1;
      lastLineLength = 0;
    }
    else{
      finalString += byte + ' ';
      lastLineLength += 1;
    }
  }
  var footerNumber = (lineNumber-1).toString(16)+lastLineLength.toString(16);
  while (footerNumber.length < 7){
    footerNumber = '0'+footerNumber;
  }
  finalString += '\n' + footerNumber + '\n';
  finalString = finalString.toLowerCase();
  return finalString
}

function decodeHex(string){
  var deformatted = '';

  var lines = string.split('\n');
  console.log(lines);

  for(var i = 0; i < lines.length; i++){
    var bytes = lines[i].split(' ');
    for(var j = 0; j < bytes.length; j++){
      if(bytes[j].length < 7){
        deformatted += bytes[j];
      }
    }
  }

  //  'sko
  console.log(deformatted);
  var buff = Buffer.from(deformatted, 'hex');
  return buff.toString();
}

function decodeHexAndStore(string, path){
  var deformatted = '';
  var listified = string.split(' ');
  for (var i = 0; i < listified.length; i++){
    if (listified[i].length < 7){
      deformatted += listified[i];
    }
  }

  var buff = Buffer.from(deformatted, 'hex');
  fs.writeFile(path, buff, function(err){})
}

function storeInFile(path, content){
  fs.writeFile(path, content, function (err){
      if (err){
        alert(err);
      }
    }
  );
}

function storeInTab(text){
    atom.workspace.open().then(function(t){
        t.insertText(text);
    });
}

function padLeft(size, str, char){
  if(size > str.length){
    while(str.length < size - 1){
      str = char + str;
    }
  }
  return str;
}

function hexFromFile(path){
  var readStream = fs.createReadStream(path, 'hex');

  var lineNum = 0;
  var numBytes = 0;
  var line = "";
  var hd = "";

  atom.workspace.open().then(function(t){
    readStream.on('data', function(buffer){
      var buff = buffer.toString();
      for(var i = 0; i < buff.length; i += 2){
        line += buff.substring(i, i+2) + " ";
        numBytes ++;
        if(numBytes >= 16){
          numBytes = 0;

          hd += padLeft(7, lineNum.toString(16), "0") + "0" + " " + line + "\n";
          line = "";
          lineNum ++;
        }
      }
      t.insertText(hd);
      hd = "";
    });
    readStream.on('end', function(){
      t.insertText(padLeft(7, lineNum.toString(16), "0") + "0" + " " + line + "\n");
      if(numBytes != 0){
        t.insertText(padLeft(7, lineNum.toString(16), "0") + numBytes.toString(16) + " ");
      }
    });
  });
}

export default {

  hexEditorView: null,
  modalPanel: null,
  subscriptions: null,

  activate(state) {
    // Events subscribed to in atom's system can be easily cleaned up with a CompositeDisposable
    this.subscriptions = new CompositeDisposable();

    //Register commands
    this.subscriptions.add(atom.commands.add('atom-workspace', {
      'hex-editor:encode': () => this.encode(),
      'hex-editor:decode': () => this.decode()
    }));
  },

  deactivate() {
    this.subscriptions.dispose();
    this.hexEditorView.destroy();
  },

  encode() {
    let editor;
    if (editor = atom.workspace.getActivePaneItem()) {
      let fileTitle = editor.getTitle();
      let filePath = editor.getPath();
      if(filePath != undefined){
        hexFromFile(filePath);
      }
    }
  },

  decode() {
    let editor;
    if (editor = atom.workspace.getActivePaneItem()) {
      var selection = editor.getText();
      var decoded = decodeHex(selection);
      storeInTab(decoded);
    }
  }
};<commit_msg>Removed some unused code<commit_after>'use babel';

import { CompositeDisposable } from 'atom';

const fs = require('fs');

// function that decodes a hex dump string
function decodeHex(string){
  var deformatted = '';

  // split the file into lines
  var lines = string.split('\n');

  for(var i = 0; i < lines.length; i++){
    // eliminate whitespace and ignore line numbers to extract bytes of the file
    var bytes = lines[i].split(' ');
    for(var j = 0; j < bytes.length; j++){
      if(bytes[j].length < 7){
        deformatted += bytes[j];
      }
    }
  }

  // put the raw hex into a buffer and conver that into a string to decode
  var buff = Buffer.from(deformatted, 'hex');
  return buff.toString();
}

//function that appends a string to a tab
function storeInTab(text){
  atom.workspace.open().then(function(t){
    t.insertText(text);
  });
}

// Simple function to add a character to the left side of a string until length equals size
function padLeft(str, char, size){
  if(size > str.length){
    var padding = char.repeat(size - str.length);
    str = padding + str;
  }
  return str;
}

// Function that generates a hex dump from a file
function hexFromFile(path){
  var readStream = fs.createReadStream(path, 'hex');

  var lineNum = 0;
  var numBytes = 0;
  var line = "";
  var hd = "";

  // Open a new tab after openning a new read stream
  atom.workspace.open().then(function(t){
    readStream.on('data', function(buffer){
      // Convert the buffer into a string
      var buff = buffer.toString();

      // Split the string of the buffer into two character long chunks (bytes)
      for(var i = 0; i < buff.length; i += 2){
        line += buff.substring(i, i+2) + " ";
        numBytes ++;

        // If 16 bytes have been processed for this line...
        if(numBytes >= 16){
          numBytes = 0;

          // Create a new line by adding the line number to the 16 byte string
          hd += padLeft(lineNum.toString(16), "0", 6) + "0" + " " + line + "\n";
          line = "";
          lineNum ++;
        }
      }

      //insert that line
      t.insertText(hd);
      hd = "";
    });
    readStream.on('end', function(){
      // Insert the last line which may not be full
      t.insertText(padLeft(lineNum.toString(16), "0", 6) + "0" + " " + line + "\n");

      // Add an ending line that shows how many bytes the previous line contained if that line was not complete
      if(numBytes != 0){
        t.insertText(padLeft(lineNum.toString(16), "0", 6) + numBytes.toString(16) + " ");
      }
    });
  });
}

export default {

  hexEditorView: null,
  modalPanel: null,
  subscriptions: null,

  activate(state) {
    // Events subscribed to in atom's system can be easily cleaned up with a CompositeDisposable
    this.subscriptions = new CompositeDisposable();

    // Register commands
    this.subscriptions.add(atom.commands.add('atom-workspace', {
      'hex-editor:encode': () => this.encode(),
      'hex-editor:decode': () => this.decode()
    }));
  },

  deactivate() {
    this.subscriptions.dispose();
    this.hexEditorView.destroy();
  },

  // Command that generates the hex dump of a file
  encode() {
    let editor;
    if (editor = atom.workspace.getActivePaneItem()) {
      let filePath = editor.getPath();
      if(filePath != undefined){
        hexFromFile(filePath);
      }
    }
  },

  // Command that converts a hex dump back to the original file
  decode() {
    let editor;
    if (editor = atom.workspace.getActivePaneItem()) {
      // get the contents of the editor as a string to make decoding unsaved files easier
      var selection = editor.getText();
      var decoded = decodeHex(selection);
      storeInTab(decoded);
    }
  }
};
{'fraction_non_alphanumeric': 0.15134928716904278, 'fraction_numerical': 0.01234725050916497, 'mean_word_length': 2.6041284403669724, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '575040', 'n_tokens_mistral': 2621, 'n_tokens_neox': 2506, 'n_words': 680}