INI.js(模块)
var eol = process.platform === "win32" ? "\r\n" : "\n"
function INI() {
this.sections = {};
}
/**
* 删除Section
* @param sectionName
*/
INI.prototype.removeSection = function (sectionName) {
sectionName = sectionName.replace(/\[/g,'(');
sectionName = sectionName.replace(/]/g,')');
if (this.sections[sectionName]) {
delete this.sections[sectionName];
}
}
/**
* 创建或者得到某个Section
* @type {Function}
*/
INI.prototype.getOrCreateSection = INI.prototype.section = function (sectionName) {
sectionName = sectionName.replace(/\[/g,'(');
sectionName = sectionName.replace(/]/g,')');
if (!this.sections[sectionName]) {
this.sections[sectionName] = {};
}
return this.sections[sectionName]
}
/**
* 将INI转换成文本
*
* @returns {string}
*/
INI.prototype.encodeToIni = INI.prototype.toString = function encodeIni() {
var _INI = this;
var sectionOut = _INI.encodeSection(null, _INI);
Object.keys(_INI.sections).forEach(function (k, _, __) {
if (_INI.sections) {
sectionOut += _INI.encodeSection(k, _INI.sections[k])
}
});
return sectionOut;
}
/**
*
* @param section
* @param obj
* @returns {string}
*/
INI.prototype.encodeSection = function (section, obj) {
var out = "";
Object.keys(obj).forEach(function (k, _, __) {
var val = obj[k]
if (val && Array.isArray(val)) {
val.forEach(function (item) {
out += safe(k + "[]") + " = " + safe(item) + "\n"
})
} else if (val && typeof val === "object") {
} else {
out += safe(k) + " = " + safe(val) + eol
}
})
if (section && out.length) {
out = "[" + safe(section) + "]" + eol + out
}
return out+"\n";
}
function safe(val) {
return (typeof val !== "string" || val.match(/[\r\n]/) || val.match(/^\[/) || (val.length > 1 && val.charAt(0) === "\"" && val.slice(-1) === "\"") || val !== val.trim()) ? JSON.stringify(val) : val.replace(/;/g, '\\;')
}
var regex = {
section: /^\s*\[\s*([^\]]*)\s*\]\s*$/,
param: /^\s*([\w\.\-\_]+)\s*=\s*(.*?)\s*$/,
comment: /^\s*;.*$/
};
/**
*
* @param data
* @returns {INI}
*/
exports.parse = function (data) {
var value = new INI();
var lines = data.split(/\r\n|\r|\n/);
var section = null;
lines.forEach(function (line) {
if (regex.comment.test(line)) {
return;
} else if (regex.param.test(line)) {
var match = line.match(regex.param);
if (section) {
section[match[1]] = match[2];
} else {
value[match[1]] = match[2];
}
} else if (regex.section.test(line)) {
var match = line.match(regex.section);
section = value.getOrCreateSection(match[1])
} else if (line.length == 0 && section) {
section = null;
}
;
});
return value;
}
/**
* 创建INI
* @type {Function}
*/
exports.createINI = exports.create = function () {
return new INI();
};
var fs = require('fs');
exports.loadFileSync =function(fileName/*,charset*/){
return exports.parse(fs.readFileSync(fileName, "utf-8")) ;
}
conf.ini:
[user]
username=test
password=123456
实际调用实例:
var INI = require("../ini/INI");//INI模块
var ini___ = INI.loadFileSync("conf.ini")//从conf.ini读取配置
var se = ini___.getOrCreateSection("user");//取得httpserver
var username = se['username'];
var password= se['password'];
【无私分享:ASP.NET CORE 项目实战(第八章)】读取配置文件(二) 读取自定义配置文件
目录索引 [无私分享:ASP.NET CORE 项目实战]目录索引 简介 我们在 读取配置文件(一) appsettings.json 中介绍了,如何读取appsettings.json. 但随之产生 ...
解决IntelliJ IDEA无法读取配置文件的问题
解决IntelliJ IDEA无法读取配置文件的问题 最近在学Mybatis,按照视频的讲解在项目的某个包里建立配置文件,然后读取配置文件,但是一直提示异常. 读取配置文件的为官方代码: String ...
java-工具类-读取配置文件
java读取配置文件,当发现文件被修改后则重新加载 package com.zg.config; import java.io.File; import java.io.FileInputStream ...
java 4种方式读取配置文件 + 修改配置文件
版权声明:本文为博主原创文章,未经博主允许不得转载. 目录(?)[-] 方式一采用ServletContext读取读取配置文件的realpath然后通过文件流读取出来 方式二采用ResourceB ...
在IIS Express中调试时无法读取配置文件 错误
在IIS Express中调试代码时,如果出现"无法读取配置文件"的问题(如图),这种情况是IIS Express的"applicationhost.config&quo ...
ASP.NET Core开发-读取配置文件Configuration
ASP.NET Core 是如何读取配置文件,今天我们来学习. ASP.NET Core的配置系统已经和之前版本的ASP.NET有所不同了,之前是依赖于System.Configuration和XML ...
Java 利用 ByteArrayOutputStream 和 ByteArrayInputStream 避免重复读取配置文件
最近参与了github上的一个开源项目 Mycat,是一个mysql的分库分表的中间件.发现其中读取配置文件的代码,存在频繁多次重复打开,读取,关闭的问题,代码写的很初级,稍微看过一些框架源码的人,是 ...
Python+Selenium进行UI自动化测试项目中,常用的小技巧2:读取配置文件(configparser,.ini文件)
在自动化测试项目中,可能会碰到一些经常使用的但 很少变化的配置信息,下面就来介绍使用configparser来读取配置信息config.ini 读取的信息(config.ini)如下: [config ...
win7 web开发遇到的问题-由于权限不足而无法读取配置文件,无法访问请求的页面
错误一: HTTP Error 500.19 - Internal Server Error配置错误: 不能在此路径中使用此配置节.如果在父级别上锁定了该节,便会出现这种情况.锁定是默认设置的 (ov ...
随机推荐
python os.path模块
os.path.abspath(path) #返回绝对路径 os.path.basename(path) #返回文件名 os.path.commonprefix(list) #返回list(多个路径) ...
PHP注册审核做法
1.注册页面 ploadForm" action= "http://localhost:8080/cfJAX_ ...
Screen对象
document.write("Screen-width:"+screen.width+"Screen-height:"+screen.height);docu ...
Linux 消息队列编程
消息队列.信号量以及共享内存被称作 XSI IPC,它们均来自system V的IPC功能,因此具有许多共性. 键和标识符: 内核中的每一种IPC结构(比如信号量.消息队列.共享内存)都用一个非负整数 ...
【转】WCF、WebAPI、WCFREST、WebService之间的区别
在.net平台下,有大量的技术让你创建一个HTTP服务,像Web Service,WCF,现在又出了Web API.在.net平台下,你有很多的选择来构建一个HTTP Services.我分享一下我对 ...
探索ArrayList自动改变size真相
探索ArrayList自动改变size真相 ArrayList的列表对象实质上是存储在一个引用型数组里的,有人认为该数组有“自动增长机制”可以自动改变size大小.正式地说,该数组是无法改变 大小的, ...
虚拟机安装CentOS7之后没有ip的问题
CentOS 7 默认是不启动网卡的(ONBOOT=no),主要是修改一下网上配置,然后重起便可,看这篇博客操作: https://blog.csdn.net/dancheren/article/de ...
ES--03
第二十一讲! 1.上机动手实战演练基于_version进行乐观锁并发控制 (1)先构造一条数据出来 PUT /test_index/test_type/7{ "test_field" ...